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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java help - Reading from a text file

  • 13-11-2006 5: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