public class FileInputStreamTestDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("FileInputStreamTestDemo.java");
byte [] b=new byte[1024];
int hasRead=0;
while((hasRead=fis.read())!=0){
System.out.print(new String(b,0,hasRead));
}
}
}异常:Exception in thread "main" java.io.FileNotFoundException: FileInputStreamTestDemo.java (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at FileInputStreamTestDemo.main(FileInputStreamTestDemo.java:8)
求解
FileInputStream fis=new FileInputStream("c:/FileInputStreamTestDemo.java");
是可以找到文件的,不会出现FileNotFoundException
所以你应该是在某工具里运行的,那么应该有包名 xxxx.xx.FileInputStreamTestDemo.java
另外
你这段读不出字节的 应该用FileInputStream.read(byte[] b)package xxxx.xx;
public class FileInputStreamTestDemo {
public static void main(String[] args) throws IOException {
String path = System.getProperty("user.dir");
path+="\\src\\xxxx\\xx\\FileInputStreamTestDemo.java";//注意包名
FileInputStream fis=new FileInputStream(path);
byte [] b=new byte[1024];
int hasRead=0;
while((hasRead=fis.read(b))!=-1){
System.out.print(new String(b,0,hasRead));
}
}
}