Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java help - Reading from a text file

Options
  • 13-11-2006 6:13pm
    #1
    Registered Users Posts: 762 ✭✭✭


    Hi, im new to java and i need to write a program to read from a text file, then to go through each line one by one and select the line that satisify's a certain condition.

    Any help appricated.

    Thanks


Comments

  • Registered Users Posts: 1,636 ✭✭✭henbane




  • Registered Users Posts: 378 ✭✭sicruise


    Throw the man a bone :o)

    Here you can use this here... I just wrote it for you there quickly so it probably isn't the best code in the world but it will give you an idea.
    import java.io.*;
    
    public class ReadFromFile{
    
    
               public static void main(String a[]) {
    
    
                   //Read in the string from request file line by line
    
                   try
    
                   {
    
                   		String str="";
    
                  		String reader="";
    
                  		FileReader fr = new FileReader(a[0]);
    
                 		BufferedReader br = new BufferedReader(fr);
    
                  		while ((reader = br.readLine()) != null)
    			{
    				str += reader + "\n";
    			}
    
                   		System.out.println(str);
    
    			br.close();
    		}
    
                   //Catch exceptions
    
    		catch (FileNotFoundException fnfe)
    		{
    
    			System.out.println(fnfe.getMessage());
    
                   	}
    
    		catch (IOException e)
    	       	{
    
                    	System.out.println(e.getMessage());
    
               	}
    		catch (ArrayIndexOutOfBoundsException e)
    		{
    
    			System.out.println("Usage: java ReadFromFile filename");
    
    		}
    
    
    
               }
    
    }
    


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    For all the examples you will ever need...

    http://www.java2s.com/Code/Java/CatalogJava.htm

    Note: Copying anything there verbatim will almost likely get you 100% fail in a test.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Ahem :rolleyes: tut tut *wags finger*


  • Registered Users Posts: 378 ✭✭sicruise


    Yea I know... I was just bored and felt sorry for him ;)


  • Advertisement
Advertisement