public class FileIn_Out {
public static void main(String args[]) {
String filename = "e://aa.text";
Reader reader = null;
try {
File f = new File(filename);
System.out.println(f);// 测试路径是否正确
char[] t = new char[40];
int b;
reader = new InputStreamReader(new FileInputStream(filename));
while ((b = reader.read(t)) != -1) {
for (int i = 0; i < b; i++) {
System.out.print(t[i]);
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
}
出现问题:
e:\aa.text
java.io.FileNotFoundException: e:\aa.text (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at ygs.xy.a.FileIn_Out.main(FileIn_Out.java:15)
public static void main(String args[]) {
String filename = "e://aa.text";
Reader reader = null;
try {
File f = new File(filename);
System.out.println(f);// 测试路径是否正确
char[] t = new char[40];
int b;
reader = new InputStreamReader(new FileInputStream(filename));
while ((b = reader.read(t)) != -1) {
for (int i = 0; i < b; i++) {
System.out.print(t[i]);
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
}
出现问题:
e:\aa.text
java.io.FileNotFoundException: e:\aa.text (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at ygs.xy.a.FileIn_Out.main(FileIn_Out.java:15)
System.out.println(f);// 测试路径是否正确
char[] t = new char[40];
int b;
reader = new InputStreamReader(new FileInputStream(filename));
你这里new的是什么?里面放的filenname
你上面file f=new,...
你下面那里用到了F这个路径?
你上面file f=new,...
你下面那里用到了F这个路径?”这个测试了路径是对的“e:\aa.text
”,下面的代码报错,这个很是纠结~
if(file.isFile()){
System.out.println("true");
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(target)));
try {
System.out.println(in.read());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
楼主何以用isDirectory() 来检查一下
请改正以后再试一试
1、是否存在
2、是否是目录
public class TestDemo02 {
public static void main(String args[]) {
String filename = "e://aa.text";
Reader reader = null;
try { File f = new File(filename);
boolean isExit = f.exists();
if (isExit) {
boolean isFile = f.isDirectory();
if (!isFile) {
char[] t = new char[40];
int b;
reader = new InputStreamReader(
new FileInputStream(filename));
while ((b = reader.read(t)) != -1) {
for (int i = 0; i < b; i++) {
System.out.print(t[i]);
}
} }else{
System.out.println("操作的是目录,不是文件");
} }else{
System.out.println("文件不存在");
} } catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) { }
}
}
}
}