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

Options
  • 20-08-2010 11:36am
    #1
    Registered Users 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 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 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 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