Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件路径不正确_Java_Exception_Input_Stream_Output - Fatal编程技术网

Java 文件路径不正确

Java 文件路径不正确,java,exception,input,stream,output,Java,Exception,Input,Stream,Output,我不知道为什么,但是outStream=newfileoutputstream(file)和inStream=newfileinputstream(newfile1.getName())引发异常。我不知道该怎么办 下面是一些代码: File tempf = new File(cmds[1]); //cmds is a String with filename cmds[1] and pathname cmds[2] where to move the file

我不知道为什么,但是
outStream=newfileoutputstream(file)
inStream=newfileinputstream(newfile1.getName())
引发异常。我不知道该怎么办

下面是一些代码:

        File tempf = new File(cmds[1]); //cmds is a String with filename cmds[1] and  pathname cmds[2] where to move the file
        File tempw = new File(cmds[2]);
        if(!tempf.isAbsolute() || !tempw.isAbsolute()){//here i make paths absolute
            tempf = new File(tempf.getAbsolutePath());
            tempw = new File(tempw.getAbsolutePath());
        }
        String from = cmds[1];
        String where = cmds[2];
        File file1 = tempf;
        File file2 = new File (tempw.toString() + "/" + new File(cmds[1]).getName());
        InputStream inStream = null;
        OutputStream outStream = null;
        try {
            inStream = new FileInputStream(new File(file1.getName())); //throws an exception
            outStream = new FileOutputStream(file2); //throws an exception too
            byte[] buffer = new byte[16384];
            int length;
            while ((length = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, length);
            }

            if (inStream != null)
                inStream.close();
            if (outStream != null)
                outStream.close();
            file1.delete();

        } catch (IOException e) {
            System.err.println("permission denied");
        }
    } else {
        System.err.println("incorrect syntax");
    }
    continue;
}
看起来一切都应该正常,但事实并非如此。我越来越

java.io.FileNotFoundException: C:\Users\Maxim\IdeaProjects\Testing\OneDrive\1234.txt
但在我看来,这是一条错误的道路。实际路径是
C:\Users\Maxim\OneDrive


UPD!发现问题在于
getAbsolutePath()
返回项目所在的路径,但它不是我需要的路径。我需要
C:\Users\Maxim\OneDrive
但是它返回
C:\Users\Maxim\IdeaProjects\Testing\OneDrive
但是
../Testng
没有OneDrive

FileInputStream和FileOutputStream的构造函数在访问文件时出现问题(如文件不存在)时抛出错误。要阻止它抛出FileNotFoundException,请确保在实例化FileInput/OutputStream对象之前创建该文件

try{
    FileInputStream fis = new FileInputStream(file);
}catch(FileNotFoundException e){
    e.printStackTrace();
}

查看文档。

显示异常!!!关于异常,特别是标准库异常,最重要的是它们告诉您发生异常的原因。