Results 1 to 15 of 15
  1. #1
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default Having trouble opening and reading a txt file

    Hello, I have to do a project where I have to be able to read a text file and then be able to search by characters and replace them with other characters. I want to start off small, so I am trying to create a small program that just opens files and reads them, due to my inexperience with IO, I came to an error and is stuck with it.

    What I did, in Netbeans I created a blank file and then wrote on it couple sentances and then named the file someFile.txt. This file is also located in the same package where I made my class called SearchFile, and it goes like this:

    ackage searchfile;
    import java.io.*;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.io.Reader;

    /**
    *
    * @author Dennis
    */
    public class SearchFile {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws IOException {

    String fileName = "someFile.txt";
    Scanner someFile = null;
    System.out.println("The file " + fileName + " contains the following lines:");

    try {
    someFile = new Scanner(new File(fileName));
    }
    catch (FileNotFoundException e) {
    System.out.println("Error opening the file: " + fileName);
    System.exit(0);
    }
    while (someFile.hasNextLine()) {
    String line = someFile.nextLine();
    System.out.println(line);
    }
    someFile.close();

    }

    }

    Everytime I ran the program, it failed to open the file. Can anyone here help me please? Perhaps I made a syntax error somewhere...

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Define "it failed"....does it throw an exception?

  3. #3
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Yes, it throws FileNotFoundException e, and the string "Error opening the file" pops up as the result.

  4. #4
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    Stop using netbeans for a while, and remove the "package" line for this time. Then run your program through the command prompt. make sure you have someFile.txt in the directory where you run your program.

  5. #5
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    When you just put "someFile.txt" it means that this file is place same with the path of your project. Try to put the absolute path temporarily to test if it will run. If it runs with the absolute file it means the someFile.txt is not located same as your project's location.

  6. #6
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by JavaHater View Post
    Stop using netbeans for a while, and remove the "package" line for this time. Then run your program through the command prompt. make sure you have someFile.txt in the directory where you run your program.
    Unfortunatly, I'm not very familliear with running the program through command prompt, I'm used to Netbeans.
    Quote Originally Posted by mine0926 View Post
    When you just put "someFile.txt" it means that this file is place same with the path of your project. Try to put the absolute path temporarily to test if it will run. If it runs with the absolute file it means the someFile.txt is not located same as your project's location.
    What do you mean by absosulute path? Do you want me to implement it as "absolute" instead of "someFile" or is that somewhere in the settings?

    What I did is I created a file right under SearchFile.java, they are both located in the source package, or at least thats what it seems to me lol.
    Last edited by DBaskov; 01-29-2011 at 01:13 AM.

  7. #7
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    The complete path.
    D:\MyProjectFolder\someFile.txt

    And in java you have to us double slash or backslash so the complete path will look like this
    Java Code:
    String fileName = "D:\\MyProjectFolder\\someFile.txt"
    //[b]OR[/b]
    String fileName = "D:/MyProjectFolder/someFile.txt"
    GoodLuck!

  8. #8
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by mine0926 View Post
    The complete path.
    D:\MyProjectFolder\someFile.txt

    And in java you have to us double slash or backslash so the complete path will look like this
    Java Code:
    String fileName = "D:\\MyProjectFolder\\someFile.txt"
    //[b]OR[/b]
    String fileName = "D:/MyProjectFolder/someFile.txt"
    GoodLuck!
    Well I tryed typing in file's location like in your example and still same error. Wow, this is a lot frustrating than I though it ws going to be...

    The file's location is C:/Users/Dennis/Music/array2/Search File/srcsearchfile , however I also added /someFile.txt to it to make it specific.
    Last edited by DBaskov; 01-29-2011 at 01:30 AM.

  9. #9
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Same error? As I remember your error a while ago is FileNotFoundException.
    FileNotFoundException is thrown when file does not exist or it is a read-only file.
    Check if
    1. the path you typed is correct, just to make sure.
    2. your file is read-only.

    Is search file being accessed from another class?

  10. #10
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    The location is correct because I copied and pasted it from properties, the only differance is that I added /someFile.txt to it as I mentioned earlier because without that I don't think it would find it.

    I'm not sure what exactly makes the file read only, but I can ask Netbeans by right click and "open in the system" and the file pops out in the notepad window.

    You know how when you compile a project, the sometimes there would be several files in some location, well thats how my text file and SearchFile class are. What I did is, I created the class, and then I right clicked on the class and said new blank file, and then I filled it and named it someFile.txt

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I guess we have to check it at runtime using File class exist() and canRead() method.
    I add some on your code.
    Java Code:
            try
            {
                [b]
                File myFile = new File(fileName);
                //If file does not exist it will print
                if(! myFile.exists())
                {
                    System.out.println("File does not exist!\n should not continue");
                }
                
                //If file cannot read it will print a message
                if(! myFile.canRead())
                {
                    System.out.println("File exist but cannot read!\n "
                            + "should not continue either.");
                }
                [/b]
                someFile = new Scanner(myFile); [b][i]//!Notice that I use myFile instead[/i][/b]
            }

  12. #12
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Ok I tried it but I took out "try" because there is no catch statement, and it gave me bunch of errors and exceptions. It also printed both "file not found" and "file find but cannot read" strings. lol this is a big mess...

  13. #13
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    No, do not remove catch. I just only show what I change, and it happens that the this I change are only at try statement.
    To make it clear, I repost the code and what I changed
    Java Code:
    package searchfile;
    
     import java.io.*;
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.io.Reader;
    
    public class SearchFile
    {
        public static void main(String[] args) throws IOException
        {
            String fileName = "someFile.txt";
            Scanner someFile = null;
            System.out.println("The file " + fileName + " contains the following lines:");
    
            try
            {
                //[b]
                File myFile = new File(fileName);
                //If file does not exist it will print
                if(! myFile.exists())
                {
                    System.out.println("File does not exist!\n should not continue");
                }
    
                //If file cannot read it will print a message
                if(! myFile.canRead())
                {
                    System.out.println("File exist but cannot read!\n "
                            + "should not continue either.");
                }
                //[/b]
                someFile = new Scanner(myFile); //[b][i]!Notice that I use myFile instead[/i][/b]
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
                System.out.println("Error opening the file: " + fileName);
                System.exit(0);
            }
            while (someFile.hasNextLine())
            {
                String line = someFile.nextLine();
                System.out.println(line);
            }
            someFile.close();
    
        }
    }

  14. #14
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Hmm, well I edited it and it threw out those to strings out again; saying that it didn't find a fail and another one saying it did but can't read and I also got an exception saying that file is not found in the system. So I guess I location is false, but idk what to do. Maybe I should relocated the file to a more simple location...

  15. #15
    DBaskov is offline Member
    Join Date
    Jan 2011
    Posts
    24
    Rep Power
    0

    Default

    Wow and it worked, phew. I relocated it to a simpler place and I guess I pasted the wrong thing. I shouldn't have copied the location, I should have copied object name in properties because it displays the location and the text's name. So I guess it works now lol.

    Thanks for the help everyone, especially Mine, you're statement worked this time. I almost lost my cool over this little thing.
    Last edited by DBaskov; 01-29-2011 at 03:00 AM.

Similar Threads

  1. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 01:11 AM
  2. Help opening and reading a file
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 09-21-2010, 07:32 AM
  3. Opening mp3 file ( not reading )
    By Char in forum New To Java
    Replies: 15
    Last Post: 08-29-2010, 07:09 AM
  4. Having trouble reading external txt file to an array
    By Metastar in forum New To Java
    Replies: 18
    Last Post: 07-21-2010, 11:29 PM
  5. Problem in opening a file
    By Raghav kv in forum New To Java
    Replies: 1
    Last Post: 08-11-2007, 01:55 PM

Posting Permissions

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