[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Reading and writing files

Posted on 2005-04-29
9
Medium Priority
?
224 Views
Last Modified: 2010-03-31
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
Comment
Question by:al_bluwi
  • 5
  • 3
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13892541
> 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
 
LVL 92

Expert Comment

by:objects
ID: 13892542
Reader/Writers are for dealing with text data, and not binary data
0
 

Author Comment

by:al_bluwi
ID: 13892572
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13892583
> 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
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13892587
           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
 

Author Comment

by:al_bluwi
ID: 13892613
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
 

Author Comment

by:al_bluwi
ID: 13892620
in.read() ====> isnt it the same in both inputstream and bufferedReader?
0
 
LVL 92

Expert Comment

by:objects
ID: 13892633
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question