. io .FileNotFoundException: Temp(访问被拒绝)

时间:2021-02-27 08:35:55

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.

我这样编译代码:javac CompFiles。然后,该书告诉我使用这个命令将文件复制到一个临时文件:java CompFiles compfile。输出是java.io。FileNotFoundExceptions:临时用餐(访问)。我没有使用任何ide。谢谢你的任何答案。

3 个解决方案

#1


0  

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).

应该工作。假设一个。java b。java是文本文件,存在于当前目录(ur执行此程序的目录位置)。

In your case

在你的情况中

java CompFiles CompFiles.java temp

temp could be a folder hence u get this error

temp可能是一个文件夹,因此你会得到这个错误。

#2


0  

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).

您的命令是java -cp。CompFiles file1 file2。但是你真的应该使用一个软件包。(- cp。你编译ComFiles asumes。类到当前目录)。

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.

被拒绝的访问很可能是由于您试图以文件流的形式打开目录(.\Temp\)。如果您的测试文件在Temp中,您可以使用java -cp。CompFiles临时temp \ \ file1 file2。

#3


0  

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

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

在Java CompFiles下面运行程序。java临时

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

这里的temp看起来更像是目录名,而不是文件名。所以,请在第二个参数中给出一个有效的文件名

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

不能像普通文件那样读取目录

#1


0  

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).

应该工作。假设一个。java b。java是文本文件,存在于当前目录(ur执行此程序的目录位置)。

In your case

在你的情况中

java CompFiles CompFiles.java temp

temp could be a folder hence u get this error

temp可能是一个文件夹,因此你会得到这个错误。

#2


0  

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).

您的命令是java -cp。CompFiles file1 file2。但是你真的应该使用一个软件包。(- cp。你编译ComFiles asumes。类到当前目录)。

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.

被拒绝的访问很可能是由于您试图以文件流的形式打开目录(.\Temp\)。如果您的测试文件在Temp中,您可以使用java -cp。CompFiles临时temp \ \ file1 file2。

#3


0  

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

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

在Java CompFiles下面运行程序。java临时

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

这里的temp看起来更像是目录名,而不是文件名。所以,请在第二个参数中给出一个有效的文件名

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

不能像普通文件那样读取目录