目录

概述:

1.file类

          2.IO流

一.File类

1.File类概述和构造方法

2.File类创建功能

3.File类删除功能

4.File类判断和获取功能

5.File类高级获取功能 

6.递归遍历目录

二.IO流

IO流的概述和分类

(1)Java IO 流原理

(2)IO流的分类

(一) 字节流

1.字节输入流 (FileInputStream)

(1)字节输入流读数据

(2)字节输入流读数据的3种方式

2.字节输出流 (FileOutputStream)

(1)字节输出流写数据

 (2)字节输出流写数据的3种方式

(3)字节流写数据的两个小问题 

 3.复制文件

4.字节缓冲流

(1)字节缓冲流:

(2)构造方法:

(3)小结

(二)字符流

编码表:

1.字符输入流

(1)字符流读数据的2种方式

2.字符输出流

(1)字符流写数据

(2)字符流写数据的5种方式

3.字符缓冲流

(1)字符缓冲流:

(2)构造方法:

(3)字符缓冲流特有功能

4. IO流小结

(三)特殊操作流

1.处理流

2.标准流

(1)标准输入流

(2)标准输出流

3.打印流

(1)字节打印流

(2)字符打印流【应用】

(3)复制Java文件打印流改进版

 2.转换流

 3.对象操作流

(1)对象序列化流: ObjectOutputStream

(2)对象反序列化流

(3)serialVersionUID 和 transient

 4.Properties 

(1)Properties介绍:

(2)Properties作为集合的特有方法

(3)Properties和IO流结合的方法: 


概述:

1.file类

File表示要读写的文件在哪,也可以对文件进行创建,删除等操作

在读写数据时告诉虚拟机要操作的(文件/文件夹)在哪,对(文件/文件夹)本身进行操作。包括创建,删除等。

2.IO流

IO就可以对硬盘中的文件进行读写

(1)可以将数据从本地文件中读取出来

(2)可以将数据从内存保存到本地文件

IO流ANSI文件 java java io流写文件_输入流

一.File类

1.File类概述和构造方法

File:它是文件和目录路径名的抽象表示

         文件和目录可以通过File封装成对象 File

         对于File而言,其封装的并不是一个真正存在的文件,仅仅是一个路径名而已。它可以是存在的,也可以是不存在的。将来是要通过具体的操作把这个路径的内容转换为具体存在的

方法名

说明

File(String pathname)

通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例

File(String parent, String child)

从父路径名字符串和子路径名字符串创建新的 File实例

File(File parent, String child)

从父抽象路径名和子路径名字符串创建新的 File实例

绝对路径和相对路径 

绝对路径:从盘符开始              File file1 = new File(“D:\\itheima\\a.txt”);

相对路径:相对当前项目下的路径                     File file2 = new File(“a.txt”);

                                                                           File file3 = new File(“模块名\\a.txt”);

2.File类创建功能

方法名

说明

public boolean createNewFile()

创建一个新的空的文件

public boolean mkdir()

创建一个单级文件夹

public boolean mkdirs()

创建一个多级文件夹

