import java.io.*;
public class TestFileInputString {
public static void main(String[] args) {
FileInputStream in = null;
int b=0;
try{
in = new FileInputStream("d:/TestFileInputStream.java");
}catch(FileNotFoundException f){
System.out.println("未找到文件!");
}
long num = 0;
try{
while((b = in.read())!=-1){
System.out.print((char)b);
num++;
}
in.close();
System.out.println();
System.out.println("共读取了" + num + "个字符");
}catch(IOException e){
System.out.println("文件读取异常!");
}
}
}
下面看一个输入输出流的程序
import java.io.*;
public class TestFileOutputStream{
public static void main(String[] args) {
int b = 0;
// FileInputStream in = null;
// FileOutputStream out = null;
try{
FileInputStream in = new FileInputStream("d:/java/HelloWorld.java");
FileOutputStream out = new FileOutputStream("d:/java/JW.java");
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
}catch(FileNotFoundException e2){
System.out.println("未找到文件!");
}catch(IOException e1){
System.out.println("文件复制错误!");
}
System.out.println("文件复制成功!");
}
}
这两个程序有着严重的问题,因为JDK文档所诉,FileInputStream和FileOutputStream这两个类只能对每个字符进行读取,因此读不出来汉子的,所以要使用FileReader和FileWrite