java学习之IO字符流
1 package com.io; 2 3 import java.io.*; 4 /** 5 * 文件字符流的读取 6 * @author ganhang 7 * 8 */ 9 public class FileReaderDemo { 10 public static void read(){ 11 File file =new File("1.txt"); 12 try { 13 FileReader fr=new FileReader(file); 14 StringBuffer sb=new StringBuffer(); 15 char [] cbuf=new char[10]; 16 int len=-1; 17 while((len=fr.read(cbuf))!=-1){ 18 sb.append(cbuf,0,len); 19 } 20 fr.close(); 21 System.out.println(sb); 22 } catch (FileNotFoundException e) { 23 e.printStackTrace(); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 28 29 } 30 public static void main(String[] args) { 31 read(); 32 } 33 }
1 package com.io; 2 3 import java.io.*; 4 /** 5 * 文件字符流的写入 6 * @author ganhang 7 * 8 */ 9 public class FileWriterDemo { 10 public static void write(){ 11 File file =new File("1.txt"); 12 try { 13 FileWriter fr = new FileWriter(file,true); 14 String str="哈哈哈哈"; 15 fr.write(str); 16 fr.close(); 17 System.out.println("写入成功!"); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 } 22 public static void main(String[] args) { 23 write(); 24 } 25 }
随便写写。一点学习心得。。。--如果本文章没有注明转载则为原创文章,可以随意复制发表,但请注明出处与作者
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 从零开始开发一个 MCP Server!
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档