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 }

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

未完。。。

 

相关文章
|
3天前
|
JavaScript Java BI
Java毕设之新生报到系统的设计与实现
Java毕设之新生报到系统的设计与实现
11 3
|
1天前
|
数据采集 前端开发 Java
Java医院绩效考核系统源码maven+Visual Studio Code一体化人力资源saas平台系统源码
医院绩效解决方案包括医院绩效管理(BSC)、综合奖金核算(RBRVS),涵盖从绩效方案的咨询与定制、数据采集、绩效考核及反馈、绩效奖金核算到科到组、分配到员工个人全流程绩效管理;将医院、科室、医护人员利益绑定;全面激活人才活力;兼顾质量和效益、长期与短期利益;助力医院降本增效,持续改善、优化收入、成本结构。
4 0
|
1天前
|
Java
排课系统【JSP+Servlet+JavaBean】(Java课设)
排课系统【JSP+Servlet+JavaBean】(Java课设)
13 5
|
1天前
|
存储 Java API
java对接IPFS系统-以nft.storage为列
java对接IPFS系统-以nft.storage为列
11 2
|
2天前
|
Java
【JAVA基础篇教学】第十三篇:Java中I/O和文件操作
【JAVA基础篇教学】第十三篇:Java中I/O和文件操作
|
2天前
|
监控 前端开发 Java
Java基于B/S医院绩效考核管理平台系统源码 医院智慧绩效管理系统源码
医院绩效考核系统是一个关键的管理工具,旨在评估和优化医院内部各部门、科室和员工的绩效。一个有效的绩效考核系统不仅能帮助医院实现其战略目标,还能提升医疗服务质量,增强患者满意度,并促进员工的专业成长
9 0
|
2天前
|
Java 云计算
Java智能区域医院云HIS系统SaaS源码
云HIS提供标准化、信息化、可共享的医疗信息管理系统,实现医患事务管理和临床诊疗管理等标准医疗管理信息系统的功能。优化就医、管理流程,提升患者满意度、基层首诊率,通过信息共享、辅助诊疗等手段,提高基层医生的服务能力构建和谐的基层医患关系。
16 2
|
3天前
|
前端开发 Java 关系型数据库
Java医院绩效考核系统源码B/S架构+springboot三级公立医院绩效考核系统源码 医院综合绩效核算系统源码
作为医院用综合绩效核算系统,系统需要和his系统进行对接,按照设定周期,从his系统获取医院科室和医生、护士、其他人员工作量,对没有录入信息化系统的工作量,绩效考核系统设有手工录入功能(可以批量导入),对获取的数据系统按照设定的公式进行汇算,且设置审核机制,可以退回修正,系统功能强大,完全模拟医院实际绩效核算过程,且每步核算都可以进行调整和参数设置,能适应医院多种绩效核算方式。
22 2
|
3天前
|
JavaScript 小程序 Java
基于java的少儿编程网上报名系统
基于java的少儿编程网上报名系统
11 2
|
3天前
|
JavaScript 小程序 Java
Java毕设之在线医疗服务系统的设计与实现
Java毕设之在线医疗服务系统的设计与实现
8 3