Results 1 to 6 of 6
Thread: Java Read File Code
- 08-07-2011, 12:35 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 7
- Rep Power
- 0
Java Read File Code
I am a trying to write a program to read from a text file that is continuously getting longer. I wrote it in C++ originally and it works fine. Now I am trying to write the same program in Java. Since I am a beginner at java I was wondering if someone could help me "translate" my code. I was not sure if this is the right place to ask this question, if I have made a mistake please advise me on where to put this question.
Here is my code in C++:
Java Code://reading a text file #include <iostream> #include <fstream> #include <string> using namespace std; int main () { char line[81]; streamsize ssize; streamsize len=80; ifstream myfile ("test.txt",ifstream::in); if (myfile.good()) { while ( myfile.good() ) { memset(line,0,len); ssize=myfile.readsome(line,len) //getline (myfile,line); if (ssize>0) { cout.write (line, ssize); } } myfile.close(); } else { cout << "Unable to open file"; } return 0; }
- 08-07-2011, 12:42 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Check out these tutorials - Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)
A quick scan of that tutorial should help you understand how to write/read to a file in java.
- 08-07-2011, 12:48 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 7
- Rep Power
- 0
I understand how to read and write a file in java. The problem is that the text file that I am dealing with is continuously getting larger. Since I am still new to java, I am having difficulty handling this aspect of I/O streams.
- 08-07-2011, 12:50 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The common idiom would be
This will continue reading a file until it is finished, so if the file increases infinitely, it will just keep reading infinitely.Java Code:String line; while((line=reader.readLine())!=null)
- 08-07-2011, 01:00 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 7
- Rep Power
- 0
This would work if the text file was being updated at a quicker rate than the reader (where the reader would never reach the end of file).
However, in my situation, the reader will often reach the end of the file, the file may then be updated, where the reader should pickup and continue reading. According to the while condition that you suggested, the loop would end after it caught up the end of the text file, even though the file was still being added to.
Any ideas on how to fix this?
- 08-07-2011, 01:04 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Read unicode code points from file to real characters
By lordelf2004 in forum Advanced JavaReplies: 1Last Post: 07-30-2011, 11:37 AM -
Read .doc file in java
By jatinkansagara in forum Advanced JavaReplies: 1Last Post: 06-27-2011, 03:48 PM -
how do i read a clob data from a function written in oracle in java code
By renu in forum New To JavaReplies: 1Last Post: 03-29-2011, 08:59 AM -
help to read .pst file through java
By umadas in forum Advanced JavaReplies: 2Last Post: 12-19-2010, 04:32 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks