Jump to content

java I/O BINARY FILES

- - - - -

  • Please log in to reply
6 replies to this topic

#1
fabiniyoung

fabiniyoung

    Newbie

  • Members
  • PipPip
  • 11 posts
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...

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
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.

#3
fabiniyoung

fabiniyoung

    Newbie

  • Members
  • PipPip
  • 11 posts
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
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'd definitely check out Apache POI.
Apache POI - the Java API for Microsoft Documents

#5
Liars_paradox

Liars_paradox

    Newbie

  • Members
  • PipPip
  • 15 posts
nm.

#6
TALucas

TALucas

    Learning Programmer

  • Members
  • PipPipPip
  • 91 posts
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
Your thoughts are the architects of your destiny.
[SIGPIC][/SIGPIC]

#7
fabiniyoung

fabiniyoung

    Newbie

  • Members
  • PipPip
  • 11 posts
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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users