【发布时间】:2016-03-23 18:17:19
【问题描述】:
当我尝试通过 Eclipse 运行此代码时,它工作正常,但是当我尝试使用“java MainClass >result.txt”通过 CMD 运行它时,我得到一个 FileNotFoundException。 这是有问题的代码:
导入 java.io.; 导入 java.util.;
公共类 MainClass {
static int cellNumber;
static int freeSpace;
static int randomResult;
static int chosen;
static int choiceSize;
public static void main(String[] args)
{
Scanner in = null;
try
{
in = new Scanner(new FileReader("C:\\users\\Alon\\workspace\\ex2temp\\bin\\input.txt"));
FileWriter fw = new FileWriter("C:\\users\\Alon\\workspace\\ex2temp\\bin\\result.txt");
PrintWriter pw = new PrintWriter(fw);
chosen = getRandomInt();
pw.printf("Choice=%d", chosen);
pw.println();
while (in.hasNext())
{
cellNumber = in.nextInt();
freeSpace = in.nextInt();
if (sizeOfChosen(chosen) <= freeSpace)
{
pw.printf("%d", cellNumber);
pw.println();
break;
}
}
if (!in.hasNext())
{
pw.println("Cannot allocate memory");
pw.println();
}
pw.close();
fw.close();
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
有人可以帮忙吗?谢谢:)
【问题讨论】:
-
在您的问题中包含堆栈跟踪。
-
C:\\users\\...可能被隐藏(我遇到过这个问题),请尝试将文件放在其他位置。 -
这是我在 CMD 中得到的堆栈跟踪:java.io.FileNotFoundException: C:\users\Alon\workspace\ex2temp\bin\result.txt(进程无法访问该文件,因为它是被另一个进程使用)在 java.io.FileOutputStream.open(Unknown Source) 在 java.io.FileOutputStream.
(Unknown Source) 在 java.io.FileOutputStream. (Unknown Source) 在 java.io.FileOutputStream.open0(Native Method) . (Unknown Source) at java.io.FileWriter. (Unknown Source) at MainClass.main(MainClass.java:20)
标签: java file cmd filenotfoundexception