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 Reading in Text File and Outputting certain lines

  • 20-08-2010 10:36am
    #1
    Registered Users, Registered Users 2 Posts: 106 ✭✭


    Hi,

    I'm doing a bit of practice for programming and wondering how would I go about reading in a text file, processing it so that if a word eg. 'aerial' is on that line, the program will pass the whole line to another text file.

    Slightly modified, that it will pass the next strings to the text file and stop when it finds an int.

    TIA


Comments

  • Registered Users, Registered Users 2 Posts: 1,112 ✭✭✭Dacelonid


    You can use something like this to read the file
    StringBuffer text = new StringBuffer();
    Scanner scanner = new Scanner(new File(fFileName));
        try {
          while (scanner.hasNextLine()){
            text.append(scanner.nextLine());
          }
        }
    
    for example, and then manipulate text in anyway you need, ie only actually append to the StringBuffer if scanner.nextLine contains the string you need


  • Closed Accounts Posts: 845 ✭✭✭yupyup7up


    this here will show you how to read in the text file line by line using the BufferedReader class.

    and this is the string tokenizer class. this will basically parse a line of text into tokens (similiar to an array).

    I would personally use the buffered reader to read each line, then apply the stringtokenizer to each line from that and perform an indexOf call like so,

    haystack.indexOf(needle1);

    this will return whether the text you are looking for is a sub string of that token(taking into consideration that the string tokenizer doesnt cater for punctuation).

    for the second part, as the tokenizer doesnt recognise the integers, you could try parsing the string to an integer and if it succeeds then theres your integer.

    havent done java in ages but hope this helps!


  • Registered Users, Registered Users 2 Posts: 428 ✭✭Joneser


    candor wrote: »
    Hi,

    I'm doing a bit of practice for programming and wondering how would I go about reading in a text file, processing it so that if a word eg. 'aerial' is on that line, the program will pass the whole line to another text file.

    Slightly modified, that it will pass the next strings to the text file and stop when it finds an int.

    TIA

    The way I would go about this is to use a FileReader to read in each line of the file, then use the .contains method on that string to check if it contains your control word. If this returns true, then use a FileWriter write it to your new file.

    Using the part with the int, you will do the same as above, but first find the position of the int, and then write anything that comes before that index to a file.

    I just wrote this out quickly to give you an idea, if you want me to go more in depth let me know.

    Good luck


  • Registered Users, Registered Users 2 Posts: 106 ✭✭candor


    Joneser wrote: »
    The way I would go about this is to use a FileReader to read in each line of the file, then use the .contains method on that string to check if it contains your control word. If this returns true, then use a FileWriter write it to your new file.

    Using the part with the int, you will do the same as above, but first find the position of the int, and then write anything that comes before that index to a file.

    I just wrote this out quickly to give you an idea, if you want me to go more in depth let me know.

    Good luck

    Thanks for all your replies, all have been helpful.

    Any chance you could go into more depth?


Advertisement