[code]
package com.teststream;
import java.io.* ;
public class TestFile {
public static void main(String args[]) {
FileInputStream file1= null ;
// FileInputStream must throws FileNotFoundException
try {
file1 = new FileInputStream("d:\\java\\test1.txt") ; // 读取文件 ,
} catch(FileNotFoundException e) {
System.out.println("找不到指定文件。") ;
System.exit(-1) ; //退出
}
// read must read() throws IOException
try {
long num = 0 ; // 用于统计读出的文件字节总数计算
int b ;
while((b = file1.read())!= -1) { //判断输入流中是否还有数据字节
System.out.print((char)b) ; //强制转成字符输出,读汉字等非一个字节的值FileInputStream无法正常读取
num ++ ;
}
file1.close() ; //关闭此文件输入流并释放与此流有关的所有系统资源
System.out.println();
System.out.println("共读取了" + num + "个字节") ;
} catch (IOException e1) {
System.out.println("文件读取发生错误。") ;
System.exit(-1) ;
}
// FileOutputStream 的测试小程序,copy 文件test1的内容copy到test2 中
try{
file1 = new FileInputStream("d:\\java\\test1.txt") ;
FileOutputStream file2 = new FileOutputStream("d:/java/test2.txt") ;
int b ;
while((b= file1.read())!=-1) {
file2.write(b) ;
}
file1.close() ;
file2.close() ;
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件。") ;
System.exit(-1) ;
} catch (IOException e3) {
System.out.println("文件复制出错。") ;
System.exit(-1) ;
}
System.out.println("文件已复制。") ;
}
}
[/code]
package com.teststream;
import java.io.* ;
public class TestFile {
public static void main(String args[]) {
FileInputStream file1= null ;
// FileInputStream must throws FileNotFoundException
try {
file1 = new FileInputStream("d:\\java\\test1.txt") ; // 读取文件 ,
} catch(FileNotFoundException e) {
System.out.println("找不到指定文件。") ;
System.exit(-1) ; //退出
}
// read must read() throws IOException
try {
long num = 0 ; // 用于统计读出的文件字节总数计算
int b ;
while((b = file1.read())!= -1) { //判断输入流中是否还有数据字节
System.out.print((char)b) ; //强制转成字符输出,读汉字等非一个字节的值FileInputStream无法正常读取
num ++ ;
}
file1.close() ; //关闭此文件输入流并释放与此流有关的所有系统资源
System.out.println();
System.out.println("共读取了" + num + "个字节") ;
} catch (IOException e1) {
System.out.println("文件读取发生错误。") ;
System.exit(-1) ;
}
// FileOutputStream 的测试小程序,copy 文件test1的内容copy到test2 中
try{
file1 = new FileInputStream("d:\\java\\test1.txt") ;
FileOutputStream file2 = new FileOutputStream("d:/java/test2.txt") ;
int b ;
while((b= file1.read())!=-1) {
file2.write(b) ;
}
file1.close() ;
file2.close() ;
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件。") ;
System.exit(-1) ;
} catch (IOException e3) {
System.out.println("文件复制出错。") ;
System.exit(-1) ;
}
System.out.println("文件已复制。") ;
}
}
[/code]