第三课 字节流和字符流

1 字节流和字符流

  • 字节流
    • 输入流:InputStream
    • 输出流:OutputStream
  • 字符流
    • 输入流:Reader
    • 输出流:Writer

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();
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值