3

I am trying to use a FileInputStream to essentially read in a text file, and then output it in a different text file. However, I always get very strange characters when I do this. I'm sure it's some simple mistake I'm making, thanks for any help or pointing me in the right direction. Here's what I've got so far.

    File sendFile = new File(fileName);
    FileInputStream fileIn = new FileInputStream(sendFile);
    byte buf[] = new byte[1024];
    while(fileIn.read(buf) > 0) {
        System.out.println(buf);
    }

The file it is reading from is just a big text file of regular ASCII characters. Whenever I do the system.out.println, however, I get the output [B@a422ede. Any ideas on how to make this work? Thanks

1
  • Note that you can't read text/strings/characters with an InputStream. You need a Reader for that. You can convert bytes to characters with an InputStreamReader. Commented Mar 19, 2013 at 1:37

5 Answers 5

6

This happens because you are printing a byte array object itself, rather than printing its content. You should construct a String from the buffer and a length, and print that String instead. The constructor to use for this is

String s = new String(buf, 0, len, charsetName);

Above, len should be the value returned by the call of the read() method. The charsetName should represent the encoding used by the underlying file.

Sign up to request clarification or add additional context in comments.

3 Comments

No, that's definitely not the constructor to use! Whenever you're converting from bytes to a string or from a string to bytes, you need an encoding! new String(bytes, encoding). If you don't specify an encoding, you get the platform default, which is for all intents and purposes random. Also you can't use any random offset and length into a buffer, you may end up cutting off a multibyte character.
@ChristofferHammarström You're right, I added the 4-th arg. Thanks!
Also, len should not be the value returned by the read() method, as it may cut in the middle of a multibyte character and isn't guaranteed to read all of the file. Use an InputStreamReader instead.
1

If you're reading from a file to another file, you shouldn't convert the bytes to a string at all, just write the bytes read into the other file.

If your intention is to convert a text file from an encoding to another, read from a new InputStreamReader(in, sourceEncoding), and write to a new OutputStreamWriter(out, targetEncoding).

1 Comment

All, pay attention to christoffer-hammarstrom@'s comments on other answer's here as well.
0

That's because printing buf will print the reference to the byte array, not the bytes themselves as String as you would expect. You need to do new String(buf) to construct the byte array into string

Also consider using BufferedReader rather than creating your own buffer. With it you can just do

String line = new BufferedReader(new FileReader("filename.txt")).readLine();

2 Comments

No, never use FileReader, it should be deprecated, because it has no way of specifying an encoding. Which means your text will be randomly corrupted depending on the platform encoding, which is essentially random.
Also, never use BufferedReader.readLine() for data that you need to read exactly as it is. It eats your newline characters, so you can never be sure you have a perfect copy of the newlines in the input.
0

Your loop should look like this:

int len;
while((len = fileIn.read(buf)) > 0) {
        System.out.write(buf, 0, len);
    }

You are (a) using the wrong method and (b) ignoring the length returned by read(), other than checking it for < 0. So you are printing junk at the end of each buffer.

Comments

-1

the object 's defualt toString method is return object's id in the memory. byte buf[] is an object.

you can print using this.

File sendFile = new File(fileName);
FileInputStream fileIn = new FileInputStream(sendFile);
byte buf[] = new byte[1024];

while(fileIn.read(buf) > 0) {
    System.out.println(Arrays.toString(buf));
}

or

 File sendFile = new File(fileName);
FileInputStream fileIn = new FileInputStream(sendFile);
byte buf[] = new byte[1024];
int len=0;
while((len=fileIn.read(buf)) > 0) {
    for(int i=0;i<len;i++){
        System.out.print(buf[i]);
    }
    System.out.println();
}

1 Comment

The first version does not work as it takes no account of the length returned by read(). The second version is several times as complex as necessary. All you need is System.out.write(buf, 0, len);

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.