Program isn't reading my .txt file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Program isn't reading my .txt file

  1. #1
    Join Date
    Jan 2013
    Location
    Chicago
    Posts
    3

    Program isn't reading my .txt file

    Greeting,
    Iím using netbeans on a MacBook. Iíve written a small program that reads a text file. However, the program isnít reading the file. Iím receiving my FileNotFoundExepection error message. Where can I place my out.txt file so that the program is able to locate the file in this simple code below.

    public class TextFileInputDemo {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    String fileName = "out.txt";
    Scanner inputStream = null;
    System.out.println("The file " + fileName +
    "\ncontains the following lines:\n");
    try
    {
    inputStream = new Scanner(new File(fileName));
    }
    catch (FileNotFoundException e)
    {
    System.out.println("Error opening the file " + fileName);
    System.exit(0);
    }
    while (inputStream.hasNextLine());
    {
    String line = inputStream.nextLine();
    System.out.println(line);
    }
    inputStream.close();

    }
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Program isn't reading my .txt file

    You either need to use a fully qualified path or put it in the current working directory. There is probably a netbeans setting specifying where this is or you can print out the system property "user.dir" or you can print out the absolute path of the File object you are creating in you program and that will show where it expects out.txt to be.ie
    Code:
    System.out.println(new File("out.txt").getAbsolutePath());
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2013
    Location
    Chicago
    Posts
    3

    Re: Program isn't reading my .txt file

    Thanks keang, although your method of code didn't work for me, the language in your explanation helped me to further my online research and I can up with this solution:

    Click on File menu.
    Click on Project Properties.
    In the categories, select Run.
    In main class you select your current java file.
    In Arguments select the file you want to read for e.g. abc.txt or abc.java
    And in Working Directory write down the path of folder in which this abc.txt or abc.java lies.
    Click OK to close Project Properties.
    While running your program don't forget to select your project as Main Project.
    Then click F^ on keyboard. i.e. You have to raun your main project instead of just running your current java file. That's it....enjoy!!!!

    It is probably your same solution, but until I start understanding the concepts of Java, I'll continue fit a square peg into this round hole...lol. Again Thanks.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Program isn't reading my .txt file

    Glad to hear you have sorted it.
    The line of code I provided was for insertion into your program at the start of the main method. It will print out the fully qualified path for the directory = "/out.txt". This is where it expects the file to be.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Jan 2013
    Location
    Chicago
    Posts
    3

    Re: Program isn't reading my .txt file

    Wow, I inserted that piece of code everywhere except where you just mentioned. Now I've learned two different methods of coding in this forum in less than 24 hours. This site is a blessing.

    Thanks keang...once again.
    Hey, how long have you been coding Java?

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,474

    Re: Program isn't reading my .txt file

    I've been coding in Java pretty much since it first came out in 1996 but was writing code in other languages for about 16 years before that.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center