java字符读写

推荐使用Buffered进行读写字符(速度快):
(1)带有缓存
(2)可读一行

1、字符流读写–带有缓存的批量字符读写Buffered(可读一行)

	// 带有缓存的批量字符读写
	// 且可以读一行
	public void test3() {
		File fromFile = new File("D:/123.txt");
		File toFile = new File("E:/123.txt");
		BufferedReader reader = null;
		BufferedWriter writer = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(fromFile), "utf-8"));
			writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toFile), "utf-8"));
			// 带缓冲区的reader可以,每次读一行。如果已到达流末尾,则返回null
			String line = null;
			while((line=reader.readLine())!=null) {
				System.out.println(line);
				// 每次写出一行,但是'\n'换行的标志需要在此处显示的加上
				writer.write(line+"\n");
			}
			
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(writer!=null) {
					writer.close();
				}
				if(reader!=null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	
	}

2、将给定的文件路径读出字符串 并返回

	public static String readStringAndString(String filePath) {
		StringBuffer buffer = new StringBuffer();
		File file = new File(filePath);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
			String line=null;
			while((line=reader.readLine())!=null) {
				buffer.append(line).append("\n");
			}
			
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(reader!=null) {
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return buffer.toString();
	}

3、字符流读写–单个字符读写

	// 单个字符读写
	public void test1() {
		File fromFile = new File("D:/123.txt");
		File toFile = new File("E:/123.txt");
		Reader reader = null;
		Writer writer = null;
		try {
			// reader = new FileReader(fromFile); // 直接读写出现乱码,需要进行包装
			// writer = new FileWriter(toFile);
			reader = new InputStreamReader(new FileInputStream(fromFile), "utf-8");
			writer = new OutputStreamWriter(new FileOutputStream(toFile), "utf-8");

			int ch = -1;
			while ((ch = reader.read()) != -1) {
				System.out.println((char) ch);
				writer.write(ch);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(writer!=null) {
					writer.close();
				}
				if(reader!=null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

4、字符流读写–批量字符读写

	public void test2() {
		File fromFile = new File("D:/123.txt");
		File toFile = new File("E:/123.txt");
		Reader reader = null;
		Writer writer = null;
		try {
			reader = new InputStreamReader(new FileInputStream(fromFile), "utf-8");
			writer = new OutputStreamWriter(new FileOutputStream(toFile), "utf-8");
			// 定义一个char的数组,用于存放批量读取的字符数据
			char[] chars = new char[1024];
			int length = -1;
			// 每次将字符读取到char的数组,并返回读取的个数,当到达流的末尾则返回-1
			while ((length = reader.read(chars)) != -1) {
				System.out.println(Arrays.toString(chars));
				System.out.println("读取的字符数量:" + length);
				writer.write(chars, 0, length);
			}
		} catch (UnsupportedEncodingException | FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(writer!=null) {
					writer.close();
				}
				if(reader!=null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}

	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值