字符流写入字符串

本文介绍如何使用Java字符流高效地将字符串统计信息写入文件。通过字符串分割、Map集合记录单词频率,最后遍历并写入D:count.txt文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用代码实现以下需求
(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(2)打印格式( 单词 = 出现的次数 ):
to=3
think=1
you=2
//…
(3)按照上面的打印格式将内容写入到D:\count.txt文件中(要求用高效流)

思路:先利用字符串方法分割字符串成字符串数组,然后利用map集合记录各单词出现的次数,最后遍历集合写入文本文件中。
特别注意,对类的命名不要和FileWriter一样。

代码:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 *	用代码实现以下需求
	(1)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
	(2)打印格式(  单词 = 出现的次数  ):
		to=3
		think=1
		you=2
		//........
	(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
 */
public class StringFileWriter {

	public static void main(String[] args) throws IOException {
		//字符串存储
		String s = "If you want to change your fate I think you must come to the ujiuye to learn java";
		//定义正则表达式
		String regex = "\\s";
		//切割字符串
		String[] ss =s.split(regex); 
		//定义map集合存储	
		Map<String, Integer> map = new HashMap<String, Integer>();
		//循环判断存入集合
		for (String s1 : ss) {
			if (map.containsKey(s1)) {
				map.put(s1, map.get(s1)+1);
			}else {
				map.put(s1, 1);
			}
		}
		//定义高效缓冲字符流
		FileWriter fw = new FileWriter("E:\\aa\\123.txt");
		BufferedWriter bw = new BufferedWriter(fw);
		//遍历map集合写入文件中
		Set<String> set = map.keySet();
		
		for (String s2 : set) {
			Integer value = map.get(s2);
			bw.write(s2+"="+value);
			bw.newLine();
		}
		
//		System.out.println(map);
		//关闭流
		bw.close();
	}  

}

结果:
在这里插入图片描述

### Java字符输出流写入字符串示例 以下是基于`java.io.FileWriter`实现的字符输出流写入字符串的具体代码示例: ```java import java.io.FileWriter; import java.io.IOException; public class WriteStringToFile { public static void main(String[] args) { String content = "这是一个测试字符串"; // 要写入的内容 try (FileWriter writer = new FileWriter("output.txt")) { // 创建文件字符输出流对象 char[] chars = content.toCharArray(); // 将字符串转换为字符数组 writer.write(chars); // 写入整个字符数组到文件中 writer.flush(); // 刷新缓冲区,确保数据被完全写入文件 } catch (IOException e) { // 捕获可能发生的IO异常 System.out.println("发生错误:" + e.getMessage()); } } } ``` 上述代码展示了如何利用`FileWriter`将字符串写入文件。其中的关键步骤包括实例化`FileWriter`对象、将字符串转为字符数组并通过`write()`方法写入文件[^2]。 对于更复杂的场景,比如仅写入字符串的部分内容,则可以通过指定起始偏移量和长度来完成。例如: ```java import java.io.FileWriter; import java.io.IOException; public class PartialWriteExample { public static void main(String[] args) { String content = "这是另一个测试字符串"; int offset = 8; // 开始位置索引 int length = 7; // 需要写入的字符数量 try (FileWriter writer = new FileWriter("partial_output.txt")) { writer.write(content.toCharArray(), offset, length); writer.flush(); } catch (IOException e) { System.out.println("发生错误:" + e.getMessage()); } } } ``` 此代码片段演示了如何通过`write(char[] cbuf, int off, int len)`方法写入字符串的一部分[^1]。 ### 注意事项 - 使用完毕后务必调用`flush()`刷新缓冲区以及`close()`关闭流资源,以防止内存泄漏或数据丢失。 - `try-with-resources`语法能够自动管理资源释放过程,推荐优先采用这种方式处理I/O操作中的资源管理问题[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值