java 文件的打开与写入分析总结
- 最简单的开始,java读取txt文本字符串
package opentxt;
import java.io.*;
public class xumingtao {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
int i;
FileInputStream fin=null;//build a FileInputStream name
try{
fin=new FileInputStream("FileInput.txt");
}catch(FileNotFoundException exe){
System.out.println("File Not Found");
}
do{
i=fin.read();
if(i!=-1)System.out.print((char)i);
}while(i!=-1);
fin.close();
}
}
首先进行解释throws IOException
这个的用法,这个是在程序表示有抛出 IOException
的可能性,只要此方法的某些行为可能抛出异常,你就必须显式的处理它,用try catch
,或者抛出。这个就是java语言的异常处理机制。
- 其次介绍
FileInputStream
的用法