我在这里有一个简单的输入/输出流:
package managingfilesanddirectories;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try (
FileInputStream in = new FileInputStream("selfie.jpg");
FileOutputStream out = new FileOutputStream("newPic.jpg");) {
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
所有文件都在同一个包中,但每当我尝试制作新文件时,都不会创建新文件(包括.jpg, - 和.txt文件)。我是否使用netbeans,我应该将文件放在另一个包或目录中吗?
我收到此错误:
java.io.FileNotFoundException: test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at managingfilesanddirectories.Main.main(Main.java:18)
答案 0 :(得分:-1)
对于使用FileInputStream或FileOutputStream,你shoukd发送了ti构造函数File。 例如:
File myFile = new File("C:\\exampleFile.txt");
FileInputStream inputStream;
try {
inputStream = new FileInputStream(myFile);
// reading from input stream
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
inputStream.close();
}
为了更好,你应该检查文件和路径是否存在。