6 Replies Latest reply: May 6, 2010 2:11 PM by 843789 RSS

    why cant i find fine my file??? using scanner

    843789
      My Main Class
      import java.io.File;
      
      
      public class MyScannerTesterClass {
           public static void main(String[] args){ 
                File f = new File("file1.txt");
                MyScanner testScanner = new MyScanner(f); 
                testScanner.loopThroughFile(); 
           }
      }
      for some reason im getting this

      java.io.FileNotFoundException: file1.txt (No such file or directory)
           at java.io.FileInputStream.open(Native Method)
           at java.io.FileInputStream.<init>(FileInputStream.java:106)
           at java.io.FileReader.<init>(FileReader.java:55)
           at MyScanner.<init>(MyScanner.java:13)
           at MyScannerTesterClass.main(MyScannerTesterClass.java:7)
      Exception in thread "main" java.lang.NullPointerException
           at MyScanner.loopThroughFile(MyScanner.java:23)
           at MyScannerTesterClass.main(MyScannerTesterClass.java:8)

      i created a file called file1.txt and it is in the src folder with the other classes. im not sure why its not detecting the file?





      My Other Class
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileReader;
      import java.util.Scanner;
      
      
      public class MyScanner {
           Scanner scanner;
           
           MyScanner(File f){ 
                
                try {
                     scanner = new Scanner(new FileReader(f));
                } 
                catch (FileNotFoundException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                } 
                 
           }
           
           public void loopThroughFile(){ 
                while(scanner.hasNext())
                { 
                     String line = scanner.nextLine(); 
                     System.out.println(line); 
                     
                }
           }
           
      }
      Edited by: camboguy on May 5, 2010 10:40 PM
        • 1. Re: why cant i find fine my file??? using scanner
          hsc71
          Use a pathname string for the file.
          Read [the api|http://java.sun.com/javase/7/docs/api/] to understand.
          • 2. Re: why cant i find fine my file??? using scanner
            843789
            I read the API about the File class but i still didn't understand it, im really new to programming and so it sounded really confusing. My professor just put in the file name and it worked when he showed us, he was using windows, im using ubuntu. Could that be the problem?
            • 3. Re: why cant i find fine my file??? using scanner
              hsc71
              camboguy wrote:
              I read the API about the File class but i still didn't understand it, im really new to programming and so it sounded really confusing. My professor just put in the file name and it worked when he showed us, he was using windows, im using ubuntu. Could that be the problem?
              Where did he put the file? Just give the absolute pathname (the driveletter, any directorys and filename). A File is not a "thing" but a location and a name
              • 4. Re: why cant i find fine my file??? using scanner
                843789
                i did this
                import java.io.*;
                
                
                public class MyScannerTesterClass {
                     public static void main(String[] args){ 
                          File f = new File("H:\WorkSpace Main\ICS22 Workspace\workspace\workspace\Practice\src\file1.txt"); 
                          MyScanner testScanner = new MyScanner(f); 
                          testScanner.loopThroughFile(); 
                     }
                }
                but i got the error

                Exception in thread "main" java.lang.Error: Unresolved compilation problem:
                     Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

                     at MyScannerTesterClass.main(MyScannerTesterClass.java:6)

                ?? i tried putting the absolute path name but it still doesn't work. ??
                • 5. Re: why cant i find fine my file??? using scanner
                  843789
                  Okay so i got it to work, but im not sure why. This is what i did when it was wrong i made a file and named it

                  somefile.txt

                  that didn't work
                  then i made a untitled text file and named it

                  somefile2.txt

                  and this worked??

                  what is the difference??? they both have the extention .txt what would make this different. (*note i am using eclipse classic version 3.5.2)
                  • 6. Re: why cant i find fine my file??? using scanner
                    843789
                    camboguy wrote:
                    i did this
                    File f = new File("H:\WorkSpace Main\ICS22 Workspace\workspace\workspace\Practice\src\file1.txt"); 
                    Try using forward slashes instead of backslashes. The backslash character is an escape character in Java. Not to mention, if you are using *nix flavor OS, you should be using forward slashes in your path names anyhow.  If you are hell-bent on using backslashes, then you need to double them up, since '\\' gives you '\' in a Java String.