java.io.FileNotFoundException: Temp (Access is denied)

1.5k Views Asked by At

I have no idea why this is happening. The purpose of my code is to compare two files, and I got it from my programming book. This is my code:

import java.io.*;

class CompFiles {
public static void main(String args[])
{
    int i=0, j=0;

// First make sure that both files have been specified.
if(args.length !=2 ) {
  System.out.println("Usage: CompFiles f1 f2");
  return;
}

// Compare the files.
try
    {
        FileInputStream f1 = new FileInputStream(args[0]);
         FileInputStream f2 = new FileInputStream(args[1]);

  // Check the contents of each file.
  do {
    i = f1.read();
    j = f2.read();
    if(i != j) break;
  } while(i != -1 && j != -1);

  if(i != j)
    System.out.println("Files differ.");
  else
    System.out.println("Files are the same.");
} catch(IOException exc) {
  System.out.println("I/O Error: " + exc);
   }
 }
}

I compiled the code like this: javac CompFiles.java then the book told me to copy the files to a temp file using this command: java CompFiles CompFiles.java temp. The output is java.io.FileNotFoundExceptions: Temp(Access is dined). I am not using any IDEs. Thanks for any answers.

3

There are 3 best solutions below

0
On BEST ANSWER

I am not sure about the book. But to execute your program :

java CompFiles a.java b.java

should work. Assuming that a.java b.java are TEXT files and are present in the current directory (The directory location from where u r executing this program).

In your case

java CompFiles CompFiles.java temp

temp could be a folder hence u get this error

0
On

Your command would be java -cp . CompFiles file1 file2. But your really should use a package. (-cp . asumes you compiled ComFiles.class to your current directory).

The access denied is most likely caused from the fact that you tried to open a directory as a file stream (.\Temp\). If your test-fiels are in Temp, you can use java -cp . CompFiles temp\file1 temp\file2.

0
On
I/O Error: java.io.FileNotFoundException: Temp (Access is denied)

You are running program as below
Java CompFiles CompFiles.java temp

Here temp is seems like directory name rather than a file name. so, please give a valid file name in the second argument

You can try this for eg:

Java CompFiles CompFiles.java CompFiles.java

this will give you output: Files are the same

Why Access is denying?

you can not read a directory like a normal file