Results 1 to 5 of 5
  1. #1
    luigi10011 is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Reading File for specific string

    The file i am reading is data saved from the Windows registry, so "UTF-16". It contains "empty/null" lines in which using BufferedReader and Scanner has been problematic for me. I am looking for a specific string inside the file, but it has been failing due to the "empty/null" lines in the file. This happens inside an actionEvent from a JButton. I've tested the JButton's actionEvent and it works.

    tl;drI am asking:
    • how to find and replace all empty/null lines in file
    • "Skip" over the empty lines and continue looking for the specific string
    • or to magically locate the string and return its line number

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Reading File for specific string

    Lines returned by a BufferedReader instance before the end of file won't be null, although they may be empty. An empty line can be treated like any other string that does not contain the string you are looking for: just increment the line count and move on.

    It might help to post some actual code that is not behaving as you intend. A couple of problem solving techniques are to separate the searching logic from the event handling logic and just post a simple example of the problem without the GUI clutter. Another is to base the BufferedReader on a hard coded string rather than an actual file: this helps others here see the problem without having the file in question.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Reading File for specific string

    Somehow this thread has ended up in a very out of the way place. I'll move it to n2j in the hope that it attracts more attention.

  4. #4
    luigi10011 is offline Member
    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Default Re: Reading File for specific string

    Quote Originally Posted by pbrockway2 View Post
    Lines returned by a BufferedReader instance before the end of file won't be null, although they may be empty. An empty line can be treated like any other string that does not contain the string you are looking for: just increment the line count and move on.

    It might help to post some actual code that is not behaving as you intend. A couple of problem solving techniques are to separate the searching logic from the event handling logic and just post a simple example of the problem without the GUI clutter. Another is to base the BufferedReader on a hard coded string rather than an actual file: this helps others here see the problem without having the file in question.
    Thank you very much! I completely forgot about isEmpty, just made a conditional and works

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Reading File for specific string

    You're welcome - I'm glad you've got it sorted out.

Similar Threads

  1. reading and writing string on notepad file
    By janroz in forum New To Java
    Replies: 5
    Last Post: 01-19-2012, 01:18 PM
  2. Reading a specific line from a file
    By efebatistaarda in forum New To Java
    Replies: 6
    Last Post: 02-22-2011, 03:57 PM
  3. Reading specific portions of text file
    By Plex in forum New To Java
    Replies: 1
    Last Post: 11-09-2010, 07:45 PM
  4. Reading data from csv file based on specific input
    By jaiminparikh in forum Advanced Java
    Replies: 14
    Last Post: 02-13-2009, 10:07 PM
  5. Replies: 2
    Last Post: 11-08-2008, 11:40 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •