您现在的位置是:首页 > Java 当前栏目 Java IO 讲解 文件 输入 输出 2023-03-02 11:13:25 时间 今天小编讲给大家简单介绍java IO ,以及读取文件代码实现。 按照流方向划分; 输入流 输出流 处理单元: 字节流 字符流=字节流+码表。 输入字节流体系: \----InputStream :输入字节流的一个基类 \--------FileInputStream 读取文件的字节流 \--------BufferedInputStream 缓冲输入字节流(速度较FileInputStream快) 输出字节流体系 \----OutputStream:输出字节流的一个基类 \---------FileOutputStream:向文件输出的字节流 \--------BufferedOutputStream 缓冲输出字节流 字符流: 输入字符流 \----Reader:输入字符流的一个基类 \--------FileReader \---------BufferedReader readLine(); 输出字符流 \----Writer:输出字符流的基类 \--------FileWriter \--------BufferedWriter newLine(); 转换流 \----InputStreamReader \----OutputStreamWriter PS:中文英文混合的文件建议使用字符流 JDK1.7下面的完整IO体系 ![SouthEast][] 感谢飞鸟提供:[java IO 最详解][java IO] //使用BufferedReader来读取 import java.io.*; public class ReaderDemo{ public static void main(String[] args) throws Exception{ BufferedReader bufferedReader = null; try{ //找到目标 File file = new File("F:\\alan.java"); //建立通道 bufferedReader = new BufferedReader(new FileReader(file)); //读取数据 String line = null; while ((line =bufferedReader.readLine())!= null){ System.out.println(line); } }catch(IOException e){ System.out.println("读取文件失败"); throw new RuntimeException(e); }finally{ //关闭流 try{ bufferedReader.close(); }catch(IOException e){ System.out.println("关闭资源失败"); throw new RuntimeException(e); } } } } //使用字节流FileInputStream来读取 import java.io.*; public class InputStreamDemo{ public static void main(String[] args) throws Exception{ FileInputStream fileInputStream = null; try{ //找到目标 File file = new File("F:\\alan.java"); //建立通道 fileInputStream = new FileInputStream(file); //读取数据 int length = 0; byte[] buf = new byte[1024]; while((length = fileInputStream.read(buf))!=-1){ System.out.print(new String(buf,0,length)); } }catch(IOException e){ System.out.println("读取文件失败"); throw new RuntimeException(e); }finally{ //关闭流 fileInputStream.close(); } } } 文件的copy实现: fileInputStream [SouthEast]: /images/20210920/931a395a38aa451daa1e997c460e28a7.png [java IO]: http://blog.csdn.net/yczz/article/details/38761237 本文地址: Java IO 讲解 相关文章 java 继承 Java基础 Java大数 JAVA继承 JAVA-继承 Java之&&、|| Java注释 Java笔记 JAVA事务 Java继承 JAVA Stream Java 算法 Java TreeMap 二分法(java) java 二分法 java 特性 Java-HelloWorld Java 字符串 Java标识符 Java注解