Results 1 to 6 of 6
  1. #1
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Unhappy 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;
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The common idiom would be

    Java Code:
    String line;
    while((line=reader.readLine())!=null)
    This will continue reading a file until it is finished, so if the file increases infinitely, it will just keep reading infinitely.

  5. #5
    xan.amini is offline Member
    Join Date
    Aug 2011
    Posts
    7
    Rep Power
    0

    Unhappy

    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?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. Read unicode code points from file to real characters
    By lordelf2004 in forum Advanced Java
    Replies: 1
    Last Post: 07-30-2011, 12:37 PM
  2. Read .doc file in java
    By jatinkansagara in forum Advanced Java
    Replies: 1
    Last Post: 06-27-2011, 04:48 PM
  3. Replies: 1
    Last Post: 03-29-2011, 09:59 AM
  4. help to read .pst file through java
    By umadas in forum Advanced Java
    Replies: 2
    Last Post: 12-19-2010, 05:32 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •