• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Why is BufferedReader not reading, but clearing my text file?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Followed this: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/



In the constructor I create the BufferedReader using the FileReader and path to the input text file(in same dir as this project). When I am done reading I close it. In the debugger it is unable to read a single line, then goes to close the file.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the BufferedWriter which is clearing your file, not the reader.
 
peter pham
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:It's the BufferedWriter which is clearing your file, not the reader.



Thanks that was it.
 
Greenhorn
Posts: 11
1
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Java 7, you can use the try-with-resources statement around the BufferedReader so you don't have to close it explicitly.



Source: https://funnelgarden.com/java_read_file/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!