java I/O系统

简介: java I/O系统 I:Input 输入 O:Output 输出 二:流的分类 按照方向:输入流  输出流 按照最小单位:字节流 (byte)  字符流 (char) 三: 所有的I/O系统操作都由以下步骤构成 1)建立流 2)操作流 3)关闭流 四:文件类 java.io包中的File类提供了管理磁盘文件和目录的基本功能 File类有四种构造方法 最常用的的是  public File(URI uri)  URI是资源统一标识符 java.io.File的一些方法,主要还是要查看API文档。

java I/O系统

I:Input 输入 O:Output 输出

二:流的分类

按照方向:输入流  输出流

按照最小单位:字节流 (byte)  字符流 (char)

三:

所有的I/O系统操作都由以下步骤构成

1)建立流

2)操作流

3)关闭流

四:文件类

java.io包中的File类提供了管理磁盘文件和目录的基本功能

File类有四种构造方法

最常用的的是  public File(URI uri)  URI是资源统一标识符

java.io.File的一些方法,主要还是要查看API文档。

 1 package com.lovo;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.Date;
 6 
 7 /**
 8  * File类测试
 9  * 
10  * @author hellokitty
11  *
12  */
13 public class FileTest {
14 
15     public static void main(String[] args) {
16         // 创建File对象
17         File file = new File("E:\\jg\\exercise_bak.txt");
18 
19         // 能否读
20         System.out.println("能否读:" + file.canRead());
21 
22         // 删除
23         System.out.println("删除成功:" + file.delete());
24 
25         // 重新创建文件对象
26         file = new File("E:\\jg\\exercise_bak.txt");
27 
28         // 判断文件是否存在
29         System.out.println("是否存在:" + file.exists());
30 
31         // 目录或文件名称
32         System.out.println("名称:" + file.getName());
33 
34         // 是否目录、文件
35         System.out.println("是否目录:" + file.isDirectory());
36         System.out.println("是否文件:" + file.isFile());
37 
38         // 最后一次修改时间
39         System.out.println("最后一次修改时间:" + new Date(file.lastModified()));
40 
41         // 文件大小
42         System.out.println("文件大小:" + file.length());
43 
44         // 重新创建File对象
45         file = new File("E:\\jg");
46 
47         System.out.println("文件目录列表:");
48         // 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录
49         String[] list = file.list();
50         for (String string : list) {
51             System.out.println(string);
52         }
5354 
55         // 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件对象
56         File[] files = file.listFiles();
57         for (File item : files) {
58             if (item.isDirectory()) { // 当前File对象为目录,则遍历该目录下所有子目录与文件
59                 System.out.println(item.getName() + " 目录下子目录与文件:");
60                 String[] it = item.list();
61                 for (String i : it) {
62                     System.out.println(i);
63                 }
64                 System.out.println("*******************************");
65                 continue;
66             }
67 
68             System.out.println(item.getName() + "  文件");
69         }
70 
71         // 重新创建File对象
72         file = new File("E:\\jg\\test\\demo\\test.txt");
73         if (!file.exists()) { // 文件不存在
74             // 获取文件路径
75             File dir = file.getParentFile();
76             if (!dir.exists()) { // 目录不存在,则创建路径中所有不存在的目录
77                 dir.mkdirs();
78             }
79 
80             try {
81                 // 创建空文件
82                 System.out.println("文件是否创建成功:" + file.createNewFile());
83             } catch (IOException e) {
84                 e.printStackTrace();
85             }
86         }
87 
88     }
89 }

五:字节流 byte:用于处理二进制文件

InputStream  ----》FileInputStream    (读取)   OutputStream ---》FileOutputStream   (写入)

字节流的输入输出流代码测试

 1 package com.lovo;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.io.OutputStream;
10 
11 /**
12  * 字节输入输出流测试
13  * 
14  * @author hellokitty
15  *
16  */
17 public class IOTest {
18 
19     public static void main(String[] args) {
20         StringBuffer buffer = new StringBuffer(); // 字符串缓冲
21         
22         /* 输入流 */
23         InputStream in = null;
24 
25         try {
26             // 1. 打开输入流
27             in = new FileInputStream("E:\\jg\\exercise.txt");
28             // 2. 读取
29
30             byte[] b = new byte[1024 * 4];
31             int len = in.read(b); // 返回读取到的字节数,返回-1表示读取到流结尾
32             while(len != -1){
33                 buffer.append(new String(b, 0, len)); // 将读取到的字节解析为String追加到缓冲
34                 len = in.read(b);
35             }
36 //            System.out.println("读到" + len + "字节的数据");
37             System.out.println(buffer.toString());
38         } catch (FileNotFoundException e) {
39             e.printStackTrace();
40         } catch (IOException e) {
41             e.printStackTrace();
42         } finally {
43             // 3. 释放资源,关闭输入流
44             if (in != null){
45                 try {
46                     in.close();
47                 } catch (IOException e) {
48                     e.printStackTrace();
49                 }
50             }
51         }
52         
53         /* 输出流 */
54         OutputStream out = null;
55         
56         try {
57             File file = new File("D:\\test\\demo\\test.txt");
58             if (!file.getParentFile().exists()){ // 文件路径不存在,则创建路径中所有不存在的目录
59                 file.getParentFile().mkdirs();
60             }
61             // 1. 打开输出流
62             out = new FileOutputStream(file);
63             // 2. 写
64             out.write(buffer.toString().getBytes());
65         } catch (FileNotFoundException e) {
66             e.printStackTrace();
67         } catch (IOException e) {
68             e.printStackTrace();
69         } finally {
70             // 3. 释放输出流资源
71             if (out != null){
72                 try {
73 74                     out.close();//已经带有刷新的了
75                 } catch (IOException e) {
76                     e.printStackTrace();
77                 }
78             }
79         }
80     }
81 }

