user2184598
user2184598

Reputation: 63

Java FileInputStream

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

Upvotes: 3

Views: 2175

Answers (5)

user207421
user207421

Reputation: 310874

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.

Upvotes: 0

ouotuo
ouotuo

Reputation: 64

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();
}

Upvotes: -1

gerrytan
gerrytan

Reputation: 41123

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();

Upvotes: 0

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).

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

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.

Upvotes: 6

Related Questions