//方式 1 new File(String pathname)
@Test
public void create01() {
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try {
file.createNewFile();
System.out.println("文件创建成功");
} catch (IOException e) {
e.printStackTrace();
}
//方式 2 new File(File parent,String child) //根据父目录文件+子路径构建
//e:\\news2.txt
@Test
public void create02() {
File parentFile = new File("e:\\");
String fileName = "news2.txt";
//这里的 file 对象,在 java 程序中,只是一个对象
//只有执行了 createNewFile 方法,才会真正的,在磁盘创建该文件
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
//方式 3 new File(String parent,String child) //根据父目录+子路径构建
@Test
public void create03() {
//String parentPath = "e:\\";
String parentPath = "e:\\";
String fileName = "news4.txt";
File file = new File(parentPath, fileName);
try {
file.createNewFile();
System.out.println("创建成功~");
} catch (IOException e) {
e.printStackTrace();
}
}

3.File类删除功能

方法名

说明

public boolean delete()

删除由此抽象路径名表示的文件或目录

删除目录时的注意事项:

                                     delete方法直接删除不走回收站。

                                     如果删除的是一个文件,直接删除。

                                     如果删除的是一个文件夹,需要先删除文件夹中的内容,最后才能删除文件夹。 

4.File类判断和获取功能

方法名

说明

public boolean isDirectory()

测试此抽象路径名表示的File是否为目录

public boolean isFile()

测试此抽象路径名表示的File是否为文件

public boolean exists()

测试此抽象路径名表示的File是否存在

public String getAbsolutePath()

返回此抽象路径名的绝对路径名字符串

public String getPath()

将此抽象路径名转换为路径名字符串

public String getName()

返回由此抽象路径名表示的文件或目录的名称

//调用相应的方法,得到对应信息
System.out.println("文件名字=" + file.getName());
//getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
System.out.println("文件绝对路径=" + file.getAbsolutePath());
System.out.println("文件父级目录=" + file.getParent());
System.out.println("文件大小(字节)=" + file.length());
System.out.println("文件是否存在=" + file.exists());//T
System.out.println("是不是一个文件=" + file.isFile());//T
System.out.println("是不是一个目录=" + file.isDirectory());//F

5.File类高级获取功能 

方法名

说明

public File[] listFiles()

返回此抽象路径名表示的目录中的文件和目录的File对象数组

 listFiles方法注意事项:

                                 当调用者不存在时,返回null

                                 当调用者是一个文件时,返回null

                                 当调用者是一个空文件夹时,返回一个长度为0的数组

                                 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回

                                 当调用者是一个有隐藏文件的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回,包含隐藏内容

                                 当调用者是一个需要权限才能进入的文件夹时,返回null

6.递归遍历目录

public class DiGuiDemo02 {
    public static void main(String[] args) {
        //根据给定的路径创建一个File对象
//        File srcFile = new File("E:\\itcast");
        File srcFile = new File("E:\\itheima");

        //调用方法
        getAllFilePath(srcFile);
    }

    //定义一个方法,用于获取给定目录下的所有内容,参数为第1步创建的File对象
    public static void getAllFilePath(File srcFile) {
        //获取给定的File目录下所有的文件或者目录的File数组
        File[] fileArray = srcFile.listFiles();
        //遍历该File数组,得到每一个File对象
        if(fileArray != null) {
            for(File file : fileArray) {
                //判断该File对象是否是目录
                if(file.isDirectory()) {
                    //是:递归调用
                    getAllFilePath(file);
                } else {
                    //不是:获取绝对路径输出在控制台
                    System.out.println(file.getAbsolutePath());
                }
            }
        }
    }
}

二.IO流

IO流的概述和分类

(1)Java IO 流原理

IO流ANSI文件 java java io流写文件_输入流_02

 

IO流ANSI文件 java java io流写文件_IO流ANSI文件 java_03

(2)IO流的分类

IO流ANSI文件 java java io流写文件_路径名_04

IO流ANSI文件 java java io流写文件_输入流_05

 IO 流体系图-常用的类:

IO流ANSI文件 java java io流写文件_输入流_06

(一) 字节流

1.字节输入流 (FileInputStream)

(1)字节输入流读数据

       步骤: 

                a.创建字节输入流对象。

注意事项:        

                     *  如果文件不存在,就直接报错。        

                b.读数据

 注意事项:        

                       

                c.释放资源

注意事项:        

                     *  每次使用完流必须要释放资源。

(2)字节输入流读数据的3种方式

方法名

说明

void read(int b)

一次读一个字节数据

void read(byte[] b)

一次读一个字节数组数据

void read(byte[] b, int off, int len)

一次读一个字节数组的部分数据 

//* 演示 FileInputStream 的使用(字节输入流 文件--> 程序)
public void readFile01() {

String filePath = "e:\\hello.txt";

int readData = 0;
FileInputStream fileInputStream = null;
try {

//创建 FileInputStream 对象,用于读取 文件
fileInputStream = new FileInputStream(filePath);

//从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。
//如果返回-1 , 表示读取完毕

while ((readData = fileInputStream.read()) != -1) {

System.out.print((char)readData);//将码值转成 char 显示
}
} catch (IOException e) {
e.printStackTrace();
} finally {

//关闭文件流,释放资源
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 使用 read(byte[] b) 读取文件,提高效率
*/

@Test
public void readFile02() {
String filePath = "e:\\hello.txt";

//字节数组
byte[] buf = new byte[8]; //一次读取 8 个字节. int readLen = 0;

FileInputStream fileInputStream = null;

try {
//创建 FileInputStream 对象,用于读取 文件
fileInputStream = new FileInputStream(filePath);

//从该输入流读取最多 b.length 字节的数据到字节数组。 此方法将阻塞,直到某些输入可用。
//如果返回-1 , 表示读取完毕
//如果读取正常, 返回实际读取的字节数

while ((readLen = fileInputStream.read(buf)) != -1) {

System.out.print(new String(buf, 0, readLen));//显示
}
} catch (IOException e) {
e.printStackTrace();
} finally {

//关闭文件流,释放资源. try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

2.字节输出流 (FileOutputStream)

IO流ANSI文件 java java io流写文件_输入流_07

(1)字节输出流写数据

       步骤: 

                a.创建字节输出流对象。

注意事项:        

                     *  如果文件不存在,就创建。        

                     *  如果文件存在就清空。如果不想被清空则加true

                b.写数据

                  * 可以写一个字节,写一个字节数组,写一个字节数组的一部分

                  * 写一个回车换行:\r\n

 注意事项:        

                       写出的整数,实际写出的是整数在码表上对应的字母。

                c.释放资源

注意事项:        

                     *  每次使用完流必须要释放资源。

 (2)字节输出流写数据的3种方式

方法名

说明

void write(int b)

一次写一个字节数据

void write(byte[] b)

一次写一个字节数组数据

void write(byte[] b, int off, int len)

一次写一个字节数组的部分数据 

public void writeFile() {

//创建 FileOutputStream 对象
String filePath = "e:\\a.txt";
FileOutputStream fileOutputStream = null;
try {

//得到 FileOutputStream 对象 对象

//1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容
//2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面
fileOutputStream = new FileOutputStream(filePath, true);

//写入一个字节
fileOutputStream.write('H');//

//写入字符串
String str = "hsp,world!";

//str.getBytes() 可以把 字符串-> 字节数组
fileOutputStream.write(str.getBytes());

/*
write(byte[] b, int off, int len) 将 len 字节从位于偏移量 off 的指定字节数组写入此文件输出流
*/

fileOutputStream.write(str.getBytes(), 0, 3);

} catch (IOException e) {
e.printStackTrace();
} finally {

try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}

(3)字节流写数据的两个小问题 

           字节流写数据如何实现换行:

                        * 写完数据后,加换行符

                                                               windows:\r\n

                                                               linux:\n

                                                                mac:\r

           字节流写数据如何实现追加写入:

                        * public FileOutputStream(String name,boolean append)

                        * 创建文件输出流以指定的名称写入文件。如果第二个参数为true ,不会清空文件里面的内容

 3.复制文件

//完成 文件拷贝,将 e:\\Koala.jpg 拷贝 c:\\

//1. 创建文件的输入流 , 将文件读入到程序
//2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件. String srcFilePath = "e:\\Koala.jpg";

String destFilePath = "e:\\Koala3.jpg";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;

try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePath);

//定义一个字节数组,提高读取效果
byte[] buf = new byte[1024];

int readLen = 0;

while ((readLen = fileInputStream.read(buf)) != -1) {

//读取到后,就写入到文件 通过 fileOutputStream
//即,是一边读,一边写

fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法
}
System.out.println("拷贝 ok~");
} catch (IOException e) {
e.printStackTrace();
} finally {

try {
//关闭输入流和输出流,释放资源
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

4.字节缓冲流

(1)字节缓冲流:

                         BufferOutputStream:缓冲输出流

                         BufferedInputStream:缓冲输入流

(2)构造方法:

                        字节缓冲输出流:BufferedOutputStream(OutputStream out)

                        字节缓冲输入流:BufferedInputStream(InputStream in)

注意:为什么构造方法需要的是字节流, 而不是具体的文件或者路径?

           因为:字节缓冲流仅仅提供缓冲区,而真正的读写数据还得依靠基本的字节流对象进行操作

(3)小结

                  字节流:       可以操作所有类型的文件 

                  缓冲流:      可以提高读写效率

/** 演示使用 BufferedOutputStream 和 BufferedInputStream 使用
* 使用他们,可以完成二进制文件拷贝. * 思考:字节流可以操作二进制文件,可以操作文本文件吗?当然可以
*/

public class BufferedCopy02 {
public static void main(String[] args) {

String srcFilePath = "e:\\a.java";
String destFilePath = "e:\\a3.java";

//创建 BufferedOutputStream 对象 BufferedInputStream 对象
BufferedInputStream bis = null;
BufferedOutputStream bos = null;

try {

//因为 FileInputStream 是 InputStream 子类
bis = new BufferedInputStream(new FileInputStream(srcFilePath));
bos = new BufferedOutputStream(new FileOutputStream(destFilePath));

//循环的读取文件,并写入到 destFilePath
byte[] buff = new byte[1024];
int readLen = 0;

//当返回 -1 时,就表示文件读取完毕
while ((readLen = bis.read(buff)) != -1) {
bos.write(buff, 0, readLen);
}
System.out.println("文件拷贝完毕~~~");
} catch (IOException e) {
e.printStackTrace();
} finally {

//关闭流 , 关闭外层的处理流即可,底层会去关闭节点流
try {
if(bis != null) {
bis.close();
}
if(bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
     }
   }
  }
}

(二)字符流

               把文件中的数据读取到内存时,如果此时文件中出现了中文,那么字节流就会出现乱码现象( 因为字节流一次读一个字节,而不管GBK还是UTF-8一个中文都是多个字节,用字节流每次只能读其中的一部分,所以就会出现乱码问题)。所以纯文本的文件,我们就需要使用字符流来进行操作。

编码表:

(1)基础知识:

              计算机中储存的信息都是用二进制数表示的;我们在屏幕上看到的英文、汉字等字符是二进制数转换之后的结果

              按照某种规则,将字符存储到计算机中,称为编码。

              按照同样的规则,将存储在计算机中的二进制数解析显示出来,称为解码 。

              编码和解码的方式必须一致,否则会导致乱码。      

简单理解:              

              存储一个字符a,首先需在码表中查到对应的数字是97,然后按照转换成二进制的规则进行存储。                

               读取的时候,先把二进制解析出来,再转成97,通过97查找到对应的字符是a。

(2)类别:

               ASCII字符集: ASCII(American Standard Code for Information Interchange,美国信息交换标准代码):包括了数字,大小写字符和一些常见的标点符号。

               注意:ASCII码表中是没有中文的。

               GBK:window系统默认的码表。兼容ASCII码表,也包含了21003个汉字,并支持繁体汉字以及部分日韩文字。

                注意:GBK是中国的码表,一个中文以两个字节的形式存储。但不包含世界上所有国家的文字 

                Unicode码表:由国际组织ISO 制定,是统一的万国码,计算机科学领域里的一项业界标准,容纳世界上大多数国家的所有常见文字和符号。但是因为表示的字符太多,所以Unicode码表中的数字不是直接以二进制的形式存储到计算机的,会先通过UTF-7,UTF-7.5,UTF-8,UTF-16,以及 UTF-32的编码方式再存储到计算机,其中最为常见的就是UTF-8。

                注意: Unicode是万国码,以UTF-8编码后一个中文以三个字节的形式存储

IO流ANSI文件 java java io流写文件_输入流_08

 

IO流ANSI文件 java java io流写文件_java_09

 (3)字符串中的编码解码问题

         编码:

                  byte[] getBytes():使用平台的默认字符集将该 String编码为一系列字节,将结果存储到新的字节数组中

                   byte[] getBytes(String charsetName):使用指定的字符集将该 String编码为一系列字节,将结果存储到新的字节数组中

         解码:

                   String(byte[] bytes):通过使用平台的默认字符集解码指定的字节数组来构造新的 String 

                   String(byte[] bytes, String charsetName):通过指定的字符集解码指定的字节数组来构造新的 String

1.字符输入流

IO流ANSI文件 java java io流写文件_IO流ANSI文件 java_10

 FileReader 相关方法:

IO流ANSI文件 java java io流写文件_IO流ANSI文件 java_11

(1)字符流读数据的2种方式

方法名

说明

int read()

一次读一个字符数据

int read(char[] cbuf)

一次读一个字符数组数据

public class FileReader_ {
public static void main(String[] args) {
}

/**
* 单个字符读取文件
*/

@Test
public void readFile01() {
String filePath = "e:\\story.txt";
FileReader fileReader = null;
int data = 0;

//1. 创建 FileReader 对象
try {
fileReader = new FileReader(filePath);

//循环读取 使用 read, 单个字符读取
while ((data = fileReader.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
     }
   }
}



/**
* 字符数组读取文件
*/

@Test
public void readFile02() {
System.out.println("~~~readFile02 ~~~");
String filePath = "e:\\story.txt";
FileReader fileReader = null;
int readLen = 0;
char[] buf = new char[8];
try {
fileReader = new FileReader(filePath);

//循环读取 使用 read(buf), 返回的是实际读取到的字符数
//如果返回-1, 说明到文件结束

while ((readLen = fileReader.read(buf)) != -1) {
System.out.print(new String(buf, 0, readLen));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
     }
   }
  }
}

2.字符输出流

IO流ANSI文件 java java io流写文件_路径名_12

 FileWriter 常用方法:

IO流ANSI文件 java java io流写文件_路径名_13

(1)字符流写数据

           步骤:

                  创建字符输出流对象

                  注意事项:        

                                 * 如果文件不存在,就创建。但是要保证父级路径存在。        

                                 * 如果文件存在就清空。

                  写数据

                  注意事项:        

                                  * 写出int类型的整数,实际写出的是整数在码表上对应的字母。        

                                  * 写出字符串数据,是把字符串本身原样写出。

                  释放资源

                   注意事项:        

                                  * 每次使用完流必须要释放资源。

(2)字符流写数据的5种方式

方法名

说明

void write(int c)

写一个字符

void write(char[] cbuf)

写入一个字符数组

void write(char[] cbuf, int off, int len)

写入字符数组的一部分

void write(String str)

写一个字符串

void write(String str, int off, int len)

写一个字符串的一部分

方法名

说明

flush()

刷新流,还可以继续写数据

close()

关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据

public class FileWriter_ {
public static void main(String[] args) {

String filePath = "e:\\note.txt";

//创建 FileWriter 对象
FileWriter fileWriter = null;
char[] chars = {'a', 'b', 'c'};
try {

fileWriter = new FileWriter(filePath);//默认是覆盖写入

// 3) write(int):写入单个字符
fileWriter.write('H');

// 4) write(char[]):写入指定数组
fileWriter.write(chars);

// 5) write(char[],off,len):写入指定数组的指定部分
fileWriter.write("韩顺平教育".toCharArray(), 0, 3);

// 6) write(string):写入整个字符串
fileWriter.write(" 你好北京~");
fileWriter.write("风雨之后,定见彩虹");

// 7) write(string,off,len):写入字符串的指定部分
fileWriter.write("上海天津", 0, 2);

//在数据量大的情况下,可以使用循环操作. } catch (IOException e) {
e.printStackTrace();
} finally {

//对应 FileWriter , 一定要关闭流,或者 flush 才能真正的把数据写入到文件

/** private void writeBytes() throws IOException {
this.bb.flip();
int var1 = this.bb.limit();
int var2 = this.bb.position();
assert var2 <= var1;
int var3 = var2 <= var1 ? var1 - var2 : 0;
if (var3 > 0) {
if (this.ch != null) {
assert this.ch.write(this.bb) == var3 : var3;
} else {
this.out.write(this.bb.array(), this.bb.arrayOffset() + var2, var3);
}
}
this.bb.clear();
}
*/

try {
//fileWriter.flush();

//关闭文件流,等价 flush() + 关闭
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("程序结束...");
}
}

3.字符缓冲流

(1)字符缓冲流:

                    BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途                        BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途

(2)构造方法:

                         BufferedWriter(Writer out)

                         BufferedReader(Reader in)

(3)字符缓冲流特有功能

           BufferedWriter:

                                       void newLine():写一行行分隔符,行分隔符字符串由系统属性定义 

           BufferedReader:

                                      public String readLine() :读一行文字。 结果包含行的内容的字符串,不包括任何行终止字符,如果流的结尾已经到达,则为null

//演示 bufferedReader 使用
public class BufferedReader_ {
public static void main(String[] args) throws Exception {

String filePath = "e:\\a.java";

//创建 bufferedReader
BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));

//读取
String line; //按行读取, 效率高

//说明
//1. bufferedReader.readLine() 是按行读取文件
//2. 当返回 null 时,表示文件读取完毕

while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}

//关闭流, 这里注意,只需要关闭 BufferedReader ,因为底层会自动的去关闭 节点流
bufferedReader.close();
//演示 BufferedWriter 的使用

public class BufferedWriter_ {
public static void main(String[] args) throws IOException {
String filePath = "e:\\ok.txt";

//创建 BufferedWriter
//说明:
//1. new FileWriter(filePath, true) 表示以追加的方式写入
//2. new FileWriter(filePath) , 表示以覆盖的方式写入

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
bufferedWriter.write("hello, 韩顺平教育!");
bufferedWriter.newLine();//插入一个和系统相关的换行
bufferedWriter.write("hello2, 韩顺平教育!");
bufferedWriter.newLine();
bufferedWriter.write("hello3, 韩顺平教育!");
bufferedWriter.newLine();

//说明:关闭外层流即可 , 传入的 new FileWriter(filePath) ,会在底层关闭
bufferedWriter.close();
}
}

4. IO流小节

IO流ANSI文件 java java io流写文件_开发语言_14

(三)特殊操作流

   1.处理流

 (1)基本介绍

IO流ANSI文件 java java io流写文件_java_15

 (2)节点流和处理流的区别和联系

IO流ANSI文件 java java io流写文件_路径名_16

 (3)处理流的功能主要体现

IO流ANSI文件 java java io流写文件_开发语言_17

 (4)处理流分类

         字节处理流-BufferedInputStream 和 BufferedOutputStream

          字符处理流-BufferedReader 和 BufferedWriter

2.标准流

(1)标准输入流

  • System类中有两个静态的成员变量
  • public static final InputStream in:标准输入流。通常该流对应于键盘输入或由主机环境或用户指定的另一个输入源
  • 自己实现键盘录入数据
public class SystemInDemo {
    public static void main(String[] args) throws IOException {
        //public static final InputStream in:标准输入流
//        InputStream is = System.in;

//        int by;
//        while ((by=is.read())!=-1) {
//            System.out.print((char)by);
//        }

        //如何把字节流转换为字符流?用转换流
//        InputStreamReader isr = new InputStreamReader(is);
//        //使用字符流能不能够实现一次读取一行数据呢?可以
//        //但是,一次读取一行数据的方法是字符缓冲输入流的特有方法
//        BufferedReader br = new BufferedReader(isr);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("请输入一个字符串:");
        String line = br.readLine();
        System.out.println("你输入的字符串是:" + line);

        System.out.println("请输入一个整数:");
        int i = Integer.parseInt(br.readLine());
        System.out.println("你输入的整数是:" + i);

        //自己实现键盘录入数据太麻烦了,所以Java就提供了一个类供我们使用
        Scanner sc = new Scanner(System.in);
    }
}

(2)标准输出流

  • System类中有两个静态的成员变量
  • public static final PrintStream out:标准输出流。通常该流对应于显示输出或由主机环境或用户指定的另一个输出目标
  • 输出语句的本质:是一个标准的输出流
  • PrintStream ps = System.out;
  • PrintStream类有的方法,System.out都可以使用
  • 示例代码
public class SystemOutDemo {
    public static void main(String[] args) {
        //public static final PrintStream out:标准输出流
        PrintStream ps = System.out;

        //能够方便地打印各种数据值
//        ps.print("hello");
//        ps.print(100);

//        ps.println("hello");
//        ps.println(100);

        //System.out的本质是一个字节输出流
        System.out.println("hello");
        System.out.println(100);

        System.out.println();
//        System.out.print();
    }
}

3.打印流

(1)字节打印流

  • 打印流分类
  • 字节打印流:PrintStream
  • 字符打印流:PrintWriter
  • 打印流的特点
  • 只负责输出数据,不负责读取数据
  • 永远不会抛出IOException
  • 有自己的特有方法
  • 字节打印流
  • PrintStream(String fileName):使用指定的文件名创建新的打印流
  • 使用继承父类的方法写数据,查看的时候会转码;使用自己的特有方法写数据,查看的数据原样输出
  • 可以改变输出语句的目的地
    public static void setOut(PrintStream out):重新分配“标准”输出流
  • 示例代码
public class PrintStreamDemo {
    public static void main(String[] args) throws IOException {
        //PrintStream(String fileName):使用指定的文件名创建新的打印流
        PrintStream ps = new PrintStream("myOtherStream\\ps.txt");

        //写数据
        //字节输出流有的方法
//        ps.write(97);

        //使用特有方法写数据
//        ps.print(97);
//        ps.println();
//        ps.print(98);
        ps.println(97);
        ps.println(98);
        
        //释放资源
        ps.close();
    }
}

(2)字符打印流【应用】

  • 字符打印流构造房方法

方法名

说明

PrintWriter(String fileName)

使用指定的文件名创建一个新的PrintWriter,而不需要自动执行刷新

PrintWriter(Writer out, boolean autoFlush)

创建一个新的PrintWriter out:字符输出流 autoFlush: 一个布尔值,如果为真,则println , printf ,或format方法将刷新输出缓冲区

  • 示例代码
public class PrintWriterDemo {
    public static void main(String[] args) throws IOException {
        //PrintWriter(String fileName) :使用指定的文件名创建一个新的PrintWriter,而不需要自动执行行刷新
//        PrintWriter pw = new PrintWriter("myOtherStream\\pw.txt");

//        pw.write("hello");
//        pw.write("\r\n");
//        pw.flush();
//        pw.write("world");
//        pw.write("\r\n");
//        pw.flush();

//        pw.println("hello");
        /*
            pw.write("hello");
            pw.write("\r\n");
         */
//        pw.flush();
//        pw.println("world");
//        pw.flush();

        //PrintWriter(Writer out, boolean autoFlush):创建一个新的PrintWriter
        PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\pw.txt"),true);
//        PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\pw.txt"),false);

        pw.println("hello");
        /*
            pw.write("hello");
            pw.write("\r\n");
            pw.flush();
         */
        pw.println("world");

        pw.close();
    }
}

(3)复制Java文件打印流改进版

  • 案例需求
  • 把模块目录下的PrintStreamDemo.java 复制到模块目录下的 Copy.java
  • 分析步骤
  • 根据数据源创建字符输入流对象
  • 根据目的地创建字符输出流对象
  • 读写数据,复制文件
  • 释放资源
  • 代码实现
public class CopyJavaDemo {
    public static void main(String[] args) throws IOException {
        /*
        //根据数据源创建字符输入流对象
        BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java"));
        //根据目的地创建字符输出流对象
        BufferedWriter bw = new BufferedWriter(new FileWriter("myOtherStream\\Copy.java"));

        //读写数据,复制文件
        String line;
        while ((line=br.readLine())!=null) {
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //释放资源
        bw.close();
        br.close();
        */

        //根据数据源创建字符输入流对象
        BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java"));
        //根据目的地创建字符输出流对象
        PrintWriter pw = new PrintWriter(new FileWriter("myOtherStream\\Copy.java"),true);

        //读写数据,复制文件
        String line;
        while ((line=br.readLine())!=null) {
            pw.println(line);
        }

        //释放资源
        pw.close();
        br.close();
    }
}

 2.转换流

IO流ANSI文件 java java io流写文件_输入流_18

IO流ANSI文件 java java io流写文件_路径名_19

 转换流就是来进行字节流和字符流之间转换的

InputStreamReader是从字节流到字符流的桥梁

OutputStreamWriter是从字符流到字节流的桥梁

  3.对象操作流

IO流ANSI文件 java java io流写文件_IO流ANSI文件 java_20

IO流ANSI文件 java java io流写文件_java_21

  • 对象序列化介绍
  • 对象序列化:就是将对象保存到磁盘中,或者在网络中传输对象
  • 这种机制就是使用一个字节序列表示一个对象,该字节序列包含:对象的类型、对象的数据和对象中存储的属性等信息
  • 字节序列写到文件之后,相当于文件中持久保存了一个对象的信息
  • 反之,该字节序列还可以从文件中读取回来,重构对象,对它进行反序列化

(1)对象序列化流: ObjectOutputStream

  • 将Java对象的原始数据类型和图形写入OutputStream。 可以使用ObjectInputStream读取(重构)对象。 可以通过使用流的文件来实现对象的持久存储。 如果流是网络套接字流,则可以在另一个主机上或另一个进程中重构对象
  • 构造方法

方法名

说明

ObjectOutputStream(OutputStream out)

创建一个写入指定的OutputStream的ObjectOutputStream

  • 序列化对象的方法

方法名

说明

void writeObject(Object obj)

将指定的对象写入ObjectOutputStream

  • 示例代码
  • 学生类
public class Student implements Serializable {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • 测试类
public class ObjectOutputStreamDemo {
    public static void main(String[] args) throws IOException {
        //ObjectOutputStream(OutputStream out):创建一个写入指定的OutputStream的ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));

        //创建对象
        Student s = new Student("林青霞",30);

        //void writeObject(Object obj):将指定的对象写入ObjectOutputStream
        oos.writeObject(s);

        //释放资源
        oos.close();
    }
}
  • 注意事项
  • 一个对象要想被序列化,该对象所属的类必须必须实现Serializable 接口
  • Serializable是一个标记接口,实现该接口,不需要重写任何方法

     (2)对象反序列化流

  • 对象反序列化流: ObjectInputStream
  • ObjectInputStream反序列化先前使用ObjectOutputStream编写的原始数据和对象
  • 构造方法

方法名

说明

ObjectInputStream(InputStream in)

创建从指定的InputStream读取的ObjectInputStream

  • 反序列化对象的方法

方法名

说明

Object readObject()

从ObjectInputStream读取一个对象

  • 示例代码
public class ObjectInputStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //ObjectInputStream(InputStream in):创建从指定的InputStream读取的ObjectInputStream
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\oos.txt"));

        //Object readObject():从ObjectInputStream读取一个对象
        Object obj = ois.readObject();

        Student s = (Student) obj;
        System.out.println(s.getName() + "," + s.getAge());

        ois.close();
    }
}

       (3)serialVersionUID 和 transient

  • serialVersionUID
  • 用对象序列化流序列化了一个对象后,假如我们修改了对象所属的类文件,读取数据会不会出问题呢?
  • 会出问题,会抛出InvalidClassException异常
  • 如果出问题了,如何解决呢?
  • 重新序列化
  • 给对象所属的类加一个serialVersionUID
  • private static final long serialVersionUID = 42L;
  • transient
  • 如果一个对象中的某个成员变量的值不想被序列化,又该如何实现呢?
  • 给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程
  • 示例代码
  • 学生类
public class Student implements Serializable {
    private static final long serialVersionUID = 42L;
    private String name;
//    private int age;
    private transient int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

//    @Override
//    public String toString() {
//        return "Student{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                '}';
//    }
}
  • 测试类
public class ObjectStreamDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        write();
        read();
    }

    //反序列化
    private static void read() throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("myOtherStream\\oos.txt"));
        Object obj = ois.readObject();
        Student s = (Student) obj;
        System.out.println(s.getName() + "," + s.getAge());
        ois.close();
    }

    //序列化
    private static void write() throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("myOtherStream\\oos.txt"));
        Student s = new Student("林青霞", 30);
        oos.writeObject(s);
        oos.close();
    }
}

(1)对象操作流的特点:可以把对象以字节的形式写到本地文件,直接打开文件,是读不懂的,需要再次用对象操作流读到内存中。

IO流ANSI文件 java java io流写文件_java_22

(2)对象操作流分为两类:对象操作输入流和对象操作输出流

 ObjectOutputStream  对象操作输出流(对象序列化流):就是将对象写到本地文件中,或者在网络中传输对象

ObjectInputStream  对象操作输入流(对象反序列化流):把写到本地文件中的对象读到内存中,或者接收网络中传输的对象 

(3)常见问题

          a. 用对象序列化流序列化了一个对象后,修改了对象所属的Javabean类,读取数据 会出问题,会抛出InvalidClassException异常

             解决 :给对象所属的类加一个serialVersionUID

                        private static final long serialVersionUID = 42L;

      b. 如果一个对象中的某个成员变量的值不想被序列化

          解决 : 给该成员变量加transient关键字修饰,该关键字标记的成员变量不参与序列化过程

 4.Properties 

(1)Properties介绍:

  • 是一个Map体系的集合类
  • Properties可以保存到流中或从流中加载
  • 属性列表中的每个键及其对应的值都是一个字符串

IO流ANSI文件 java java io流写文件_开发语言_23

Properties基本使用

public class PropertiesDemo01 {
    public static void main(String[] args) {
        //创建集合对象
//        Properties<String,String> prop = new Properties<String,String>(); //错误
        Properties prop = new Properties();

        //存储元素
        prop.put("itheima001", "林青霞");
        prop.put("itheima002", "张曼玉");
        prop.put("itheima003", "王祖贤");

        //遍历集合
        Set<Object> keySet = prop.keySet();
        for (Object key : keySet) {
            Object value = prop.get(key);
            System.out.println(key + "," + value);
        }
    }
}

(2)Properties作为集合的特有方法

方法名

说明

Object setProperty(String key, String value)

设置集合的键和值,都是String类型,底层调用 Hashtable方法 put

String getProperty(String key)

使用此属性列表中指定的键搜索属性

Set<String> stringPropertyNames()

从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串

public class PropertiesDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        Properties prop = new Properties();

        //Object setProperty(String key, String value):设置集合的键和值,都是String类型,底层调用Hashtable方法put
        prop.setProperty("itheima001", "林青霞");
        /*
            Object setProperty(String key, String value) {
                return put(key, value);
            }

            Object put(Object key, Object value) {
                return map.put(key, value);
            }
         */
        prop.setProperty("itheima002", "张曼玉");
        prop.setProperty("itheima003", "王祖贤");

        //String getProperty(String key):使用此属性列表中指定的键搜索属性
//        System.out.println(prop.getProperty("itheima001"));
//        System.out.println(prop.getProperty("itheima0011"));

//        System.out.println(prop);

        //Set<String> stringPropertyNames():从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串
        Set<String> names = prop.stringPropertyNames();
        for (String key : names) {
//            System.out.println(key);
            String value = prop.getProperty(key);
            System.out.println(key + "," + value);
        }
    }
}

(3)Properties和IO流结合的方法: 

IO流ANSI文件 java java io流写文件_输入流_24

public class Properties01 {
public static void main(String[] args) throws IOException {

//读取 mysql.properties 文件,并得到 ip, user 和 pwd
BufferedReader br = new BufferedReader(new FileReader("src\\mysql.properties"));
String line = "";

while ((line = br.readLine()) != null) { //循环读取
String[] split = line.split("=");

//如果我们要求指定的 ip 值
if("ip".equals(split[0])) {

System.out.println(split[0] + "值是: " + split[1]);
}
}
br.close();
 }
}

方法名

说明

void load(InputStream inStream)

从输入字节流读取属性列表(键和元素对)

void load(Reader reader)

从输入字符流读取属性列表(键和元素对)

void store(OutputStream out, String comments)

将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流

void store(Writer writer, String comments)

将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流

public class Properties {
public static void main(String[] args) throws IOException {

//使用 Properties 类来读取 mysql.properties 文件
//1. 创建 Properties 对象
Properties properties = new Properties();

//2. 加载指定配置文件
properties.load(new FileReader("src\\mysql.properties"));

//3. 把 k-v 显示控制台
properties.list(System.out);

//4. 根据 key 获取对应的值
String user = properties.getProperty("user");
String pwd = properties.getProperty("pwd");
System.out.println("用户名=" + user);
System.out.println("密码是=" + pwd);
}
}


public class Properties03 {
public static void main(String[] args) throws IOException {

//使用 Properties 类来创建 配置文件, 修改配置文件内容
Properties properties = new Properties();

//创建
//1.如果该文件没有 key 就是创建
//2.如果该文件有 key ,就是修改

properties.setProperty("charset", "utf8");
properties.setProperty("user", "汤姆");//注意保存时,是中文的 unicode 码值
properties.setProperty("pwd", "888888");

//将 k-v 存储文件中即可
properties.store(new FileOutputStream("src\\mysql2.properties"), null);
System.out.println("保存配置文件成功~");
}