Using scanner, file not found

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 4747 Views - Last Post: 10 December 2012 - 11:27 AM Rate Topic: -----

#1 timharroun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 09

Using scanner, file not found

Posted 10 February 2011 - 08:49 PM

Hi. In the main method I'm using a scanner to get a file name from the user and then in the loader method I use a scanner to read in the file. Unfortunately I keep getting java.io.FileNotFoundException even though the path and filename are correct. It's probably something simple I'm overlooking but if someone could take a look and see what the problem might be that would be great. Here's the code that matters:

import java.io.*;
      import java.util.*;
        
    public class Main {
       
       //variable declaration
       public static final String defPath = "C:\\Isadirectory\\Workingdirectory";
       .......

       public static void main(String[] args) throws Exception
       {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the test file name: ");
        String infile = in.nextLine();
        File filename = new File(defPath, infile);
        filename.setReadOnly();
        System.out.println(filename);

	// Load the program

	long value = loader(filename);
        .....
       }



For the sake of argument, we'll say the filename the user enters is "isafile.exe."

The error occurs before the loader method, but here is part of it just for the sake of completeness:

public static long loader(File filename) throws Exception //Load executable program
    {// Load the program from the given filename into memory
        FileReader reader = new FileReader(filename);
        Scanner rin = new Scanner(reader);
	if (!rin.hasNext())
        {
            System.out.println("File open error.");
            return FileOpenError; //return file open error code
        }
        while (rin.hasNext())  //read file in line by line until end
        {
            // store program in memory
            addr = rin.nextLong();              //get address
            content = rin.nextLong();      //get content
            .....
     }



Here is the output (I'm using NetBeans):

C\Isadirectory\Workingdirectory\isafile.exe
Exception in thread "main" java.io.FileNotFoundException: C:\Isadirectory\Workingdirectory\isafile.exe (The system cannot find the file specified)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:106)
        at java.io.FileReader.<init>(FileReader.java:55)
        at Main.loader(Main.java:113)
        at Main.main(Main.java:68)
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)


If anybody has any ideas about what I'm doing wrong they will be appreciated.

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Using scanner, file not found

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using scanner, file not found

Posted 10 February 2011 - 08:59 PM

Not sure Scanner.hasNext() would work with a .exe file. Does not make sense will try to find a " " or a <LF> on the .exe

A .exe is just a bunch of bytes, do not use a Scanner to read it
Just used a FileInputStream and the read() method to read each byte
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using scanner, file not found

Posted 10 February 2011 - 09:05 PM

Try

System.out.println(filename + " exists ?: " + filename.exists());

to see if the file exists
Was This Post Helpful? 0
  • +
  • -

#4 timharroun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 09

Re: Using scanner, file not found

Posted 10 February 2011 - 09:09 PM

That came out false as I expected, but the file is there and the path is correct. That's what is frustrating me about this.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using scanner, file not found

Posted 10 February 2011 - 09:13 PM

If your code is on the C: drive remove C: from your path
There is something tricky about disk drive in file path
I'll try to find a pointer
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Using scanner, file not found

Posted 10 February 2011 - 09:14 PM

Try using a single / instead of the two \\ to separate your directories in the path.
Was This Post Helpful? 0
  • +
  • -

#7 timharroun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 09

Re: Using scanner, file not found

Posted 10 February 2011 - 09:18 PM

View Postpbl, on 10 February 2011 - 08:59 PM, said:

Not sure Scanner.hasNext() would work with a .exe file. Does not make sense will try to find a " " or a <LF> on the .exe

A .exe is just a bunch of bytes, do not use a Scanner to read it
Just used a FileInputStream and the read() method to read each byte


It's not really an .exe, it's a text file. I change it to .txt and I get the same problem.

View Postpbl, on 10 February 2011 - 09:13 PM, said:

If your code is on the C: drive remove C: from your path
There is something tricky about disk drive in file path
I'll try to find a pointer


Sigh. This doesn't work...
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Using scanner, file not found

Posted 10 February 2011 - 09:18 PM

You could also use JFileChooser to allow you to visually locate the File. Then you can use the File getAbsolutePath() method to help display the path of the File as you want to use it.
Was This Post Helpful? 0
  • +
  • -

#9 timharroun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 09

Re: Using scanner, file not found

Posted 10 February 2011 - 09:19 PM

View Postmacosxnerd101, on 10 February 2011 - 09:14 PM, said:

Try using a single / instead of the two \\ to separate your directories in the path.


...and this didn't work, either.
Was This Post Helpful? 0
  • +
  • -

#10 timharroun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 09

Re: Using scanner, file not found

Posted 10 February 2011 - 09:25 PM

View Postmacosxnerd101, on 10 February 2011 - 09:18 PM, said:

You could also use JFileChooser to allow you to visually locate the File. Then you can use the File getAbsolutePath() method to help display the path of the File as you want to use it.


Yeah, I'll probably end up doing that, but I think for this assignment I'm not really supposed to. So if I can't figure out this problem I'll probably just fall back to that and take any grade reduction.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Using scanner, file not found

Posted 10 February 2011 - 09:26 PM

I'm saying for testing purposes use that method to get you on the right track. Then use the path and go back to the way you were supposed to.
Was This Post Helpful? 1
  • +
  • -

#12 timharroun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 30-December 09

Re: Using scanner, file not found

Posted 10 February 2011 - 09:58 PM

View Postmacosxnerd101, on 10 February 2011 - 09:26 PM, said:

I'm saying for testing purposes use that method to get you on the right track. Then use the path and go back to the way you were supposed to.


[ROLLS EYES] Yep, that helped me figure it out. Stupidly I was looking at the filename in Windows Explorer and it didn't show the file with the extension, so I added the extension, but that meant the filename was actually file.txt.txt, so of course the program couldn't find it.

Thanks for the assist. Sorry to waste everybody's time for something so silly.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10387
  • View blog
  • Posts: 38,438
  • Joined: 27-December 08

Re: Using scanner, file not found

Posted 10 February 2011 - 09:59 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using scanner, file not found

Posted 10 February 2011 - 10:05 PM

We should have asked for the image of the directory at first
We will never learn :(
Was This Post Helpful? 0
  • +
  • -

#15 tedcrow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-December 12

Re: Using scanner, file not found

Posted 10 December 2012 - 11:16 AM

Hi! Try this with your parameters (filename, path to file, etc.). Put your Scanner-code in try-catch. I hope it will work for you. Have a nice day!

	public static void main(String[] args) {
		// Read the file and build the hash-table of courses
		HashMap<String, ArrayList<Student>> courses =
		new HashMap<String, ArrayList<Student>>();
		
		System.out.println("--- Start Debug ---");
		System.out.println(new File("Students.txt").getAbsolutePath());
		System.out.println("--- End Debug ---");
		
		//File f = new File("C:/Java/Eclipse/workspace/introToJava2/Students.txt");
		File f = new File("C:/Java/Students.txt");
		
		System.out.println(f.getName() + " exists ?: " + f.exists());
		
		try {
			Scanner input = new Scanner(f);
			while (input.hasNext()) {
				String line = input.nextLine();
				String[] studentEntry = line.split("\\s*\\|\\s*");
				String firstName = studentEntry[0];
				String lastName = studentEntry[1];
				String course = studentEntry[2];
				ArrayList<Student> students = courses.get(course);
				if (students == null) {
				// New course -> create a list of students for it
				students = new ArrayList<Student>();
				courses.put(course, students);
				}
				Student student = new Student(firstName, lastName);
				students.add(student);
				}
			// Print the courses and their students
			Set<String> coursesNames = courses.keySet();
			for (String course : coursesNames) {
			System.out.println("Course " + course + ":");
			ArrayList<Student> students = courses.get(course);
			Student[] studentsArr =
			students.toArray(new Student[students.size()]);
			Arrays.sort(studentsArr);
			for (Student student : studentsArr) {
			System.out.printf("\t%s\n", student);
			}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	} 

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2