Search code examples
javacommand-prompttemp

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


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.


Solution

  • 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