wood burning stoves*
The moose likes I/O and Streams and the fly likes problem while dispalying from file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem while dispalying from file" Watch "problem while dispalying from file" New topic
Author

problem while dispalying from file

laxmikant sabane
Greenhorn

Joined: Jun 04, 2008
Posts: 3
Hi,
I wrote the following code.
The dis object reads the text from file. but it gives the problem while displaying on screen. The following line doesnt display the contents read in buffer 'aoBuffer'.

System.out.println(aoBuffer.toString());

Can anyone help me to solve the problem??




import java.io.*;

public class File_read {

String record = null;
int recCount = 0;

public static void main(String args[]){
String filename = null;
byte aoBuffer[] = new byte[256];



System.out.println("Enter the file name \n");
try {
int record = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
filename = br.readLine();
File f = new File(filename.toString());
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);

while(record != (-1)) {
record = dis.read(aoBuffer, 0, aoBuffer.length);

System.out.println(aoBuffer.toString());
System.out.println(record);
}
dis.close();


}
catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error!" + e.getMessage());

}
}

}
[ August 26, 2008: Message edited by: laxmikant sabane ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
following line doesnt display the contents read

What does it display? Please post all output when describing a problem.

System.out.println(aoBuffer.toString());
What object is aoBuffer? What does its toString() method do?
What is in the file that you are trying to read?

Read the API doc for the String class to see how to make a String object out of an array of bytes.
laxmikant sabane
Greenhorn

Joined: Jun 04, 2008
Posts: 3
Hi,
thanks for reply.
I am trying to read a simple text file.
aoBuffer is the array of bytes having size 256.
When I try to display the output using System.out.println(), it gives the following output:

[B@addbf1
256
[B@42e816
256
[B@9304b1
-1
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
The output you've posted is generated by the Object class toString() method.
As I said before:

System.out.println(aoBuffer.toString());
What object is aoBuffer? What does its toString() method do?
What is in the file that you are trying to read?


You probably want to output String data vs what the Object class's toString() method returns. Look at the String class for how to convert an array of bytes to a String.
laxmikant sabane
Greenhorn

Joined: Jun 04, 2008
Posts: 3
Hi Norm,
Thanks for comment.
Thanks for your suggestion.
I got the answer in String API.
Thank you for your valuable help.
 
jQuery in Action, 2nd edition
 
subject: problem while dispalying from file
 
Similar Threads
input file and output file
Reading Data from a file and extracting the data
How can I use StringBuffer in this case...
File Not Found Error
problem with reading and writing toa files