java–文本文件写入
标签:数组matchdexintclassacknbspstr字符串
写入文本文件
1. 关联读入的文件,使用Reader 和 FileReader
2.关联写出的文件,使用Writer和 FileWriter
3. 创建缓冲 char数组,用于接收读取到的文本信息
4. 将文本读入到 缓冲数组(buff)中
5. 输出读取到的文本信息
6. 写出读取到的文件
7. 关闭写出文件流
8. 关闭读取文件流
package com.machuang.io.charIO;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class textWrite {
public static void main(String[] args) {
// 与文件建立联系
Reader reader = null;
Writer writer = null;
try {
reader = new FileReader(\”F:/win10/test/a.txt\”);
writer = new FileWriter(\”F:/win10/test/aCopy.txt\”, true);
// 创建 char 字符串缓冲数组
char[] cbuf = new char[1024];
// 读取和写入
int len = 0;
while(-1 != (len = reader.read(cbuf))) {
writer.write(cbuf);
writer.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(null != writer) {
writer.close();
}
if(null != reader) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} // match main function
}
java–文本文件写入
标签:数组matchdexintclassacknbspstr字符串
原文地址:https://www.cnblogs.com/cappuccinom/p/8809818.html