码海拾遗 · Java I/O 学习笔记 一、标准输入输出控制台1. 标准输出System.outSystem.out.print()/println()/printf()最常用的控制台输出。javaSystem.out.println(普通输出); System.out.printf(格式化输出%d %d %d%n, 3, 5, 3 5);2. 标准输入System.inSystem.in是字节流InputStream通常用Scanner或BufferedReader包装。方式一Scanner最常用javaimport java.util.Scanner; Scanner sc new Scanner(System.in); System.out.print(请输入姓名); String name sc.nextLine(); System.out.print(请输入年龄); int age sc.nextInt(); System.out.println(name 今年 age 岁); sc.close();方式二BufferedReader效率高需处理异常javaimport java.io.BufferedReader; import java.io.InputStreamReader; BufferedReader br new BufferedReader(new InputStreamReader(System.in)); System.out.print(请输入内容); String line br.readLine(); System.out.println(你输入了 line); br.close();二、文件 I/O 基础Java I/O 分为字节流和字符流顶层抽象类字节流InputStream/OutputStream字符流Reader/Writer1. 字节流读写文件适合图片、视频等二进制文件文件输入流FileInputStreamjavaimport java.io.FileInputStream; import java.io.IOException; try (FileInputStream fis new FileInputStream(test.txt)) { int b; while ((b fis.read()) ! -1) { System.out.print((char) b); } } catch (IOException e) { e.printStackTrace(); }文件输出流FileOutputStreamjavaimport java.io.FileOutputStream; import java.io.IOException; try (FileOutputStream fos new FileOutputStream(output.txt)) { String data Hello, Java I/O!; fos.write(data.getBytes()); } catch (IOException e) { e.printStackTrace(); }拷贝文件字节流一次读取一个字节数组javatry (FileInputStream fis new FileInputStream(src.jpg); FileOutputStream fos new FileOutputStream(dst.jpg)) { byte[] buffer new byte[1024]; int len; while ((len fis.read(buffer)) ! -1) { fos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); }2. 字符流读写文件适合文本文件FileReader / FileWriterjavaimport java.io.FileReader; import java.io.FileWriter; import java.io.IOException; // 写文本文件 try (FileWriter fw new FileWriter(note.txt)) { fw.write(第一行内容\n); fw.write(第二行内容); } catch (IOException e) { e.printStackTrace(); } // 读文本文件 try (FileReader fr new FileReader(note.txt)) { int ch; while ((ch fr.read()) ! -1) { System.out.print((char) ch); } } catch (IOException e) { e.printStackTrace(); }3. 缓冲流提高性能BufferedInputStream/BufferedOutputStream字节缓冲BufferedReader/BufferedWriter字符缓冲支持按行读写使用 BufferedReader 按行读取javaimport java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; try (BufferedReader br new BufferedReader(new FileReader(data.txt))) { String line; while ((line br.readLine()) ! null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); }使用 BufferedWriter 按行写入javaimport java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; try (BufferedWriter bw new BufferedWriter(new FileWriter(out.txt))) { bw.write(第一行); bw.newLine(); bw.write(第二行); } catch (IOException e) { e.printStackTrace(); }三、转换流字节 ↔ 字符InputStreamReader字节→字符OutputStreamWriter字符→字节通常用于指定编码如 UTF-8, GBK。javaimport java.io.*; // 以 UTF-8 编码读取文件 try (BufferedReader br new BufferedReader( new InputStreamReader(new FileInputStream(utf8.txt), UTF-8))) { String line; while ((line br.readLine()) ! null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } // 以 GBK 编码写入文件 try (BufferedWriter bw new BufferedWriter( new OutputStreamWriter(new FileOutputStream(gbk.txt), GBK))) { bw.write(中文内容); } catch (IOException e) { e.printStackTrace(); }四、NIONew I/OJava 1.4NIO 提供更高效的非阻塞 I/O核心ChannelBufferSelector。简单示例使用 FileChannel 拷贝文件javaimport java.io.*; import java.nio.channels.FileChannel; try (FileChannel src new FileInputStream(src.txt).getChannel(); FileChannel dest new FileOutputStream(dest.txt).getChannel()) { dest.transferFrom(src, 0, src.size()); } catch (IOException e) { e.printStackTrace(); }使用 Files 工具类Java 7最简洁javaimport java.nio.file.*; // 一次性读取所有行 ListString lines Files.readAllLines(Paths.get(data.txt), StandardCharsets.UTF_8); lines.forEach(System.out::println); // 一次性写入 ListString content Arrays.asList(行1, 行2, 行3); Files.write(Paths.get(out.txt), content, StandardCharsets.UTF_8); // 复制文件 Files.copy(Paths.get(a.txt), Paths.get(b.txt), StandardCopyOption.REPLACE_EXISTING);五、常见问题与最佳实践问题建议中文乱码使用InputStreamReader/OutputStreamWriter指定编码如UTF-8忘记关闭流使用try-with-resourcesJava 7自动关闭效率低使用缓冲流BufferedReader/BufferedWriter大文件读取使用BufferedReader按行读或 NIO 的Files.lines()需要随机访问使用RandomAccessFile或 NIO 的FileChannel六、完整综合示例含异常处理javaimport java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.*; public class IODemo { public static void main(String[] args) { // 写入 try (BufferedWriter bw Files.newBufferedWriter(Paths.get(demo.txt), StandardCharsets.UTF_8)) { bw.write(Hello, 你好); bw.newLine(); bw.write(第二行); } catch (IOException e) { e.printStackTrace(); } // 读取 try (BufferedReader br Files.newBufferedReader(Paths.get(demo.txt), StandardCharsets.UTF_8)) { String line; while ((line br.readLine()) ! null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }

