62,623
社区成员
发帖
与我相关
我的任务
分享
import java.io.*;
public class FileInputStreamTest {
public FileInputStreamTest() {
}
public static void main(String[] args){
FileInputStream fls = null;
File file = null;
int n;
try{
file = new File("FileInputStreamTest.java");
fls = new FileInputStream(file);
while((n=fls.read())!=-1){
//char ch = (char)fls.read();
System.out.write (n);
}
fls.close();
}catch(FileNotFoundException fnfe){
System.out.println ("找不到文件!!");
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
package test.io;
import java.io.*;
public class ReadFile{
public static void main(String[] args){
byte[] buff = new byte[1024];
int n;
FileInputStream fis = null;
System.out.println("输入文件名:");
try{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=br.readLine();
if(null==str)
return;
fis = new FileInputStream(str);
while((n=fis.read(buff)) != -1){
System.out.write(buff,0,n);
//FileOutputStream fos=new FileOutputStream("d:\1.txt");
//fos.write(buff,0,n);
} //end while
}catch(FileNotFoundException f){
System.out.println("文件未找到!");
f.printStackTrace();
}catch(IOException ee){
System.out.println("读错误。。。。");
}catch(NullPointerException e){
System.err.println("没有输入参数");
}catch(ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}finally{
try{
if(null!=fis)
fis.close();
}catch(IOException e){
System.out.println("文件错误");
System.exit(1);
}
} //end try...catch()
} //end main()
} //end class ReadFile