大家好:
本人在io读写时陷入了困境,希望各位能给予帮助,情况如下:1,我有段程式要写文件,本来能程式也跑的很欢快的,写文件时,如没有该文件,就会自动创建一个(感觉很美好)2,某天,程式忽然就罢工了,还提出以下请求
Exception in thread "main" java.io.FileNotFoundException: D:\PageCapture\index.html (系統找不到指定的路徑。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at java.io.FileWriter.<init>(FileWriter.java:46)
at pagecapture.Page.SaveAsFile(Page.java:87)
at pagecapture.Main.main(Main.java:12)
源码很简单,但就是在没有该文件的时候不肯写文件了(如果有这个文件就不会报错)3,想了想我改了改代码,在读写前加上了File.createNewFile()结果如下
java.io.IOException: 系統找不到指定的路徑。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at pagecapture.Page.SaveAsFile(Page.java:87)
at pagecapture.Main.main(Main.java:12)心有所感,总觉得这个应该与操作系统或JVM有关(代码很标准规范,也没有Bug)
不知道哪位前辈先人有遇到过这个问题,敬请指点,不胜感激
本人在io读写时陷入了困境,希望各位能给予帮助,情况如下:1,我有段程式要写文件,本来能程式也跑的很欢快的,写文件时,如没有该文件,就会自动创建一个(感觉很美好)2,某天,程式忽然就罢工了,还提出以下请求
Exception in thread "main" java.io.FileNotFoundException: D:\PageCapture\index.html (系統找不到指定的路徑。)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at java.io.FileWriter.<init>(FileWriter.java:46)
at pagecapture.Page.SaveAsFile(Page.java:87)
at pagecapture.Main.main(Main.java:12)
源码很简单,但就是在没有该文件的时候不肯写文件了(如果有这个文件就不会报错)3,想了想我改了改代码,在读写前加上了File.createNewFile()结果如下
java.io.IOException: 系統找不到指定的路徑。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at pagecapture.Page.SaveAsFile(Page.java:87)
at pagecapture.Main.main(Main.java:12)心有所感,总觉得这个应该与操作系统或JVM有关(代码很标准规范,也没有Bug)
不知道哪位前辈先人有遇到过这个问题,敬请指点,不胜感激
如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开第二个异常的原因就是: 发生io错误了!
File.separator,这样不管是linux还是windows分隔符号都是对的
//targetName 文件名
//path 存储路径
//content 是一个有内容的String
System.out.println("Begin Save as a file... :" + targetName);
SaveAllImages(path);
BufferedReader in = new BufferedReader(new StringReader(content));
try {
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(path+ File.separator + targetName)));
String inputLine;
while ((inputLine = in.readLine()) != null) {
String strLine = System.getProperty("line.separator");
out.println(inputLine + strLine);
}
out.close();
} catch (EOFException e) {
System.err.println("End of stream save as a file:"+targetName);
}
in.close();
System.out.println("Save File Success! On :" + path);
}
这个应该不是代码的原因,本来好好的,突然出现的上面的错误。
总的说就是无法创建文件,如果系统有这个代码就可以正常执行。
我觉得应该注意错误中的 (Native Method) 。
简单的说,是无法创建文件~除了这个错误后我专门写了一个简单的文件读写类来验证。
如下:import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringReader;public class test { static String path = "D:/PageCapture";
// static String path = "PageCapture"; 关于路径这几种都不行
// static String path = "D:\\PageCapture"; 本来应该都可以的,我windows系统
static String targetName = "index.txt"; public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new StringReader("I am very buzy now!")); File file = new File(path + File.separator + targetName);
// if (!file.exists()) { 如果这里不注释报首贴中的第2种错
// file.createNewFile();
// }
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
String inputLine;
while ((inputLine = in.readLine()) != null) {
String strLine = System.getProperty("line.separator");
out.println(inputLine + strLine);
}
out.close();
in.close();
}
}我确定,大多数人跑这个代码肯定没有问题
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringReader;public class test { static String path = "D:/PageCapture";
// static String path = "PageCapture";
// static String path = "D:\\PageCapture";
static String targetName = "index.txt"; public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new StringReader("I am very buzy now!"));
File fpath = new File(path); //一定要先创建好目录
fpath.mkdirs();
File file = new File(path + File.separator + targetName);
// if (!file.exists()) { //在目录存在的情况下不一定要创建文件了,打开流的时候会自动创建
// file.createNewFile();
// }
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
String inputLine;
while ((inputLine = in.readLine()) != null) {
String strLine = System.getProperty("line.separator");
out.println(inputLine + strLine);
}
out.close();
in.close();
}
}以上谢谢了
结贴