• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Reading and writing files

Hello there....

what I am trying to do is the following:
I want to read any file of any extension, write it as a 1s and 0s to a txt file, then read the txt file and write back my original file.

for example if I have a jpg file, I want to read it, and make a new txt file which when I open it I will see 111111000000111010101101010 and so on, and then read this file and write back my old jpg picture.

I want to be able to do this for any type of file.

I have written some code which is supposed to work, but I dont know why it is not working.
here is the code:

The program that reads:

public class test2 {

    public static void main(String[] args) {
       
        try {
            BufferedReader in = new BufferedReader(new FileReader(new File("ibraheem.jpg")));
            BufferedWriter out = new BufferedWriter(new FileWriter(new File("out23.txt")));
           
            int number;
            boolean first = true;
           
            while ((number=in.read()) != -1){
               
                if(!first)
                    out.newLine();
               
                out.write(Integer.toBinaryString(number));
                out.flush();
               
                first = false;
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}


the program that wirtes:

public class test3 {

    public static void main(String[] args) {
       
        try {
            BufferedReader in = new BufferedReader(new FileReader(new File("out23.txt")));
            BufferedWriter out = new BufferedWriter(new FileWriter(new File("newIbraheem.jpg")));
           
            String number;
           
            while ((number=in.readLine()) != null){
                out.write(Integer.parseInt(number, 2));
                out.flush();
            }
        }
        catch(Exception e){
            System.out.println(e);
        }
    }
}


any idea about why it is not giving me the original file correctly?

waiting....

thanks in advance.
0
al_bluwi
Asked:
al_bluwi
  • 5
  • 3
1 Solution
 
objectsCommented:
> BufferedReader in = new BufferedReader(new FileReader(new File("ibraheem.jpg")));

you should be using a InputStream instead of a Reader

>   BufferedWriter out = new BufferedWriter(new FileWriter(new File("newIbraheem.jpg")));

And an OutputStream instead of a Writer
0
 
objectsCommented:
Reader/Writers are for dealing with text data, and not binary data
0
 
al_bluwiAuthor Commented:
if I use inputstream and output stream I wouldn't be able to read or write lines.

say the first number in the file was 1 and the second was 1, if my output was 11 how would I know that it is 1 and 1 not 3?

thats why I outputing each on a seperate line.

What I am actually looking for is a constant way to XOR a word with a file.

if it is a text file then I convert it into binary, then convert the word into binary and do the XORing, it worked with text files.

now with non-txt files, I want to have my file as a 1s and 0s file so that I can do the XORing just like what I did before.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
zzynxSoftware engineerCommented:
0
 
objectsCommented:
> if I use inputstream and output stream I wouldn't be able to read or write lines.

If you are dealing with binary file there is no concept of lines, that only applies to text files.

> say the first number in the file was 1 and the second was 1, if my output was 11 how would I know that it is 1 and 1 not 3?

You still use Reader/Writer for dealing with the generated text file.
0
 
objectsCommented:
           InputStream in = new BufferedInputStream(new FileInputStream(new File("ibraheem.jpg")));
            BufferedWriter out = new BufferedWriter(new FileWriter(new File("out23.txt")));


and

            BufferedReader in = new BufferedReader(new FileReader(new File("out23.txt")));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("newIbraheem.jpg")));
0
 
al_bluwiAuthor Commented:
hehee...thats funny, it worked!!

Thanks objects, but can you please explain why the reader and writer dont do the same thing as the the input and output streams? arent they both reading integers and outputting integers?

thanks again
0
 
al_bluwiAuthor Commented:
in.read() ====> isnt it the same in both inputstream and bufferedReader?
0
 
objectsCommented:
because readers read the bytes and convert them to characters, and writers write characters (after converting them to bytes). the conversion process depends on the character encoding in place.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now