Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java help - Reading from a text file

  • 13-11-2006 06:13PM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 Posts: 1,636 ✭✭✭henbane




  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Ahem :rolleyes: tut tut *wags finger*


  • Registered Users, Registered Users 2 Posts: 378 ✭✭sicruise


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


  • Advertisement
Advertisement