Results 1 to 6 of 6
  1. #1
    joshhazel is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Reading text file and ouput to screen problem

    I get no errors reading or printing to screen, but when I print the data out it stops printing partway though the file, stopping in the middle of line 618 of text file (sample file attached) which is approximately 22000 characters. There are several thousand lines of data but it always stops printing out at the same spot.

    My only guess is there is some limitation on how much of the file can be read into a scanner? How can I fix this?


    My code:
    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    public class ConvertText {
    		
    	public static void main(String[] args) {
    		
    	try {
    		Scanner fin = new Scanner(new File("C:\\database6.txt"));
    
    		String line = "";
    		while(fin.hasNextLine()) {
    			line = fin.nextLine();
    			System.out.println(line);	
    		}//end while
    	}
    	catch(Exception e) {
    		System.out.println("Error: " + e.getMessage());	
    
    	}	//end try catch
    	}//end main
    	
    }//end class
    Attached Files Attached Files

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Reading text file and ouput to screen problem

    use the File class and FileReader.
    Something like File f1 = new File("target.txt");
    FileReader in = new FileReader(f1);
    Then run on the whole file with the reader() method.

  3. #3
    joshhazel is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default Re: Reading text file and ouput to screen problem

    Thanks that worked... code below for others...

    Btw, why does this occur with Scanner? Ive used FileWriter but never used FileReader...
    Java Code:
    import java.io.File;
    import java.io.FileReader;
    import java.io.BufferedReader;
    
    public class ConvertText {
    		
    	public static void main(String[] args) {
    		
    	try {
    		FileReader fr = new FileReader("C:\\database6.txt"); 
    		BufferedReader br = new BufferedReader(fr); 
    		String s; 
    		while((s = br.readLine()) != null) { 
    			System.out.println(s); 
    		} 
    		fr.close(); 
    	}
    	catch(Exception e) {
    		System.out.println("Error: " + e.getMessage());	
    	}	//end try catch
    	}//end main
    	
    }//end class

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Reading text file and ouput to screen problem

    I never used Scanner to read from a file.
    But I do know how it should be and I can't find any mistake in your code.
    I hope someone can find whats wrong and let us know.

  5. #5
    dheaven is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: Reading text file and ouput to screen problem


  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: Reading text file and ouput to screen problem

    It's an encoding problem: your default decoder doesn't match the encoding of the file.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. problem with reading a text file with special char
    By kishan.java in forum New To Java
    Replies: 1
    Last Post: 04-10-2011, 09:30 AM
  3. Replies: 5
    Last Post: 03-12-2011, 01:04 AM
  4. Creating Ouput Text File
    By Raian in forum New To Java
    Replies: 14
    Last Post: 05-24-2010, 09:56 AM
  5. Problem with reading text from a .txt file
    By Gigi in forum New To Java
    Replies: 40
    Last Post: 01-22-2009, 03:22 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
  •