1 字节流和字符流
- 字节流
- 输入流:
InputStream
- 输出流:
OutputStream
- 字符流
2 字节流
/*
* 从文件中读取
*/
public static void test01() {
String filePath = "D:\\test\\readTest.txt";
// 建立连接
File file = new File(filePath);
InputStream input = null;
try {
// 选择流
input = new FileInputStream(file);
byte[] b = new byte[10000];
int len = 0;
// 读写操作
while ((len = input.read(b)) != -1) {
String str = new String(b, 0, len, "UTF-8");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 向文件中写入
*/
public static void test02() {
String filePath = "D:\\test\\writeTest.txt";
// 建立连接
File file = new File(filePath);
OutputStream output = null;
try {
file.createNewFile();
// true:以追加的形式写入,默认为false覆盖
output = new FileOutputStream(file, true);
String str = "aaa";
byte[] b = str.getBytes();
output.write(b);
output.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/*
* 文件拷贝
*/
public static void test03() {
String inputFilePath = "D:\\test\\readTest.txt";
String outputFilePath = "D:\\test\\writeTest.txt";
File inputFile = new File(inputFilePath);
File outputFile = new File(outputFilePath);
InputStream inputStream = null;
OutputStream outputStrem = null;
try {
byte[] b = new byte[100000];
inputStream = new FileInputStream(inputFile);
outputStrem = new FileOutputStream(outputFile, true);
int len = 0;
while ((len = inputStream.read(b)) != -1) {
// 推荐使用三个参数的写入方法,防止最后一次写入的时候写入多余的空格
outputStrem.write(b, 0, len);
}
outputStrem.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 先打开的后关闭
if (outputStrem != null) {
outputStrem.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
3 字符流
- 只能处理纯文本,全部为可见字符,常见包括.txt/.html
- 一般步骤
- 建立连接
- 选择合适的流
- 读取写入操作(使用
char[]
) - 释放资源
/*
* 字符流读取文件
*/
public static void test01() {
// 建立连接
String srcFilePath = "D:\\test\\readTest.txt";
File srcFile = new File(srcFilePath);
// 选取流
Reader reader = null;
try {
reader = new FileReader(srcFile);
char[] ch = new char[100];
int len = 0;
// 读取操作
while ((len = reader.read(ch)) != -1) {
String str = new String(ch, 0, len);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/*
* 字符流写入文件
*/
public static void test02() {
// 建立连接
String destFilePath = "D:\\test\\writeTest.txt";
File destFile = new File(destFilePath);
// 选取流
Writer writer = null;
try {
// 方法一:使用write方法写入文件
writer = new FileWriter(destFile, true);
String str = "aaaaaaaa";
writer.write(str);
writer.flush();
// 方法二:使用append方法写入文件
char[] ch = { 'b', 'b', 'b', 'b', 'b', 'b', 'b' };
for (char tmp : ch) {
writer.append(tmp);
}
writer.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/*
* 字符流文件的拷贝
*/
public static void test03() {
// 建立连接
String srcFilePath = "D:\\test\\readTest.txt";
String destFilePath = "D:\\test\\writeTest.txt";
File srcFile = new File(srcFilePath);
File destFile = new File(destFilePath);
// 选取流
Reader reader = null;
Writer writer = null;
try {
reader = new FileReader(srcFile);
writer = new FileWriter(destFile);
char[] ch = new char[100];
int len = 0;
while ((len = reader.read(ch)) != -1) {
writer.write(ch, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}