六:字符流 char 用于处理文本文件   Reader----》InputStreamReader--->FileReader       Write---》OutputStreamWrite---》FileWrite

 1 package com.lovo;
 2 
 3 import java.io.FileNotFoundException;
 4 import java.io.FileReader;
 5 import java.io.FileWriter;
 6 import java.io.IOException;
 7 import java.io.Reader;
 8 import java.io.Writer;
 9 
10 /**
11  * 字符输入输出流测试
12  * 
13  * @author14  *
15  */
16 public class IOTest2 {
17 
18     public static void main(String[] args) {
19         StringBuffer buffer = new StringBuffer();
20 
21         /* 输入流 */
22         Reader reader = null;
23 
24         try {
25             // 1. 打开流
26             reader = new FileReader("E:\\jg\\exercise.txt");
27             // 2. 读取
28             char[] ch = new char[128]; // 缓冲区
29             int len;
30             do {
31                 len = reader.read(ch);
32                 if (len == -1)
33                     break;
34                 buffer.append(new String(ch, 0, len));
35             } while (len != -1);
36             System.out.println(buffer.toString());
37         } catch (FileNotFoundException e) {
38             e.printStackTrace();
39         } catch (IOException e) {
40             e.printStackTrace();
41         } finally {
42             // 3. 释放资源
43             if (reader != null) {
44                 try {
45                     reader.close();
46                 } catch (IOException e) {
47                     e.printStackTrace();
48                 }
49             }
50         }
51 
52         /* 输出流 */
53 
54         Writer writer = null;
55 
56         try {
57             // 1. 打开流
58             writer = new FileWriter("d:\\test.txt");
59             // 2. 写入
60             writer.write(buffer.toString());
61         } catch (IOException e) {
62             e.printStackTrace();
63         } finally {
64             // 3. 释放资源
65             if (writer != null) {
66                 try {
67 68                     writer.close();
69                 } catch (IOException e) {
70                     e.printStackTrace();
71                 }
72             }
73         }
74     }
75 }

七:缓冲流

  1 package com.lovo.day2;
  2 
  3 import java.io.BufferedInputStream;
  4 import java.io.BufferedOutputStream;
  5 import java.io.File;
  6 import java.io.FileInputStream;
  7 import java.io.FileNotFoundException;
  8 import java.io.FileOutputStream;
  9 import java.io.IOException;
 10 import java.io.InputStream;
 11 import java.io.OutputStream;
 12 
 13 public class BufferedTest {
 14 
 15     public static void main(String[] args) {
 16         long start = System.currentTimeMillis();
 17         copyByBuffer("E:\\myeclipse-2015-2014-07-11-offline-installer-windows.exe", "d:\\test.exe");
 18         long end = System.currentTimeMillis();
 19         System.out.println("缓冲:" + (end - start));
 20         System.out.println("***************");
 21         start = System.currentTimeMillis();
 22         copy("E:\\myeclipse-2015-2014-07-11-offline-installer-windows.exe", "d:\\test2.exe");
 23         end = System.currentTimeMillis();
 24         System.out.println("不带缓冲:" + (end - start));
 25     }
 26     
 27     private static void copy(String source, String destination){
 28         InputStream in = null;
 29         OutputStream out = null;
 30         
 31         File file = new File(destination);
 32         if (!file.getParentFile().exists()){
 33             file.getParentFile().mkdirs();
 34         }
 35         
 36         try {
 37             // 打开流
 38             in = new FileInputStream(source);
 39             out = new FileOutputStream(file);
 40             // 操作:读写
 41             byte[] b = new byte[1024];
 42             int len;
 43             
 44             while(-1 != (len=in.read(b))){
 45                 out.write(b, 0, len);
 46             }
 47         } catch (FileNotFoundException e) {
 48             e.printStackTrace();
 49         } catch (IOException e) {
 50             e.printStackTrace();
 51         } finally {
 52             // 释放资源
 53             if (in != null){
 54                 try {
 55                     in.close();
 56                 } catch (IOException e) {
 57                     e.printStackTrace();
 58                 }
 59             }
 60             if(out != null){
 61                 try {
 62                     out.close();
 63                 } catch (IOException e) {
 64                     e.printStackTrace();
 65                 }
 66             }
 67         }
 68     }
 69     
 70     private static void copyByBuffer(String source, String destination){
 71         BufferedInputStream in = null;
 72         BufferedOutputStream out = null;
 73         
 74         try {
 75             // 打开流
 76             in = new BufferedInputStream(new FileInputStream(source), 8 * 1024 * 1024);
 77             out = new BufferedOutputStream(new FileOutputStream(destination), 8 * 1024 * 1024);
 78             // 读写
 79             byte[] b = new byte[1024];
 80             int len;
 81             
 82             while(-1 != (len = in.read(b))){
 83                 out.write(b, 0, len);
 84             }
 85         } catch (FileNotFoundException e) {
 86             e.printStackTrace();
 87         } catch (IOException e) {
 88             e.printStackTrace();
 89         } finally {
 90             // 释放资源
 91             if (in != null){
 92                 try {
 93                     in.close();
 94                 } catch (IOException e) {
 95                     e.printStackTrace();
 96                 }
 97             }
 98             if (out != null){
 99                 try {
100                     out.close();
101                 } catch (IOException e) {
102                     e.printStackTrace();
103                 }
104             }
105         }
106     } 
107 }

