Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

File reading

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
hi!
I just put the code together . it works..
Regds
Gautham Kasinath
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic