This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes how to read a file data into byte array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "how to read a file data into byte array" Watch "how to read a file data into byte array" New topic
Author

how to read a file data into byte array

taylor mark
Greenhorn

Joined: Jan 27, 2005
Posts: 7
Hi,

I want to read a file content into byte array from server and I don't know how big the file is. Can any one help how do I read a file content into a byte array? (the file may 1MB or 10k size).
Any help please..

Thanks.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8838
    
    7

Use a ByteArrayOutputStream:

This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray()


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
taylor mark
Greenhorn

Joined: Jan 27, 2005
Posts: 7
Hi Right now I am using the following code to read the data from file into a StringBuffer. This is working fine but the problems is with new line character.
protected String read(String fileName) {
StringBuffer buffer = new StringBuffer();
String newline = System.getProperty("line.separator");
if(fileName == null) return null;
File file = new File(fileName);
try{
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = "";
while ( (line = reader.readLine()) != null) {
buffer.append(line);
buffer.append(newline);
}
}catch(Exception e) { throw new RuntimeException (e); }
return buffer.toString().trim();
}

The new line character is diff on windows and unix. FileReader removes the line character. That's why I am addding that new line character again.
The file contains new line. I want to read that new line also.
How do I get new line also in the buffer instead of appending after reading each line. I don't want append new line after reading each line. I want to read new line also.
Any otherway or sample code please...

Thanks,
taylor mark
Greenhorn

Joined: Jan 27, 2005
Posts: 7
Hi Joe,

Can you provide sample code that reads a file using ByteArrayOutputStream()?

Thanks,
taylor mark
Greenhorn

Joined: Jan 27, 2005
Posts: 7
Hi,

Can any one provide some sample code that reads new line character also?
I don't want to append newline after reading each line.

Thanks.
taylor mark
Greenhorn

Joined: Jan 27, 2005
Posts: 7
Hi I got solution for the problem. I am reading the file using the following code.
import java.io.*;

class Ran {
public static void main(String ars[]) throws Exception{
FileInputStream fis = new FileInputStream("SSO.txt");
int x= fis.available();
byte b[]= new byte[x];
fis.read(b);
String content = new String(b);
System.out.println(content);
}
}

I want to read the hole file as it is. The above code does that.

Thanks.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8838
    
    7

A trap many people fall into: InputStream.available() does not return the size of the file:
Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream

Javadoc for InputStream

Also, FileReader does not consume line separators, BufferedReader does:

public String readLine() throws IOException

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Javadoc for BufferedReader

Take a look at this example from the The Java Developers Almanac
Jill Nilsson
Greenhorn

Joined: Apr 30, 2005
Posts: 6
My appologize, for not being more clear.
Questions:
1.Where am I suppose to place the file? As far as the path and location is concerened?
2. Once it it is working, how do I actually read out the records to the screen/console or a new file.

I am thinking that I should use :
while next().....//as long as there are reords in the file.

Can anyone give some directions?
Thanks again
Jill Nilsson
Greenhorn

Joined: Apr 30, 2005
Posts: 6
Please disregard the previous comment, I posted on the wrong thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to read a file data into byte array
 
Similar Threads
Trouble Converting from Int Array to Byte Array
Reading file into byte array
sending image file from applet to servlet
inputstream to bytebuffer
Reading a file using NIO