hey guys,
can somebody please help me understanding why this code doesn t work proparly with .docx files. it works with .txt files though.
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Encrypter {
/**this class shall read the content of a designated file,
**encrypt the content,
*and writte the new data in a new file
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Scanner kibod=new Scanner(System.in);
System.out.println("enter file name, and file location: ");
String filename=kibod.nextLine();
FileInputStream file_in=new FileInputStream(filename);
DataInputStream reader=new DataInputStream(file_in);
kibod.nextLine();
byte[] data=new byte[30];
reader.read(data);
reader.close();
System.out.println("enter new location for encrypted file: ");
String newfile=kibod.nextLine();
FileOutputStream file_out= new FileOutputStream(newfile);
DataOutputStream writter=new DataOutputStream(file_out);
for (int i=0;i<data.length;i++){
writter.write(data[i]+data[3]);
}
writter.close();
}
}
susggestion on a better alternative to the" data array" would be more then welcomed.
thanks in advance...
6 replies to this topic
#1
Posted 18 December 2010 - 02:04 PM
|
|
|
#2
Posted 19 December 2010 - 02:45 AM
for txt-files it works because it's just text, and what you do is read a character, add some value to it so it becomes another character and write it back.. But it's still text.
A docx files will contain a lot more than just plain text. It will also contain a header with info about the file, it contains fonts used, colors, ...
if you just change that info by adding a random value, word won't be able to read the header properly, all the fonts don't exist,..... thus word won't open it.
Have you opened a docx file in notepad once? All the trash you see... Word can read and understand it. When you change that trash to .. other trash word won't be able to make anything out of it.
As your code currently is, you can aswell just read characters using a scanner on the textfile, then do char+20 to get another value and write the character. It makes more sence than working with bytes.
A docx files will contain a lot more than just plain text. It will also contain a header with info about the file, it contains fonts used, colors, ...
if you just change that info by adding a random value, word won't be able to read the header properly, all the fonts don't exist,..... thus word won't open it.
Have you opened a docx file in notepad once? All the trash you see... Word can read and understand it. When you change that trash to .. other trash word won't be able to make anything out of it.
As your code currently is, you can aswell just read characters using a scanner on the textfile, then do char+20 to get another value and write the character. It makes more sence than working with bytes.
#3
Posted 19 December 2010 - 08:53 AM
thanks alot for your answer it makes sence....but can u suggest me how i can successfully read .docx file and create a copy of the file .....
#4
Posted 19 December 2010 - 01:13 PM
#5
Posted 19 December 2010 - 07:50 PM
nm.
#6
Posted 24 December 2010 - 08:29 PM
I've created a very similar program that will encrypt any type of file.... check out my tutorial here on the forums. My program opens the files in binary mode, then rewrites them. It could be modified to copy a file.
http://forum.codecal...encryption.html Tutorial
http://forum.codecal...encryption.html Tutorial
Your thoughts are the architects of your destiny.
[SIGPIC][/SIGPIC]
[SIGPIC][/SIGPIC]
#7
Posted 27 February 2011 - 06:13 AM
hmm i just saw your post and i wish i had seen it when you posted it, it was really helpful! so thanks again for your post.
besides, i m trying to implement conway's game of life. for this wrote a "cell" class that extends (components or Rectangle: open for suggestions). objects of this class will have the ability to set their own color according to the colors of their neighbors. an other class that extends JPanel will build a grid of these objects and show them. after a x time, each cell will change its color using specific rules.
besides, i m trying to implement conway's game of life. for this wrote a "cell" class that extends (components or Rectangle: open for suggestions). objects of this class will have the ability to set their own color according to the colors of their neighbors. an other class that extends JPanel will build a grid of these objects and show them. after a x time, each cell will change its color using specific rules.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









