Results 1 to 4 of 4
  1. #1
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default reading a file problem

    I'm still learning java so this will likely be an obvious fix, sorry in advance :P

    So, I'm trying to load strings from a file into a string array. Then return that array. Here's my "attempt code":

    Java Code:
    public String [] loadFile (String fileName){
    		
    			int i = 0;
    			int j = 0; 
    		
    		
    			try{
    
    				FileInputStream fstream = new FileInputStream(fileName);
    			  
    				DataInputStream in = new DataInputStream(fstream);
    			  
    				BufferedReader br = new BufferedReader(new InputStreamReader(in));
    			  
    				while (br.readLine() != null)   {
    					i++;
    				}
    	
    				in.close();
    					}catch (Exception e){//Catch exception if any
    			    	System.err.println("Error: " + e.getMessage());
    					}
    			  
    					String sentences[] = new String [i];   
    		
    					try{
    
    						FileInputStream fstream = new FileInputStream(fileName);
    			  
    						DataInputStream in = new DataInputStream(fstream);
    			  
    						BufferedReader br = new BufferedReader(new InputStreamReader(in));
    			  
    						while ( br.readLine() != null)   {
    				  
    							String temp = "a";
    				  
    							temp = br.readLine();
    				  
    							sentences [j] = temp;
    				  
    							j++;
    						}
    	
    						in.close();
    					}catch (Exception e){//Catch exception if any
    						System.err.println("Error: " + e.getMessage());
    					}
    		    
    					return sentences;
    	}
    Here is the file I'm trying to read from (its content exactly):

    Alex left.

    It is written exactly like that but also includes an invisible "\n".

    Basically my problem is that the readLine method returns null.

    I ran a debugger and here is what stumps me, the while loop condition requires readLine() to be different from null. However when the Temp variable receives value of readLine() it is null


    Should you need it here is the code that saves data to the file:
    Java Code:
    public void SaveToFile (String sentence, String fileName){
    		
    		try {
    		    BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
    		    out.write(sentence);
    		    out.close();
    		} catch (IOException e) {
    		}
    	}
    What am I doing wrong?

    Thanks in advance :)

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by aianta View Post

    Java Code:
    			  
    						while ( [B]br.readLine() [/B]!= null)   {
    				  
    							String temp = "a";
    				  
    							[B]temp = br.readLine();[/B]
    				  
    							sentences [j] = temp;
    				  
    							j++;
    						}
    The bolded statements are the problem. You are advancing the buffer two lines for every iteration of the loop. You need to save the string that is returned by the readline() method within the stopping condition of your while loop

    Java Code:
    String line;
    while ( (line = br.readLine()) != null){
       //reference line variable instead of calling readLine() again
    }
    Last edited by yellowledbet; 08-03-2011 at 04:54 AM.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Also, it is inefficient to read the file twice. You should just read it once. Which raises the problem of how big to make the array. You have 2 options.

    1. Make a really big array that can fit all your data. This is also inefficient if you create an array to hold 100,000 Strings but only store 2 Strings.

    1.a) To avoid the above problem you can make a small array and "grow" it as needed. By this I mean create an array with a size or say 10. Then when it gets full and you haven't finished, make another larger array and copy all your data across.

    2. A better solution is to add the Strings to a List. Then when you have finished reading the file convert the List to an array.

  4. #4
    aianta is offline Member
    Join Date
    Apr 2011
    Posts
    63
    Rep Power
    0

    Default Thanks

    @yellowledbet

    Thanks, that was it. It works perfectly now! :)

    @Junky

    Thanks for the reply, I had a feeling I was doing some unnecessary steps there. Unfortunately I'm a complete Noob with Java (still learning the basics), I'll look up lists and see what I can do thanks for the hint!

Similar Threads

  1. Reading from file problem
    By BillyB in forum New To Java
    Replies: 10
    Last Post: 03-18-2011, 04:09 AM
  2. problem reading file
    By jmoutia in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 03:19 AM
  3. Problem reading from a file
    By sarapeace in forum New To Java
    Replies: 13
    Last Post: 10-03-2010, 02:08 PM
  4. Reg: File Reading Problem
    By balaji csc in forum New To Java
    Replies: 0
    Last Post: 11-06-2009, 04:22 PM
  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
  •