• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to read a file data into byte array

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()

 
taylor mark
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe,

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

Thanks,
 
taylor mark
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please disregard the previous comment, I posted on the wrong thread.
reply
    Bookmark Topic Watch Topic
  • New Topic