0

The following program should print the String "Humpty Dumpty sat on a wall,\n Humpty Dumpty had a great fall." to a file and input it back.

package io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

public class ByteIO {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String output = "Humpty Dumpty sat on a wall,\n Humpty Dumpty had a great fall.";
        System.out.println("Output String : " + output);
        try(PrintStream out = new PrintStream("F:\\Test.txt")) {
            out.println(output);
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        }

        String input = "";
        try(FileInputStream in = new FileInputStream("F:\\Test.txt")) {
            while(in.read() != -1)
                input += (char)in.read();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
        System.out.println("Input String : " + input);
    }
}

However, the String I got from the FileInputStream was "upyDmt a nawl, upyDmt a ra al?"! Also, when I opened the file "Test.txt" I found that the output String had become "Humpty Dumpty sat on a wall, Humpty Dumpty had a great fall." in a single line. Where did the \n go?

5
  • 1
    The \n is there, but maybe your editor won't show it. Regarding your input: your loop is broken insofar as it delivers only every 2nd character. Commented Dec 18, 2012 at 14:29
  • You should close the file between writing and reading from it. Commented Dec 18, 2012 at 14:55
  • @JavaNewbie always accept the answer, when you got your solution. Commented Dec 19, 2012 at 13:10
  • @PeterLawrey Not required, because I use try-with-resources : The file is automatically closed at the end of the try block. Commented Dec 19, 2012 at 13:21
  • @JavaNewbie_M107 Using try-with will solve this. I would catch the exceptions unles syou are going to do something useful with them. If you don't catch teh exception, it will print them out anyway. Commented Dec 19, 2012 at 13:46

4 Answers 4

4

You are calling in.read() twice:

while(in.read() != -1)
    input += (char)in.read();

This reads two characters each iteration instead of one, so you are effectively discarding a character each time.

Try storing the character in the while condition, then just adding that character to input:

EDIT: based on JavaNewbie_M107's comment

int i;    
while((i = in.read()) != -1)
   input += (char)i;
Sign up to request clarification or add additional context in comments.

2 Comments

The problem with your code is that the char c will never return -1, instead, it will keep returning zero, thus setting an infinite loop.
@JavaNewbie_M107 You are absolutely right, I missed that earlier. I will make an edit.
1

For the second part, Windows (and many applications like Notepad) do not recognize \n as new line. In Windows, \r\n marks a new line. Try opening with a more serious edit program (WordPad should suffice) and you will see it correctly formatted.

Comments

0

As Hunter said, you need to change your code to this

char c;
while((c=in.read()) != -1)
input += c;

Comments

0

This is the correct solution to my problem :

int i = 0;
char c;
    do {
        c = (char)i;
        input += c;
        i = in.read();
    } while(i != -1);

The extra space at the front is removed by using a .trim() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.