This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

File reading

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to read file to html thru servlet.
BufferedReader buf=new BufferedReader(new InputStreamReader(new FileInputStream("d:/Java Web Server2.0/seminar/bio.txt")));
int i;
while((i=buf.read())!=-1){
String str=buf.readLine();
out.println(str+"
");
}
HOWEVER IT DOES NOT READ THE FIRST CHARACTER OF ANY LINE.
Like if the line is "hello" it prints "ello".
how to get the first letter?
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you're reading that first char with that read()
call - try:
String str;
while ((str = buf.readLine()) != null) {
...
}
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another option is to use Readers:
File file = new File("/dira/dirb/myfile.txt");
StringBuffer buf = new StringBuffer();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = reader.readLine();
while(line != null){
buf.append(line);
line = reader.readLine();
}
This will read every line of the file (and remove line break characters) into the string buffer.
 
Ranch Hand
Posts: 583
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi!
I just put the code together . it works..
Regds
Gautham Kasinath
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic