栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Java高级】IO流概述

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Java高级】IO流概述

目录

01 File类的使用

1.1 创建File实例

1.2 File类的常用方法

1.2.1 File类的获取功能

1.2.2 重命名功能  

1.2.3 File类的判断功能

1.2.4 File类文件删除和创建功能

1.3 课后练习

02 IO流原理及流的分类

2.1 Java IO原理

2.2 流的分类

2.3 IO流体系

03 节点流(文件流)

3.1 FileReader 和 FileWriter(char[] 处理文本文件)

3.1.1  FileReader中使用read(char[] cbuf)读入数据

3.1.2 FileWriter写出数据的操作

 3.1.3 使用FileReader和FileWriter实现文本文件的复制

3.2 FileInputStream 和FileOutputStream(byte[] 处理非文本文件)

 3.2.1 处理非文本文件会有乱码

3.2.2 读写非文本文件(.doc.jpg.mp3)

3.2.3 实现指定路径下文件的复制

04 缓冲流

4.1 缓冲流实现非文本文件的复制

4.2 缓冲流(字符型)实现文本文件的复制

4.3 缓冲流练习

05 转换流

5.1 转换流概述

5.2 转换流实现文件的读入和写出

5.3 补充: 字符编码

5.3.1 常见编码

5.3.2 unicode转换 

06 标准输入输出流

 07 打印流

08 数据流

09 对象流(掌握)

9.1 对象流序列化和反序列化操作

 9.1.1 对象的序列化

 9.1.2 对象的反序列化

 9.2 自定义类实现序列化与反序列化操作

 9.3 全局常量serialVersionUID的理解

 10 NIO.2中Path、Paths、Files类的使用


01 File类的使用

java .io.File 类: 文件 和文件目录路径 的抽象表示形式 ,与平台无关File 能新建、删除、重命名文件和目录,但 File 不能访问文件内容本身。如果需要访问文件内容本身,则需要使用输入输出 流 。想 要在 Java 程序中表示一个真实存在的文件或目录,那么必须有一个 File 对象 ,但是 Java 程序中的一个 File 对象,可能没有一个真实存在的文件或目录 。File 对象可以作为参数传递给流的构造器

1.1 创建File实例
1如何实例化File实例: 调用构造器
File(String filePath)
File(String parentPath,String childPath)
File(File parentFile,String childPath)
@Test
    public void test1(){
        // 构造器1  File(String filePath)
        File file = new File("hello.txt"); //相对于当前modul
        File file1 = new File("G:\Java_study\Java_Gaoji\IOStream\he.txt");// 决对路径

        System.out.println(file);
        System.out.println(file1); // 内存层面的对象

        // 构造器2  File(String parentPath,String childPath)
        File file2 = new File(""G:\\Java_study","Java_Gaoji");
        System.out.println(file2);

        // 构造器3 File(File parentFile, String childPath)
        File file3 = new File(file2,"hi.txt");
        System.out.println(file3);


    }

注意:路径分隔符

      windows:\      unix:/

     Java程序支持跨平台运行,因此路径分隔符要慎用。

     为了解决这个隐患,File类提供了一个常量:

        public  static final String separator。
        根据操作系统,动态的提供分隔符。

   File file1= new File("d:\Work\info.txt");
      File file2= new File("d:"+ File.separator+ "Work"+ File.separator+ "info.txt");
      File file3= new File("d:/Work");

1.2 File类的常用方法

1.2.1 File类的获取功能
     public String getAbsolutePath():获取绝对路径 public String getPath() :获取路径public String getName() :获取名称public String getParent():获取上层文件目录路径。若无,返回null public long length() :获取文件长度(即:字节数)。不能获取目录的长度。public long lastModified() :获取最后一次的修改时间,毫秒值
@Test
    // File类的常用方法,获取功能
    public void test2(){

        File file = new File("hello.txt");
        File file1 = new File("G:\Java_study\Java_Gaoji\IOStream\he.txt");

        System.out.println(file.getAbsoluteFile());// G:Java_studyJava_GaojiIOStreamhello.txt
        System.out.println(file.getPath());//hello.txt
        System.out.println(file.getName());//hello.txt
        System.out.println(file.getParent());//null
        System.out.println(file.length()); //未定义时: 0 、有文件时:输出文件长度
        System.out.println(file.lastModified());//0 、 1646169028263

        System.out.println();

        System.out.println(file1.getAbsoluteFile());//G:Java_studyJava_GaojiIOStreamhe.txt
        System.out.println(file1.getPath());// G:Java_studyJava_GaojiIOStreamhe.txt
        System.out.println(file1.getName());//he.txt
        System.out.println(file1.getParent());//G:Java_studyJava_GaojiIOStream
        System.out.println(file1.length()); //0
        System.out.println(file1.lastModified());//0


    }

     如下的两个方法适用于文件目录:
     public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
     public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组

@Test
    // File类的常用方法
    public void test3(){
        File file = new File("G:\Java_study"); // 要求文件存在!!!
        String[] list = file.list(); //获取指定目录下的所有文件或文件目录的名称数组
        for(String s : list){
            System.out.println(s);
        }

        File[] list2 =  file.listFiles();
        for(File f : list2){
            System.out.println(f);
        }
    }

1.2.2 重命名功能  

public boolean renameTo(File dest):把文件重命名为指定的文件路径
       比如:file1.renameTo(file2)为例:
           要想保证返回true,需要file1在硬盘中是存在的,且file2不能在硬盘中存在。

public void test4(){
        File file = new File("hello.txt");
        File file2 = new File("G:\Java_study\Java_Gaoji\IOStream\he.txt");

        boolean rename = file.renameTo(file2); //想要返回true,要保证file必须存在,且file2不存在
        System.out.println(rename); //返回的是布尔型变量
    }

1.2.3 File类的判断功能
    public boolean isDirectory():判断是否是文件目录public boolean isFile() :判断是否是文件public boolean exists() :判断是否存在public boolean canRead() :判断是否可读public boolean canWrite() :判断是否可写public boolean isHidden() :判断是否隐藏
@Test
    public void test5(){
        File file = new File("he.txt"); //一个文件,相对路径,四那个对于当前Module
        System.out.println(file.isFile());  //判断是不是文件 true
        System.out.println(file.isDirectory());//判断是不是文件夹 false
        System.out.println(file.exists());//判断是不是存在 true
        System.out.println(file.canRead());//判断是不是可读 true
        System.out.println(file.canWrite());//判断是不是可写 true
        System.out.println(file.isHidden());//判断是不是隐藏 false
    }

1.2.4 File类文件删除和创建功能

 创建硬盘中对应的文件
     public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
 删除磁盘中的文件或文件目录
     public boolean delete():删除文件或者文件夹
     *    删除注意事项:Java中的删除不走回收站。要想删除成功,该文件目录下不能有子文件或文件目录

  // File类的文件创建和删除功能
    @Test
    public void test6() throws IOException {
        File file = new File("hi.txt"); //目前不存在
        if (!file.exists()){
            file.createNewFile();
            System.out.println("创建成功");
        }else { //文件存在
            file.delete();
            System.out.println("删除成功");
        }
    }

创建硬盘中的文件夹

public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。如果此文件目录的上层目录不存在,也不创建。
public boolean mkdirs() :创建文件目录。如果此文件目录存在,就不创建了。如果上层文件目录不存在,一并创建 

@Test
    // 文件夹的创建
    // file.mkdir(): 当file文件上层目录不存在时,就不创建
    //file.mkdirs(): 当file文件上层目录不存在时,会连同上层目录一并创建
    public void test7(){
        File file = new File("G:\Java_study\Java_Gaoji\IOStream\IO1"); // IO1上层存在
        boolean mkdir = file.mkdir();
        if(mkdir == true){
            System.out.println("创建成功");
        }
        File file2 = new File("G:\Java_study\Java_Gaoji\IOStream\IO2");
        boolean mkdir2 = file2.mkdirs();
        if(mkdir2){
            System.out.println("创建成功2");
        }
   //要想删除成功,io4文件目录下不能有子目录或文件
        File file3 = new File("D:\io\io1\io4");
        file3 = new File("D:\io\io1");
        System.out.println(file3.delete());
    }
}

1.3 课后练习

public class FileTest {
    @Test
    public void test() throws IOException {
        File file = new File("D:\io\io1\hello.txt");
        //创建一个与file同目录下的另外一个文件,文件名为:hi.txt
        File destFile = new File(file.getParent(),"i.txt");
        boolean newFile = destFile.createNewFile();
        if(newFile){
            System.out.println("创建成功!");
        }
    }
}

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
//判断 指定目录下是否有后缀名为 .jpg 的文件,如果有,就输出该文件 名称

public class FileCondtructor {
    public static void main(String[] args) throws IOException {
        File file = new File("G:\Java_study\Java_Gaoji\IOStream");//确定指定文件目录
        //方式一
        String[] list = file.list();
        for (String s: list){
            if(s.endsWith(".jpg")){
                System.out.println(s);
            }
        }

        // 方法二
        File[] files = file.listFiles();
        for(File f:files){
            if(f.getName().endsWith(".jpg")){
                System.out.println(f.getAbsolutePath());
            }
        }

        // 方法三
        
        File[] subFiles = file.listFiles(new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".jpg");
            }
        });

        for(File f : subFiles){
            System.out.println(f.getAbsolutePath());
        }
    }

    }

public class fILE {
    public static void main(String[] args) {
        File file = new File("G:\Java_study\Java_Gaoji\IOStream");
        printSubFile(file);
        System.out.println(getFileLength(file));
    }

    public static void printSubFile(File file) {
        File[] list = file.listFiles();
        for (File f : list) {
            if (f.isDirectory()) {
                printSubFile(f);
            } else {
                System.out.println(f);
            }
        }
    }

    // 计算指定目录占用空间大小
    public static long getFileLength(File file) {
        File[] list = file.listFiles();
        long size = 0;
        for (File f : list) {
            if (f.isDirectory()) {
                getFileLength(f);
            } else {
                size += f.length();
            }
        }
        return size;
    }

02 IO流原理及流的分类

2.1 Java IO原理

I/O是Input/Output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络通讯等。Java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出数据。输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中。

2.2 流的分类

按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)按数据流的流向不同分为:输入流,输出流按流的角色的不同分为:节点流,处理流

2.3 IO流体系

03 节点流(文件流)

3.1 FileReader 和 FileWriter(char[] 处理文本文件)

读取文件:

1. 建立一个流对象,将已存在的文件加载进流

FileReader fr= new FileReader(new File(“Test.txt”));

2.  创建一个临时存放数据的数组。

char[] ch= new char[1024];

 3. 调用流对象的读取方法将流中的数据读入到数组中。

fr.read(ch);

4. 关闭资源。

	fr.close();
@Test
    
    public void testFileReader() throws IOException { //try-catch更好
        FileReader fr = null;
        try {
            // 1.实例化file对象,指明要操作的文件
            File file = new File("hello.txt"); //相较于当前module,直接放到当前module目录下
            //2. 提供具体的流
            fr = new FileReader(file);  // 可能会报文件找不到异常
            //3.数据读入
            int data = fr.read(); //返回读入的字符的数值,当返回-1时,读取完毕
            while (data != -1) {  //循环读入
                System.out.print((char) data);
                data = fr.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 流的关闭
            fr.close();
        }

    }

3.1.1  FileReader中使用read(char[] cbuf)读入数据
 @Test
    
    public void testFileReader1() throws IOException {
        // 1 file类实例化
        File file = new File("hello.txt");
        // 2 Reader流类实例化
        FileReader fr = new FileReader(file);
        // 3.读入操作细节
        // read(char[] cbuf) 返回每次读入cbuf数组中字符的个数,达到文件末尾返回-1
        char[] cbuf = new char[5];
        int len;
       len = fr.read(cbuf);  // 读入数组,每次读入字符个数
        while(len != -1){
           // for(int i = 0; i < cbuf.length ; i++){ //这里不能写cbuf.length,//helloworld123ld
            // 方式一:
            for(int i = 0; i < len ; i++){
                System.out.print(cbuf[i]);
            }
            len = fr.read(cbuf);
        }

        // 方式二:
        while(len != -1){
            // String str = new String(cbuf);// char转写成String
            String str = new String(cbuf,0,len); //取数组中的len个
        }
        // 4.流资源关闭
        fr.close();


    }

3.1.2 FileWriter写出数据的操作

 写入文件

 @Test
    
    public void testWriter() throws IOException{
        // 1. 提供File类,指明写出到的文件
        File file = new File("hello1.txt");

        // 2. 提供FileWriter类,将要写出的文件作为参数传入,用于数据写出
        FileWriter fw = new FileWriter(file);
        // 3. 写出流的操作
        fw.write("I have a dream".toCharArray());
        fw.write("you need to have a dream");
        // 4. 关闭流
        fw.close();

    }

 3.1.3 使用FileReader和FileWriter实现文本文件的复制
   @Test
    public void testFileReaderFileWriter() throws IOException {
        // 创建File类对象,指明读入和写出的文件
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File fileReader = new File("hello.txt");
            File fileWriter = new File("hello1.txt");

            // 创建读入流和输出流
            fr = new FileReader(fileReader);
            fw = new FileWriter(fileWriter);

            // 读入流输出流的具体操作
            //读入
            char[] cubf = new char[5];
            int len = fr.read(cubf); //从hello读入
            while(len != -1){
                fw.write(cubf,0,len); //写出到hello1
                len = fr.read(cubf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 流的关闭,首先要判断是否有流
            if(fw != null){
                fw.close();
            }
            if (fr != null){
                fr.close();
            }

        }



    }

3.2 FileInputStream 和FileOutputStream(byte[] 处理非文本文件)

 3.2.1 处理非文本文件会有乱码
public class InputOutPut {
    
    @Test
    public void testFileInputStream() throws IOException {
        FileInputStream fis = null;
        try {
            File file = new File("hello.txt");

            fis = new FileInputStream(file);

            byte[] buffer = new byte[5];
            int len = fis.read(buffer);
            while(len != -1){
                String str = new String(buffer,0,len);
                System.out.println(str);
                len = fis.read(buffer);  //英文字符还是用字节存储的,所以不会乱码,如果是中文(三个字节存储),会有乱码
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fis.close();
        }

3.2.2 读写非文本文件(.doc.jpg.mp3)
 @Test
    // 处理非文本文件,实现对图片的复制
    public void testFileInputStream1() throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File("1 Achse.jpg");
            File drcFile = new File("CopyOf1Achse.jpg");

            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(drcFile);

            byte[] buffer = new byte[5];
            int len = fis.read(buffer);

            while (len != -1) {
                fos.write(buffer, 0, len);
                len = fis.read(buffer);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fis.close();
            fos.close();
        }

    }

3.2.3 实现指定路径下文件的复制
// 定义方法,实现指定路径下文件的复制
    public void copyFile(String srcPath, String destPath) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File srcFile = new File(srcPath);
            File drcFile = new File(destPath);

            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(drcFile);

            byte[] buffer = new byte[5];
            int len = fis.read(buffer);
            while (len != -1) {
                fos.write(buffer, 0, len);
                len = fis.read(buffer);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fis.close();
            fos.close();
        }
    }

04 缓冲流

为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组,缺省使用8192个字节(8Kb)的缓冲区。

缓冲流要“套接”在相应的节点流之上,根据数据操作单位可以把缓冲流分为:

BufferedInputStream和BufferedOutputStreamBufferedReader和BufferedWriter当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从文件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组。向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里。使用方法flush()可以强制将缓冲区的内容全部写入输出流关闭流的顺序和打开流的顺序相反。只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流flush()方法的使用:手动将buffer中内容写入文件如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出。

4.1 缓冲流实现非文本文件的复制
public class BufferedTest {
    @Test
    public void testBufferedInputOutput() throws IOException {
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            // 1 造文件
            File srcFile = new File("1 Achse.jpg");
            File destFile = new File("copy2.jpg");

            // 2 造流
            // 2.1 两个文件流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream fos = new FileOutputStream(destFile);
            // 2.2 两个处理流
            bis = new BufferedInputStream(fis); // 将文件流作为参数传递给处理流
            bos = new BufferedOutputStream(fos);

            // 3 文件的读取写入
            byte[] buffer = new byte[10];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }

            // 关闭流
            // 要求: 先关闭外层流,再关闭内层流

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bos.close();
            bis.close();
        }
        // 说明: 关闭外层流的同时,内层流也会关闭,
        //fos.close();
        //fis.close();
    }

4.2 缓冲流(字符型)实现文本文件的复制
@Test
    
    public void testBufferedReaderWriter() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File("hello.txt")));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("Hello2.txt")));

        // 读写操作
        char[] cubf =new char[6];
        int len = br.read(cubf);
        while(len != -1){
            bw.write(cubf,0,len);
            len = br.read(cubf);
        }
        // 关闭资源
        bw.close();
        br.close();


    }

4.3 缓冲流练习

 @Test
    // 图片的加密
    public void testFile() throws IOException {
        FileInputStream fis = new FileInputStream(new File("copy2.jpg"));
        FileOutputStream fos = new FileOutputStream("JIaMi.jpg");

        byte[] buffer = new byte[10];
        int len;
        while((len = fis.read(buffer)) != -1){
            // 对所有字节进行修改,需要遍历数组中数值进行异或操作
            for(int i = 0; i < len; i++){
                buffer[i] = (byte)(buffer[i] ^ 5); // 需要强转,因为5是int,而得到数值需要是byte
            }
            fos.write(buffer,0,len);
        }
        fos.close();
        fis.close();
    }

import org.junit.Test;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


public class WordCount {

  
    @Test
    public void testWordCount() {
        FileReader fr = null;
        BufferedWriter bw = null;
        try {
            //1.创建Map集合
            Map map = new HashMap(); //用于存放数据次数

            //2.遍历每一个字符,每一个字符出现的次数放到map中
            fr = new FileReader("dbcp.txt");
            int c = 0;
            while ((c = fr.read()) != -1) {
                //int 还原 char
                char ch = (char) c;   //文件读入返回时 interesting型,需要解码成char型
                // 判断char是否在map中第一次出现
                if (map.get(ch) == null) {
                    map.put(ch, 1);
                } else {
                    map.put(ch, map.get(ch) + 1);
                }
            }

            //3.把map中数据存在文件count.txt
            //3.1 创建Writer
            bw = new BufferedWriter(new FileWriter("wordcount.txt"));

            //3.2 遍历map,再写入数据
            Set> entrySet = map.entrySet();
            for (Map.Entry entry : entrySet) {
                switch (entry.getKey()) {
                    case ' ':
                        bw.write("空格=" + entry.getValue());
                        break;
                    case 't'://t表示tab 键字符
                        bw.write("tab键=" + entry.getValue());
                        break;
                    case 'r'://
                        bw.write("回车=" + entry.getValue());
                        break;
                    case 'n'://
                        bw.write("换行=" + entry.getValue());
                        break;
                    default:
                        bw.write(entry.getKey() + "=" + entry.getValue());
                        break;
                }
                bw.newline();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关流
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

05 转换流

5.1 转换流概述

转换流提供了在字节流和字符流之间的转换Java API提供了两个转换流:InputStreamReader:将InputStream转换为Reader

实现将字节的输入流按指定字符集转换为字符的输入流。需要和InputStream“套接”。构造器

public InputStreamReader(InputStream in)public InputSreamReader(InputStream in,String charsetName)如:Reader isr= new InputStreamReader(System.in,”gbk”);(指定字符集)OutputStreamWriter:将Writer转换为OutputStream

实现将字符的输出流按指定字符集转换为字节的输出流。需要和OutputStream“套接”。构造器

public OutputStreamWriter(OutputStream out)public OutputSreamWriter(OutputStream out,String charsetName)字节流中的数据都是字符时,转成字符流操作更高效。很多时候我们使用转换流来处理文件乱码问题。实现编码和解码的功能。

public class InputOutputStreamReaderWriter {
    @Test
    public void test() throws IOException {
        // 实现字节输入流到字符输入流的转化
        FileInputStream fis = new FileInputStream("hello.txt");
        // 参数2指明了字符集,具体使用哪个字符集,取决于hello.txt保存时使用了哪个字符集
        InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); // 不写字符集的话使用默认字符集

        char[] cubf = new char[5];
        int len;
        while ((len = isr.read(cubf)) != -1){
            String str = new String(cubf,0,len);
            System.out.println(str);
        }

    }

5.2 转换流实现文件的读入和写出
@Test
    // 综合使用 InputStreamReader 和OutputStreamWriter
    public void test1() throws IOException {
        File file1 = new File("hello.txt");
        File file2 = new File("hello_gbk.txt");

        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream fos = new FileOutputStream(file2);

        InputStreamReader isr = new InputStreamReader(fis,"utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");

        // 读写过程
        char[] cubf = new char[4];
        int len;
        while((len = isr.read(cubf)) != -1){
            osw.write(cubf,0,len);
        }

        isr.close();
        osw.close();

    }

5.3 补充: 字符编码

5.3.1 常见编码
 

5.3.2 unicode转换 

06 标准输入输出流

System.in和System.out分别代表了系统标准的输入和输出设备默认输入设备是:键盘,输出设备是:显示器System.in的类型是InputStreamSystem.out的类型是PrintStream,其是OutputStream的子类FilterOutputStream的子类重定向:通过System类的setIn,setOut方法对默认设备进行改变。

public static void setIn(InputStreamin)public static void setOut(PrintStreamout)

import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class OtherStreamTest {

    
    @Test
    public void test(){
        BufferedReader br = null;
        try {
            InputStreamReader isr = new InputStreamReader(System.in);
            br = new BufferedReader(isr);

            while (true) {
                System.out.println("请输入字符串:");
                String data = br.readLine();
                if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
                    System.out.println("程序结束");
                    break;
                }

                String upperCase = data.toUpperCase();
                System.out.println(upperCase);

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}

 07 打印流

实现将基本数据类型的数据格式转化为字符串输出打印流:PrintStream和PrintWriter提供了一系列重载的print()和println()方法,用于多种数据类型的输出PrintStream和PrintWriter的输出不会抛出IOException异常PrintStream和PrintWriter有自动flush功能PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节。在需要写入字符而不是写入字节的情况下,应该使用PrintWriter 类。System.out返回的是PrintStream的实例

import org.junit.Test;
import java.io.*;

public class OtherStreamTest {

    
    @Test
    public void test2(){
        PrintStream ps = null;
        try {
            FileOutputStream fos = new FileOutputStream(new File("D:\IO\text.txt"));

            // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 'n' 时都会刷新输出缓冲区)
            ps = new PrintStream(fos, true);
            if (ps != null) {// 把标准输出流(控制台输出)改成文件
                System.setOut(ps);
            }

            for (int i = 0; i <= 255; i++) { // 输出ASCII字符
                System.out.print((char) i);
                if (i % 50 == 0) { // 每50个数据一行
                    System.out.println(); // 换行
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                ps.close();
            }
        }
    }
}

08 数据流

为了方便地操作Java语言的基本数据类型和String的数据,可以使用数据流。数据流有两个类:(用于读取和写出基本数据类型、String类的数据)DataInputStream和DataOutputStream分别“套接”在InputStream和OutputStream子类的流上DataInputStream中的方法

boolean readBoolean()	        byte readByte()
char readChar()	                float readFloat()
double readDouble()	            short readShort()
long readLong()	                int readInt()
String readUTF()	            void readFully(byte[s] b)
import org.junit.Test;
import java.io.*;

public class OtherStreamTest {    
   
    @Test
    public void test3() throws IOException {
        //1.
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
        //2.
        dos.writeUTF("刘刚");
        dos.flush();//刷新操作,将内存中的数据写入文件
        dos.writeInt(23);
        dos.flush();
        dos.writeBoolean(true);
        dos.flush();
        //3.
        dos.close();
    }

    
    @Test
    public void test4() throws IOException {
        //1.
        DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
        //2.
        String name = dis.readUTF();
        int age = dis.readInt();
        boolean isMale = dis.readBoolean();

        System.out.println("name = " + name);
        System.out.println("age = " + age);
        System.out.println("isMale = " + isMale);

        //3.
        dis.close();

    }
}

09 对象流(掌握)

用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。ObjectInputStream和OjbectOutputSteam序列化:用ObjectOutputStream类保存基本类型数据或对象的机制反序列化:用ObjectInputStream类读取基本类型数据或对象的机制ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量

9.1 对象流序列化和反序列化操作

 9.1.1 对象的序列化
@Test
    public void testObjectOutputStream() throws IOException {

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));

    oos.writeObject(new String("我爱北京"));
    oos.flush(); // 刷新操作

    oos.writeObject(new Person("wang",23));
    oos.flush(); //报错NotSerializableException

    oos.close();
}

 9.1.2 对象的反序列化
@Test
//反序列化:将磁盘文件中的对象还原为内存中的一个java对象
// 使用ObjectInputStream实现
    public void testObjectIutputStream() throws IOException, ClassNotFoundException {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));

    Object obj = ois.readObject();
    String str = (String) obj;
    System.out.println(str); //我爱北京

    Person p = (Person) ois.readObject();
    System.out.println(p);//Person{name='wang', age=23},序列化之后要flush
    ois.close();

}

 9.2 自定义类实现序列化与反序列化操作

 person类

import java.io.Serializable;

public class Person implements Serializable {
    
   public static final long serialVersionUID = -6873283914834732847l;
   private  String name;
   private int age;
   private Account account;

    public Person() {
    }

    public Person(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 "Person{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}

class Account implements Serializable{
    public static final long serialVersionUID = 4754534532L;
    private double banlance;

    public Account(double banlance) {
        this.banlance = banlance;
    }

    public double getBanlance() {
        return banlance;
    }

    public void setBanlance(double banlance) {
        this.banlance = banlance;
    }

    @Override
    public String toString() {
        return "Account{" +
                "banlance=" + banlance +
                '}';
    }
}

 9.3 全局常量serialVersionUID的理解

 

 10 NIO.2中Path、Paths、Files类的使用

Java NIO (New IO,Non-Blocking IO)是从Java 1.4版本开始引入的一套新的IO API,可以替代标准的Java IO API。NIO与原来的IO有同样的作用和目的,但是使用的方式完全不同,NIO支持面向缓冲区的(IO是面向流的)、基于通道的IO操作。NIO将以更加高效的方式进行文件的读写操作。Java API中提供了两套NIO,一套是针对标准输入输出NIO,另一套就是网络编程NIO。

|-----java.nio.channels.Channel
    |-----FileChannel:处理本地文件
    |-----SocketChannel:TCP网络编程的客户端的Channel
    |-----ServerSocketChannel:TCP网络编程的服务器端的Channel
    |-----DatagramChannel:UDP网络编程中发送端和接收端的Channel
12345

NIO. 2为了弥补不足,引入了Path接口,代表一个平台无关的平台路径,描述了目录结构中文件的位置。Path可以看成是File类的升级版本,实际引用的资源也可以不存在。 在以前 IO 操作都是这样写的

import java.io.File;

File file = new File(“index.html”);

但在Java7 中,我们可以这样写

import java.nio.file.Path;
import java.nio.file.Paths;
Path path = Paths.get(“index.html”);

Paths类提供的静态get()方法用来获取Path对象:

static Pathget(String first, String … more) : 用于将多个字符串串连成路径static Path get(URI uri): 返回指定uri对应的Path路径

Path接口 方法

Files类 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号