本地文件读写编程的基本过程为:
① 生成文件流对象(对文件读操作时应该为FileInputStream类,而文件写应该为FileOutputStream类);
② 调用FileInputStream或FileOutputStream类中的功能函数如read()、write(int b)等)读写文件内容;
③ 关闭文件(close())。
//读取某路径下的文件
File file = new File("D:\\123.txt");
try {
FileInputStream fis=new FileInputStream(file);//新建一个FileInputStream对象
try {
byte[] b=new byte[fis.available()];//新建一个字节数组
fis.read(b);//将文件中的内容读取到字节数组中
fis.close();
String str2=new String(b);//再将字节数组中的内容转化成字符串形式输出
System.out.println(str2);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//文件的写入
FileOutputStream file_out = new FileOutputStream(file);
file_out.write(outputData); //byte数据
file_out.close();
继承于InputStream和OutputStream的类,用于本地文件读写(二进制格式读写并且是顺序读写,读和写要分别创建出不同的文件流对象)
http://www.cnblogs.com/kyxyes/archive/2013/02/16/2913424.html
http://www.cnblogs.com/jjtech/archive/2011/04/17/2019210.html