Results 1 to 8 of 8

Thread: Reading files

  1. #1
    ptuckley is offline Member
    Join Date
    Dec 2010
    Posts
    11
    Rep Power
    0

    Default Reading files

    So here we go, hope this is an alright place to post this.

    It's very much a personal project, just something I've set myself to try and further my knowledge of java. I only started studying it this year so I'm pretty useless so far.

    Basically, the end aim is to develop a piece of code that will go through a file, copy all the lines that begin with a number and then insert them into a new file.

    I've started off with a piece of code that will read through a file and display the letter "Y" when the line begins with an 'A' and display the letter "N" when it begins with anything else. The only way I know how to go through an entire file is to use:

    while(MyFile.readLine()!=null)

    However, I want it to be able to process lines that have nothing on them.

    The code I've got so far is:

    import java.io.*;

    class test1
    {
    public static void main(String[] args) throws IOException
    {
    FileReader file = new FileReader("body.txt");
    BufferedReader MyFile = new BufferedReader(file);

    String text = MyFile.readLine();

    while(text!=null)
    {
    if(text.charAt(0)=='A')
    {
    System.out.println("Y");
    }

    else
    {
    System.out.println("N");
    }
    text = MyFile.readLine();
    }

    MyFile.close();
    }
    }
    body.txt is simply
    A
    B
    B
    A
    B
    B
    A
    A
    A
    However, if there is an empty line in the middle of body.txt, it obviously won't work. Any help would be greatly appreciated!

    Many thanks.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    340
    Rep Power
    5

    Default

    I actually think what you have should work. No reason why a blank line should stop the loop. It will read the entire file. So for instance, if you have a file with 7 lines:
    A
    B
    C

    D
    E
    F

    It will evaluate all the way to the F. Heres a sample of the File reader, but it's very similar to what you have. What output are you seeing?

    Java Code:
          try{
      // Open the file that is the first 
      // command line parameter
      FileInputStream fstream = new FileInputStream("testing.txt");
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
      System.out.println(strLine);
      }
      //Close the input stream
      in.close();
        }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      }
    That should produce the following output:

    Java Code:
    A
    B
    C
    
    D
    E
    F

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    it obviously won't work
    Please explain the reason it won't work.
    I want it to be able to process lines that have nothing on them.
    You can test the contents of the String that you read into to see if it is empty. See the String API for methods to use.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    It won't work because a blank line does not have a char at index 0. Op will need to add a separate test for a blank line, such as testing the length of the String read in.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,556
    Rep Power
    23

    Default

    Yes, I saw that. I was wondering if that was the reason for the message the OP posted. You never know what a student thinks his code does or does not do. So I ask to be sure we are on the same page.
    You'll note I recommended that the String be tested to see if it is empty which I thought would be a solution for the OPs problem.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Oops. I really have to read entire posts. ;)

  7. #7
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    The blank line will result in a zero length String so charAt( 0 ) will bomb.

    I would test to see if the length of the text string is greater than 0 before seeing what the first character is.

    Refer to the String class.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Another pointless post. Why do you bother when the problem has already been identified and a solution provided

Similar Threads

  1. Reading from .txt files
    By rileannas in forum Java Applets
    Replies: 4
    Last Post: 05-10-2011, 05:16 PM
  2. Bug Reading txt files
    By MHardeman25 in forum New To Java
    Replies: 4
    Last Post: 08-13-2010, 10:03 PM
  3. Reading .bin files
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-22-2010, 04:39 PM
  4. Reading .txt files
    By cvcs1 in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:07 AM

Posting Permissions

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