有缓冲的比没有缓冲要快些。

未完。。。

 

相关文章
|
10月前
|
消息中间件 Java Kafka
Java 事件驱动架构设计实战与 Kafka 生态系统组件实操全流程指南
本指南详解Java事件驱动架构与Kafka生态实操,涵盖环境搭建、事件模型定义、生产者与消费者实现、事件测试及高级特性,助你快速构建高可扩展分布式系统。
473 7
|
9月前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
数据采集 前端开发 JavaScript
产科专科电子病历系统基于Java开发,实现与HIS、LIS、PACS及区域妇幼信息平台的三级互联互通
产科专科电子病历系统基于Java开发,采用前后端分离架构(Vue+ElementUI前端,MySQL数据库),实现与HIS、LIS、PACS及区域妇幼信息平台的三级互联互通。系统涵盖患者全息视图、快速智能录入、检验检查模块、智能高危评估、异常值提醒及自我监测等功能,支持孕期时间轴和综合评估,自动归集数据并完成高危评分,助力产科数据标准化、结构化,以及临床保健工作的全程智能化管理。
351 1
|
11月前
|
存储 Java 数据库连接
java 初学者必看的系统知识结构图详解
本文详解Java知识结构图,涵盖Java语言基础、JVM原理、集合框架、并发编程、网络通信及主流框架(如Spring Boot、MyBatis),并结合学生信息管理系统实例,帮助初学者构建完整知识体系,提升实战开发能力。
330 0
|
8月前
|
移动开发 监控 小程序
java家政平台源码,家政上门清洁系统源码,数据多端互通,可直接搭建使用
一款基于Java+SpringBoot+Vue+UniApp开发的家政上门系统,支持小程序、APP、H5、公众号多端互通。涵盖用户端、技工端与管理后台,支持多城市、服务分类、在线预约、微信支付、抢单派单、技能认证、钱包提现等功能,源码开源,可直接部署使用。
614 24
|
8月前
|
设计模式 消息中间件 传感器
Java 设计模式之观察者模式:构建松耦合的事件响应系统
观察者模式是Java中常用的行为型设计模式,用于构建松耦合的事件响应系统。当一个对象状态改变时,所有依赖它的观察者将自动收到通知并更新。该模式通过抽象耦合实现发布-订阅机制,广泛应用于GUI事件处理、消息通知、数据监控等场景,具有良好的可扩展性和维护性。
620 8
|
8月前
|
安全 前端开发 Java
使用Java编写UDP协议的简易群聊系统
通过这个基础框架,你可以进一步增加更多的功能,例如用户认证、消息格式化、更复杂的客户端界面等,来丰富你的群聊系统。
308 11
|
8月前
|
机器学习/深度学习 人工智能 自然语言处理
Java与生成式AI:构建内容生成与创意辅助系统
生成式AI正在重塑内容创作、软件开发和创意设计的方式。本文深入探讨如何在Java生态中构建支持文本、图像、代码等多种生成任务的创意辅助系统。我们将完整展示集成大型生成模型(如GPT、Stable Diffusion)、处理生成任务队列、优化生成结果以及构建企业级生成式AI应用的全流程,为Java开发者提供构建下一代创意辅助系统的完整技术方案。
410 10
|
8月前
|
人工智能 监控 Java
Java与AI智能体:构建自主决策与工具调用的智能系统
随着AI智能体技术的快速发展,构建能够自主理解任务、制定计划并执行复杂操作的智能系统已成为新的技术前沿。本文深入探讨如何在Java生态中构建具备工具调用、记忆管理和自主决策能力的AI智能体系统。我们将完整展示从智能体架构设计、工具生态系统、记忆机制到多智能体协作的全流程,为Java开发者提供构建下一代自主智能系统的完整技术方案。
1046 4
|
9月前
|
NoSQL Java 关系型数据库
超全 Java 学习路线,帮你系统掌握编程的超详细 Java 学习路线
本文为超全Java学习路线,涵盖基础语法、面向对象编程、数据结构与算法、多线程、JVM原理、主流框架(如Spring Boot)、数据库(MySQL、Redis)及项目实战等内容,助力从零基础到企业级开发高手的进阶之路。
612 1