尝试使用输入和输出文件通过CMD运行java程序时获取FileNotFoundException

尝试使用输入和输出文件通过CMD运行java程序时获取FileNotFoundException,java,file,cmd,filenotfoundexception,Java,File,Cmd,Filenotfoundexception,当我尝试通过eclipse运行这段代码时,它运行得很好,但是当我尝试使用“javamainclass>result.txt”通过CMD运行它时,我得到一个FileNotFoundException。 以下是相关代码: 导入java.io。; 导入java.util 公共类主类 { static int cellNumber; 静态int自由空间; 静态随机结果; 选择静态int; 静态整数选择大小; 公共静态void main(字符串[]args) { 扫描仪输入=空; 尝试 { in=新扫描仪

当我尝试通过eclipse运行这段代码时,它运行得很好,但是当我尝试使用“javamainclass>result.txt”通过CMD运行它时,我得到一个FileNotFoundException。 以下是相关代码:

导入java.io。; 导入java.util

公共类主类 {

static int cellNumber;
静态int自由空间;
静态随机结果;
选择静态int;
静态整数选择大小;
公共静态void main(字符串[]args)
{
扫描仪输入=空;
尝试
{
in=新扫描仪(新文件阅读器(“C:\\users\\Alon\\workspace\\ex2temp\\bin\\input.txt”);
FileWriter fw=newfilewriter(“C:\\users\\Alon\\workspace\\ex2temp\\bin\\result.txt”);
PrintWriter pw=新的PrintWriter(fw);
selected=getRandomInt();
pw.printf(“选择=%d”,已选择);
println();
while(在.hasNext()中)
{
cellNumber=in.nextInt();
freeSpace=in.nextInt();

如果(sizeOfChosen(selected)您正在使用“../ext2temp/bin/”中的文件,我假定它是Eclipse的输出文件夹。请使用原始文件所在的路径。

为了在同一文件上创建连续写入,我必须将文本附加到现有文件中。我已添加了“true”在创建FileWriter时。然后,我创建了一个新的BufferedWriter,它接收FileWriter对象,最后,将PrintWriter接收对象更改为BufferedWriter。这样,每次我运行程序时,在旧的数据行下面会形成两行新的数据,我会得到一个“日志文件”.

在您的问题中包括堆栈跟踪。
C:\\users\\\…
可能被隐藏(我遇到了这个问题),请尝试将文件放在其他地方。这是我在CMD:java.io.FileNotFoundException:C:\users\Alon\workspace\ex2temp\bin\result.txt中获得的堆栈跟踪(进程无法访问该文件,因为它正被另一进程使用)java.io.FileOutputStream.open0(本机方法)java.io.FileOutputStream.open(未知源代码)java.io.FileOutputStream.(未知源代码)java.io.FileOutputStream.(未知源代码)java.io.FileWriter.(未知源代码)MainClass.main(MainClass.java:20)你的意思是将类文件和输入文件向上移动一级,然后尝试从那里运行吗?在bin目录的内部-外部?不是类文件。我说的是input.txt和result.txtException避免了!!谢谢!!我已经将input.txt向上移动了一级。result.txt是在运行之后形成的,但是现在,当我运行“java MainClass>result.txt”时在CMD上,我得到一个空的result.txt,而当我从Eclipse上运行它时,result.txt中有正确的数据。只是澄清几件事:1.我应该运行这个代码20次。第一次,result.txt不存在。它应该在第一次形成,在它结束时,它应该保存2行数据。2.运行后如果再重复19次,我应该在同一个result.txt文件中有40行数据。(类似于构建日志文件的训练和练习)。我在这里做错了什么?
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();
    }
}