相关新闻

最新新闻

CUTTag与RNA-seq多组学关联分析:5大核心套路与实操指南

CUTTag与RNA-seq多组学关联分析:5大核心套路与实操指南

1. 项目概述:从单组学到多组学关联的必然之路在表观遗传学和转录组学研究领域,我们早已不满足于“单打独斗”式的数据分析。过去,我们可能分别做一次CUT&Tag实验来描绘组蛋白修饰或转录因子的结合图谱,再单独做一次RNA-seq来测…

2026/8/13 1:33:46
航空数据分析实战:从ADS-B数据到航班离港轨迹可视化

航空数据分析实战:从ADS-B数据到航班离港轨迹可视化

最近在做一个航空数据分析的小项目,需要处理大量的航班起降数据。在整理香港国际机场的离港航班信息时,我发现了一个有趣的现象:从同一个机场起飞的航班,其飞行轨迹、高度变化、速度曲线等数据背后,其实隐藏着一套复杂…

2026/8/13 1:33:46
彻底告别重复图片!AntiDupl.NET开源图片去重工具终极指南

彻底告别重复图片!AntiDupl.NET开源图片去重工具终极指南

彻底告别重复图片!AntiDupl.NET开源图片去重工具终极指南 【免费下载链接】AntiDupl A program to search similar and defect pictures on the disk 项目地址: https://gitcode.com/gh_mirrors/an/AntiDupl 你是否曾因电脑中堆积如山的重复图片而烦恼&#…

2026/8/13 1:33:46
AI智能体工作流:从模糊需求到清晰开发任务的自动化拆解实践

AI智能体工作流:从模糊需求到清晰开发任务的自动化拆解实践

最近在技术社区里,一个名为“这能赢啊”的项目悄然走红。乍一看,这个标题充满了游戏化的轻松感,甚至带点调侃,很容易让人误以为又是一个昙花一现的“玩具”项目。但当你真正深入其中,会发现它瞄准了一个非常具体且高频…

2026/8/13 1:33:46
破解AMD Ryzen内存性能瓶颈:ZenTimings实战指南

破解AMD Ryzen内存性能瓶颈:ZenTimings实战指南

破解AMD Ryzen内存性能瓶颈:ZenTimings实战指南 【免费下载链接】ZenTimings 项目地址: https://gitcode.com/gh_mirrors/ze/ZenTimings 当你在AMD Ryzen平台上进行内存超频时,是否曾遇到这样的困境:BIOS中设置的参数与系统实际运行的…

2026/8/13 1:33:46
Insomnia API测试工具:5分钟掌握环境变量与自动化测试实战

Insomnia API测试工具:5分钟掌握环境变量与自动化测试实战

1. 项目概述:为什么Insomnia能成为API测试的“瑞士军刀”?如果你和我一样,每天都要和十几个甚至几十个API打交道,那你肯定懂那种在Postman、命令行curl、浏览器开发者工具之间反复横跳的烦躁。数据格式对不对?认证头加…

2026/8/13 1:28:45