Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Beginning Java
Why is BufferedReader not reading, but clearing my text file?
peter pham
Greenhorn
Posts: 12
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Followed this:
http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
public static final String FILENAME="BinarySearchTree.txt"; .... public BinarySearchTree(TNode r) throws IOException { root = r; textfile = new File(FILENAME); fw = new FileWriter(textfile.getAbsoluteFile()); bw = new BufferedWriter(fw); br = new BufferedReader(new FileReader(FILENAME)); } public void createFromFile() throws IOException { String currLine; // read a line. while there is one... while((currLine = br.readLine()) != null) { StringTokenizer st = new StringTokenizer(currLine, DELIMITER); while(st.hasMoreTokens()) { String currValue = st.nextToken(); TNode newNode = new TNode(currValue); if(root == null) { root = newNode; } else { insert(root, newNode); } } } br.close(); }
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.
Paul Clapham
Marshal
Posts: 28424
102
I like...
posted 11 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
It's the BufferedWriter which is clearing your file, not the reader.
peter pham
Greenhorn
Posts: 12
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Paul Clapham wrote:
It's the BufferedWriter which is clearing your file, not the reader.
Thanks that was it.
Daniel Baker
Greenhorn
Posts: 11
1
I like...
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Since
Java
7, you can use the try-with-resources statement around the BufferedReader so you don't have to close it explicitly.
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFile_BufferedFileReader_ReadLine { public static void main(String [] args) throws IOException { String fileName = "c:\\temp\\2.sample-10KB.txt"; FileReader fileReader = new FileReader(fileName); try (BufferedReader bufferedReader = new BufferedReader(fileReader)) { String line; while((line = bufferedReader.readLine()) != null) { System.out.println(line); } } } }
Source:
https://funnelgarden.com/java_read_file/
Don't get me started about those stupid
light bulbs
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
File I/O
BufferedReader & StringTokenizer
FileIO and mult. arrays
Inputting data from a csv file to create a data array
More...