标签:
java7中引入自动资源管理(ARM),使用try(){}catch{}finally{},()中的资源会在程序运行后自动释放。
文件流分为输入流和输出流,典型用法
FileReader fr = new FileReader("poem.txt"); //读取已有文件
FileWriter fw = new FileWriter("newPoem2.txt"); //向特定名文件写入(不存在先创建),覆盖
char[] cbuf = new char[32]; //Reader和Writer读写以字符为单位
int hasRead = 0; //返回一次读取数
while ((hasRead = fr.read(cbuf)) > 0)
{
fw.write(cbuf,0,hasRead);
}
FileInputStream fis = new FileInputStream("poem.txt");
FileOutputStream fos = new FileOutputStream("newPoem.txt"))
byte[] bbuf = new byte[32]; //InputStream和OutputStream读写以字节为单位
int hasRead = 0;
while ((hasRead = fis.read(bbuf)) > 0)
{
fos.write(bbuf,0,hasRead);
}
标签:
原文地址:http://www.cnblogs.com/himanxu/p/4789911.html