• 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?

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I am working on a project to read the content of all the files in one directory and put them into one new java file. I am using readline() to read the content of the file. But when I encounter some text like /**, it return null. Anyone does the samething?
/** My code **/
if(aFile.isFile())
{
try
{
RandomAccessFile r = new RandomAccessFile(sPath + "\\" + oneFile, "r");
StringBuffer buf = new StringBuffer();
String text;
while((text = r.readLine() ) != null)
{
buf.append(r.readLine() + "\n");
System.out.println(r.readLine());
}
output.writeBytes(buf.toString() + "\n");
}
catch (IOException e)
{
System.err.println(e.toString());
System.exit(1);
}
}
Thanks!
qionghua
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
Don't use RandomAccessFile to perform your work. Use BufferedReader it is designed to be used very efficiently with characters and strings. It also supplies a readLine method.
Regards,
Manfred.
 
qionghua yang
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware of the readLine method of the BufferedReader class. This excerpt comes from Elliotte Rusty Harold's book on Java Network Programming p.107:
"Unfortunately, this method shares the same bugs as the readLine() method in DataInputStream, discussed before. That is, it will tend to hang its thread when reading streams where lines end in carriage returns, such as is commonly the case when the streams derive from a Macintosh or a Macintosh text file. Consequently, you should scrupulously avoid this method in network programs."
I hope that helps with your project.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic