Java基础(十二)IO输入输出

一、IO 概述

1、IO 概念

  IO:I 代表 Input 输入;O 代表 Output 输出。

  Java 中 IO 是以流为基础进行输入输出,所有的数据被串行化(保存)写入输出流,或者从输入流读入。

  注:数据串行化把对象的状态以特定的形式(比如 byte[]保存到通过流的方式写入

2、IO 的作用

  1、文本文件,通过特定方法能够把数据写到文件,也能够读取出文件中的内容。

  2、把信息保存到磁盘文件中。

3、Java 操作文件

1、创建 File 对象方式

  测试创建文件的三种方式:

 1 import org.junit.Test;
 2 
 3 import java.io.File;
 4 
 5 /**
 6  * @author zt1994
 7  * @date 2018/3/2 10:56
 8  */
 9 public class CreateFile {
10     
11     /**
12     * 测试创建文件的三种方式,仅仅在程序中创建File对象
13     */
14     @Test
15     public void testCreateFile(){
16         //1、File(String pathname) 通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
17         File file = new File("test1.txt");  //相对路径(相对于当前工程)
18         File file1 = new File("D:\file\test1.txt");//绝对路径
19 
20         //2、File(String parent, String child) 根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
21         File file2 = new File("D:\file\", "test2.txt");
22 
23         //3、File(File parent, String child) 根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
24         File file3 = new File(new File("D:\file\"), "test3.txt");
25 
26         System.out.println(file);  //test1.txt
27         System.out.println(file1);  //D:file	est1.txt
28         System.out.println(file2);  //D:file	est2.txt
29         System.out.println(file3);  //D:file	est3.txt
30     }
31 }

2、创建删除文件和文件夹

1、创建文件

 1 import org.junit.Test;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 /**
 7  * @author zt1994
 8  * @date 2018/3/2 11:16
 9  */
10 public class CreateNewFile {
11     
12     /**
13      * 在磁盘上面创建文件
14      * 1、创建文件的路径必须存在,否则抛出异常
15      * 2、如果文件已经存在,返回false
16      * 3、此方法只能创建文件,不能创建文件夹
17      */
18     @Test
19     public void testCreateNewFile() throws IOException {
20         File file1 = new File("D:\test.txt");//绝对路径
21         boolean b = file1.createNewFile();
22         System.out.println(b);
23     }
24 }

 2、创建和删除文件夹

  测试创建和删除文件夹:

 1 import java.io.File;
 2 
 3 /**
 4  * @author zt1994
 5  * @date 2018/3/2 11:27
 6  */
 7 public class CreateDir {
 8     /**
 9      * 测试和删除创建文件夹
10      */
11     @Test
12     public void testMakeDir(){
13         //创建文件夹
14         File dir = new File("E:\testIo");
15         System.out.println(dir);
16         //1、创建单层文件夹
17         boolean mkdir = dir.mkdir();
18         System.out.println(mkdir);
19 
20         //2、创建多层文件夹
21         File dirs = new File("E:\Demo\test\file");
22         boolean mkdirs = dirs.mkdirs();
23         System.out.println(mkdirs);
24         boolean deleteDirs = dirs.delete();  //删除
25         System.out.println(deleteDirs);
26 
27         //删除文件夹 public boolean delete()
28         boolean delete = dir.delete();
29         System.out.println(delete);
30     }
31 }

3、其他常用方法

  boolean exists() 测试文件或者目录是否存在;

  String getName() 得到文件或者目录的名称;

  String getParent() 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。

  boolean isFile() 测试是否是一个文件;

  boolean isDirectory 测试是否一个目录;

3、删除文件夹目录的全部内容(递归删除)

 1 import org.junit.Test;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 public class TestDeleteAll {
 7     /**
 8      * 删除文件夹内所有文件,递归删除
 9      */
10     @Test
11     public void testDeleteAll(){
12         //创建一个多层文件夹
13         File file = new File("E:\Demo\test\file");
14         file.mkdirs();
15 
16         File file1 = new File("E:\Demo");
17         deleteAll(file1);
18 
19     }
20 
21     //删除指定目录下所有文件
22     public static void deleteAll(File file){
23         if (file.isFile() || file.list().length==0){
24             file.delete();
25         }else {
26             File[] files = file.listFiles();
27             for (File f:files){
28                 deleteAll(f);  //调用方法自身
29                 f.delete();
30             }
31         }
32     }
33 }

 二、IO 流读写文件

1、IO 流分类

1、按流动方向分类

  输入流和输出流,流动方向是相对的。

2、按数据的单位分类

  字节流和字符流。

  测试 IO 流代码:

  1 import org.junit.Test;
  2 
  3 import java.io.*;
  4 
  5 /**
  6  * 输出流和输入流程测试
  7  */
  8 public class TestIoStream {
  9     /**
 10      * 1.读取单个字符和字符转换
 11      */
 12     @Test
 13     public void test1() throws IOException {
 14         //创建文件对象
 15         File file = new File("f1.txt");
 16 
 17         FileInputStream fileInputStream = new FileInputStream(file);
 18 
 19         int i = fileInputStream.read();
 20         System.out.println(i);
 21 
 22         //字符编码转换
 23         char x = (char) i;
 24         System.out.println(x);
 25     }
 26 
 27 
 28     /**
 29      * 2.读取多个字符
 30      */
 31     @Test
 32     public void test2() throws Exception {
 33         File file = new File("f1.txt");
 34         FileInputStream fileInputStream = new FileInputStream(file);
 35 
 36         byte[] fs = new byte[(int) file.length()];
 37         while (fileInputStream.read(fs) != -1){
 38             System.out.println(new String(fs));
 39             for (byte b: fs){
 40                 System.out.print((char) b);
 41             }
 42         }
 43 
 44         fileInputStream.close();
 45     }
 46 
 47 
 48     /**
 49      * 3.输出流,会覆盖
 50      * @throws IOException
 51      */
 52     @Test
 53     public void test3() throws IOException {
 54         File file = new File("f1.txt");
 55 
 56         FileOutputStream fileOutputStream = new FileOutputStream(file);
 57 
 58         String str = "你好,世界!";
 59 
 60         //获取字符串数组对象
 61         byte[] bytes = str.getBytes();
 62 
 63         fileOutputStream.write(bytes);
 64         fileOutputStream.flush();
 65         fileOutputStream.close();
 66     }
 67 
 68     /**
 69      * 4.字符流输入
 70      */
 71     @Test
 72     public void test4() throws IOException {
 73         FileReader fileReader = new FileReader("f1.txt");
 74 
 75         boolean f = true;
 76         while(f){
 77 
 78             int read = fileReader.read();
 79             System.out.print((char) read);
 80             if (read == -1){
 81                 f = false;
 82             }
 83         }
 84     }
 85 
 86 
 87     /**
 88      * 5.字符流输出
 89      */
 90     @Test
 91     public void test5() throws IOException {
 92         FileWriter fileWriter = new FileWriter("f1.txt");
 93 
 94         fileWriter.write("方便的输出");
 95 
 96         fileWriter.close();
 97     }
 98 
 99 
100     /**
101      * 6.字节流转字符流
102      */
103     @Test
104     public void test6() throws IOException {
105         FileInputStream inputStream = new FileInputStream("f1.txt");
106         InputStreamReader reader = new InputStreamReader(inputStream, "utf-8");
107 
108         boolean f = true;
109         while (f){
110             int i = reader.read();
111             System.out.print((char) i);
112             if (i == -1){
113                 f = false;
114             }
115         }
116     }
117 }

3、字节流和字符流的区别

①操作的单位不一样,一个是字节,一个是字符;

②操作中文使用字符流很爽;

③字符流的输出流可以直接写一个字符串 write(String msg);

④执行流程:

  字节输出流  --》程序 --》文件

  字符输出流  --》程序 --》缓存 ---》文件

测试:

      A:字节流调用了写数据的方法之后如果没有关闭,还是会把数据写到文件;

      B:字符流调用了写数据的方法之后如果没有关闭,不会把数据写到文件;

原文地址:https://www.cnblogs.com/zt19994/p/8489598.html