• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

problem while dispalying from file

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Norm,
Thanks for comment.
Thanks for your suggestion.
I got the answer in String API.
Thank you for your valuable help.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic