50、IO流

news2024/9/17 9:04:17

*学习的难点:要知道在什么情况,该用什么流

补:ANSI码就是gbk码 

一、基本概念:

1、什么是文件:

文件是保存数据的地方

2、文件流:

文件在程序中是以流的形式来操作的

(1)流:数据在数据源(文件)和程序(内存)之间经历的路径

(2)输入流:数据从数据源(文件)到程序(内存)的路径

(3)输出流:数据从程序(内存)到数据源(文件)的路径

3、常用的文件操作:

(1)创建文件对象相关构造器和方法:

1)常用构造器:

new File(String pathname)//根据路径构建一个File对象
new File(File parent,String child)//根据父目录文件+子路径构建
new File(String parent,String child)//根据父目录+子路径构建

2)常用方法:

createNewFile创建新文件

演示:

请在e盘下,创建文件news1.txt, news2.txt, news3.txt,用三种不同方式创建

package file;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

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

    }
    @Test
    //方式1:new File(String pathname)
    public void create01(){
        String filePath="e:\\news1.txt";
        File file=new File(filePath);
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    //方式2:new File(File parent,String child)
    public void create02(){
        File parentFile=new File("e:\\");
        String fileName="news2.txt";
        File file = new File(parentFile, fileName);//在内存处创建一个对象
        try {
            file.createNewFile();//把内存里的对象写入磁盘
            System.out.println("创建成功~");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    //方式3:new File(String parent,String child)
    public void create03(){
        String parenPath="e:\\";
        String fileName="news3.txt";
        File file = new File(parenPath, fileName);
        try {
            file.createNewFile();
            System.out.println("创建成功~");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

(2)获取文件的相关信息:

getName, getAbsolutePath, getParent, length, exists, isFile, isDirectory
package file;

import org.junit.jupiter.api.Test;

import java.io.File;

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

    }
    @Test
    //获取文件的信息
    public void info(){
        File file = new File("e://news1.txt");
        System.out.println("文件名字:"+file.getName());
        System.out.println("文件绝对路径:"+file.getAbsolutePath());
        System.out.println("文件父级目录:"+file.getParent());
        System.out.println("文件大小(字节):"+file.length());
        System.out.println("文件是否存在:"+file.exists());
        System.out.println("是不是一个文件:"+file.isFile());
        System.out.println("是不是一个目录:"+file.isDirectory());
        //一个汉字3个字节,一个字母1个字节

    }
}
//文件名字:news1.txt
//文件绝对路径:e:\news1.txt
//文件父级目录:e:\
//文件大小(字节):14
//文件是否存在:true
//是不是一个文件:true
//是不是一个目录:false

(3)目录的操作和文件删除:

mkdir创建一级目录、 mkdirs创建多级目录、 delete删除空目录或文件
package file;

import org.junit.jupiter.api.Test;

import java.io.File;

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

    }
    @Test
    //判断e:\news1.txt是否存在,如果存在就删除
    public void m1(){
        String filePath="e:\\news1.txt";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println(filePath + "删除成功");
            }else{
                System.out.println(filePath+"删除失败");
            }
        }else{
                System.out.println("该文件不存在");
            }
    }
    @Test
    //判断d:\\demo02(目录,可以创建一个文件夹来试验)是否存在,如果存在就删除
    public void m2(){
        String filePath="d:\\demo02";
        File file = new File(filePath);
        if (file.exists()) {
            if (file.delete()) {
                System.out.println(filePath + "删除成功");
            }else{
                System.out.println(filePath+"删除失败");
            }
        }else{
            System.out.println("该目录不存在");
        }
    }
    @Test
    //判断d:\demo02\a\b\c(目录,可以创建一个文件夹来试验)是否存在,如果存在就删除
    public void m3(){
        String directoryPath="d:\\demo02\\a\\b\\c";
        File file = new File(directoryPath);
        if (file.exists()) {
            System.out.println(directoryPath+"存在");
        }else{
            //创建多级目录用mkdirs();
            //创建一级目录用mkdir();
            if(file.mkdirs()){
                System.out.println(directoryPath+"创建成功");
            }else{
                System.out.println(directoryPath+"创建失败");
            }
        }
    }
}

4、IO流原理及流的分类

(1)IO流原理:

1)IO(Input/Output的缩写),I/O技术是非常实用的技术,用于处理数据传输,如读/写文件,网络通讯等

2)java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行

3)java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据

4)输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中

5)输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

 (2)流的分类:

1)按操作数据单位不同分为:字节流(8 bit),二进制文件;字符流(按字符),文本文件。

2)按数据流的流向不同分为:输入流,输出流

3)按流的角色的不同分为:节点流,处理流/包装流

补:

· java的IO流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的

· 由这个四个类派生出来的子类名称都是以其父类名作为子类名后缀

(5)IO流体系图:

 二、InputStream:

1、基本介绍:

(1)InputStream抽象类是所有类字节输入流的超类

(2)InputStream常用的子类:

1)FileInputStream:文件输入流

//单个字节的读取,效率较低
package inputstream;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;

@SuppressWarnings({"all"})
public class FileInputStream_ {

    public static void main(String[] args) {

    }
    @Test
    public void readFile01(){
        String filePath="e:\\hello.txt";
        int readData=0;
        FileInputStream fileInputStream=null;//因为finally也要用,所以定义在外面,大家都能用得到
        try {
            //创建FileInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取一个字节的数据,如果没有输入可用,此方法将阻止
            //如果返回-1,表示读取完毕
            while((readData=fileInputStream.read())!=-1){
                System.out.print((char)readData);//转成char显示
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
//如果是中文则会输出乱码,英文不会
//输入“hello,world韩顺平”
//hello,world!é©é¡ºå¹³
package inputstream;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;

@SuppressWarnings({"all"})
public class FileInputStream_ {

    public static void main(String[] args) {

    }
    @Test
    public void readFile02(){
        String filePath="e:\\hello.txt";
        byte[] buf=new byte[8];//一次读取8个字节
        //如果改成一次读取3个字节就可以顺利读取到汉字了
        
        int readLen=0;
        FileInputStream fileInputStream=null;//因为finally也要用,所以定义在外面,大家都能用得到
        try {
            //创建FileInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取最多b.length字节的数据到字节数组,此方法将阻塞,直到某些输入可用
            //如果读取正常,返回实际读取的字节数
            //如果返回-1,表示读取完毕
            while((readLen=fileInputStream.read(buf))!=-1){
                System.out.print(new String(buf,0,readLen));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
//hello,world

2)BufferedInputStream:缓冲字节输入流

3)ObjectInputStream:对象字节输入流

三、OutputStream:

(1)FileOutputStream:

1)基本介绍:

 2)应用实例1:

请使用FileOutputStream在a.txt文件中写入“hello,world”。如果文件不存在会创建文件(前提是目录已经存在)

package outputstream;

import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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

    }
    @Test
    public void writeFile() {
        //创建FileOutputStream对象
        String filePath = "e:\\a.txt";
        FileOutputStream fileOutputStream = null;
        try {
            //说明:
            //1、new FileOutputStream(filePath)创建方式:当写入内容时,会覆盖原来的内容
            //2、new FileOutputStream(filePath,true)创建方式:当写入内容时,是追加到文件后面

            //得到FileOutputStream对象
            fileOutputStream = new FileOutputStream(filePath);

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

            //写入字符串
            String str="hello,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)应用实例2:

拷贝图片、音乐

package outputstream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

@SuppressWarnings({"all"})
public class FileCopy {
    public static void main(String[] args) {
        //完成文件拷贝,将e:\\金1.png拷贝e:\\
        //思路分析:
        //1、创建文件的输入流,将文件读入到程序
        //2、创建文件的输出流,将读取到的文件数据,写入到指定的文件
        String strFilePath="e:\\金1.png";
        String destFilePath="e:\\贤1.png";
        FileInputStream fileInputStream=null;
        FileOutputStream fileOutputStream=null;
        try {
            fileInputStream=new FileInputStream(strFilePath);
            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();
            }

        }
    }
}

四、FileReader和FileWriter介绍:

FileReader和FileWriter是字符流,即按照字符来操作io

1、FileReader相关方法:

1)new FileReader(File/String)

(2)read:每次读取单个字符,返回该字符,如果到文件末尾返回-1

(3)read(char[]):批量读取多个字符到数组,返回读取到的字符数,如果到文件末尾返回-1

相关API:

(1)new String(char[]):将char[]转换成String

(2)new String(char[],off,len):将char[]的指定部分转换成String

2、FileWriter相关方法:

1)new FileWriter(File/String):覆盖模式,相当于流的指针在首端

2)new FileWriter(File/String, true):追加模式,相当于流的指针在尾端

3)write(int):写入单个字符

4)write(char[]):写入指定数组

5)write(char[], off, len):写入指定数组的指定部分

6)write(string):写入整个字符串

7)write(string, off, len):写入字符串的指定部分

相关API:

String类:

toCharArray:将String转换成char[]

注意:

FileWriter使用后,必须要关闭(close)或刷新(flush),否则写入不到指定的文件!

(如果没有关闭流,那就内容没有写入指定的文档,等于前面的工作(如录音)白干了) 

3、应用实例:

(1)使用FileReader从story.txt读取内容,并显示 

package reade_;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReader_ {
    public static void main(String[] args) {
        //事先先写好这个文件,否则程序报错
        String filePath="e:\\story.txt";
        FileReader fileReader=null;
        int readLen=0;
        char[] buf=new char[8];
        try {
            fileReader=new FileReader(filePath);
            //循环读取、使用read(buf),一次读取8个字符
            while((readLen=fileReader.read(buf))!=-1){
                System.out.print(new String(buf,0,readLen));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

(2)使用FileWriter将“风雨之后,定见彩虹”写入到note.txt文件中,注意细节

package writer_;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriter_ {
    public static void main(String[] args) {
        String filePath="e:\\note.txt";
        FileWriter fileWriter=null;
        char[] chars={'a','b','c'};
        try {
            fileWriter=new FileWriter(filePath);//默认是覆盖写入

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

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

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

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

            //write(string,off,len):写入字符串的指定部分
            fileWriter.write("上海天津",0,2);
            
            //在数据量大的情况下,可以使用循环操作

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

五、节点流和处理流

1、基本介绍:

(1)节点流可以从一个特定的数据源读写数据,如FileReader, FileWriter

(2)处理流(也叫包装流)是“连接”在已存在的流(节点流或处理流)之上,为程序提供更为强大的读写功能,如BufferedReader, BufferedWriter

2、节点流和处理流的区别和联系:
(1)节点流是底层流/低级流,直接跟数据源相接
(2)处理流(包装流)包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出。
(3)处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据源相连
3、处理流的功能主要体现在以下两个方面:
(1)性能的提高:主要以增加缓冲的方式来提高输入输出的效率
(2) 操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便

·演示:[模拟修饰器设计模式]

package writer_;

public class Test_ {
    public static void main(String[] args) {
        BufferedReader_ bufferedReader_ = new BufferedReader_(new FileReader_());
        bufferedReader_.readFiles(10);
        BufferedReader_ bufferedReader_1 = new BufferedReader_(new StringReader_());
        bufferedReader_1.readStrings(5);
    }
}
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//对文件进行读取。。。
//读取字符串。。。
//读取字符串。。。
//读取字符串。。。
//读取字符串。。。
//读取字符串。。。
package writer_;

public abstract class Reader_ {//抽象类
    public void readFile(){}
    public void readString(){}
}
package writer_;

/**
 * 看成节点流
 */
public class FileReader_ extends Reader_{
    public void readFile(){
        System.out.println("对文件进行读取。。。");
    }
}
package writer_;

/**
 * 看成节点流
 */
public class StringReader_ extends Reader_{
    public void readString(){
        System.out.println("读取字符串。。。");
    }
}
package writer_;

/**
 * 做成处理流/包装流
 */
public class BufferedReader_ extends Reader_{
    private Reader_ reader_;//属性是Reader_类型

    //构造器可以接收Reader_子类
    public BufferedReader_(Reader_ reader_) {
        this.reader_ = reader_;
    }
    public void readFile(){//对自己有的方法封装一层,也可以调用
        reader_.readFile();
    }
    //扩展readFile,让方法更加灵活,多次读取文件,或者加缓冲char[]...
    public void readFiles(int num){
        for(int i=0;i<num;i++){
            reader_.readFile();
        }
    }
    //扩展readString,批量处理字符串数据
    public void readStrings(int num){
        for(int i=0;i<num;i++){
            reader_.readString();
        }
    }
}

4、BufferedReader和BufferedWriter属于字符流,是按照字符来读取数据的,关闭处理流时,只需要关闭外层流即可(处理流的底层(里面)是调用了节点流,关闭处理流后,它的底层会自动关闭节点流)

5、应用案例:使用BufferedReader读取文本文件,并显示在控制台

package writer_;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReader_ {
    public static void main(String[] args) throws IOException {
        String filePath="e:\\hello.java";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        String line;//按行读取,效率高
        //1、bufferedReader.readLine()是按行读取文件
        //2、当返回null时,表示文件读取完毕
        while((line=bufferedReader.readLine())!=null){
            System.out.println(line);
        }
        //只需要关闭BufferedReader即可,因为底层会自动地去关闭节点流FileReader
        bufferedReader.close();
    }
}
//输出:
1、public class Hello表示Hello是一个类,是一个public公有的类
2、Hello{}表示一个类的开始和结束
3、public static void main(String[] args)表示一个主方法,即我们程序的入口
4、main(){}表示方法的开始和结束
//public class Hello{
//	public static void main(String[] args){
//		System.out.println("hello,world~");
//	}
//}

6、应用案例:使用BufferedWriter将“hello,韩顺平教育”,写入到文件中

package writer_;

import java.io.*;

public class BufferedCopy {
    public static void main(String[] args) {
        //1、BufferedReader和BufferedWriter是按照字符操作
        //2、不要去操作二进制文件[声音,视频,pdf],可能造成文件损坏
        String srcFilePath="e:\\hello.java";
        String destFilePath="e:\\hello1.java";
        BufferedReader br=null;
        BufferedWriter bw=null;
        String line;
        try {
            br=new BufferedReader(new FileReader(srcFilePath));
            bw=new BufferedWriter(new FileWriter(destFilePath));

            //readLine读取一行内容,但是没有换行
            while((line=br.readLine())!=null){
                //每读取一行,就写入
                bw.write(line);
                //插入一个换行
                bw.newLine();
            }
            System.out.println("拷贝完毕。。。");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {

        }
    }
}

7、BufferedInputStream

(1)基本介绍:

BufferedInputStream是字节流,在创建BufferedInputStream时,会创建一个内部缓冲区数组

package writer_;

import java.io.*;

public class BufferedCopy02 {
    public static void main(String[] args) {
        String srcFilePath="e:\\金1.png";
        String destFilePath="e:\\镇1.png";

        //创建BufferedInputStream和BufferedOutputStream对象
        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;
            while((readLen=bis.read(buff))!=-1){
                bos.write(buff,0,readLen);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭外层处理流
                if(bis!=null){
                    bis.close();
                }
                if(bos!=null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

 8、对象流--ObjectlnputStream和ObjectOutputStream
>看一个需求
1.将intnum=100这个int数据保存到文件中,注意不是100数字,而是int100(即
保存数据的同时保存该数据的类型),并且,能够
    从文件中直接恢复int100
2  将Dogdog=newDog(“小黄”,3)这个dog对象保存到文件中,并且能够从文件恢复
3  上面的要求,就是能够将基本数据类型或者对象进行序列化和反序列化操作

(1)序列化和反序列化:

1)序列化就是在保存数据时,保存数据的值和数据类型
2)反序列化就是在恢复数据时,恢复数据的值和数据类型
3)需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:
    >Serializable //这是一个标记接口,没有方法
    >Externalizable//该接口有方法需要实现,所以一般用上面那个Serializable

(2)基本介绍:

1)功能:提供了对基本类型或对象类型的序列化和反序列化的方法

2)ObjectOutputStream提供了序列化功能

 3)ObjectlnputStream提供反序列化功能

 (3)应用案例:

//序列化
package inputstream;

import org.junit.jupiter.api.Test;

import java.io.FileInputStream;

@SuppressWarnings({"all"})
public class FileInputStream_ {

    public static void main(String[] args) {

    }
    @Test
    public void readFile02(){
        String filePath="e:\\hello.txt";
        byte[] buf=new byte[8];//一次读取8个字节
        //如果改成一次读取3个字节就可以顺利读取到汉字了

        int readLen=0;
        FileInputStream fileInputStream=null;//因为finally也要用,所以定义在外面,大家都能用得到
        try {
            //创建FileInputStream对象,用于读取文件
            fileInputStream = new FileInputStream(filePath);
            //从该输入流读取最多b.length字节的数据到字节数组,此方法将阻塞,直到某些输入可用
            //如果读取正常,返回实际读取的字节数
            //如果返回-1,表示读取完毕
            while((readLen=fileInputStream.read(buf))!=-1){
                System.out.print(new String(buf,0,readLen));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭文件流,释放资源
            try {
                fileInputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
//hello,world
//序列化
package inputstream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectOutputStream_ {
    public static void main(String[] args) throws Exception {
        //序列化后,保存的文件格式,不是存文本,而是按照他的格式来保存
        String filePath="e:\\data.dat";

        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(filePath));

        //序列化数据到e:\data.dat
        oos.writeInt(100);//int---->Integer
        oos.writeBoolean(true);//boolean---->Boolean
        oos.writeChar('a');//char---->Character
        oos.writeDouble(9.5);//double---->Double
        oos.writeUTF("韩顺平教育");//String
        oos.writeObject(new Dog("旺财",10));

        oos.close();
        System.out.println("数据保存完毕(序列化完毕)");
    }
}
//反序列化
package inputstream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInputStream_ {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //指定返回序列化的文件
        String filePath="e:\\data.dat";

        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(filePath));

        //1、读取(反序列化)的顺序和你保存数据(序列化)的顺序一致
        //2、否则会出现异常

        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        Object dog=ois.readObject();

        //dog的编译类型是Object,dog的运行类型是Dog
        System.out.println("运行类型:"+dog.getClass());
        System.out.println("dog信息:"+dog);

        //重要细节:
        //1、如果我们希望调用Dog的方法,需要向下转型
        //2、需要我们将Dog类的定义,拷贝到可以引用的位置
        Dog dog1=(Dog)dog;
        System.out.println(dog1.getName());

        //关闭流
        ois.close();
    }
}
//100
//true
//a
//9.5
//韩顺平教育
//运行类型:class inputstream.Dog
//dog信息:Dog{name='旺财', age=10}
//旺财

 

(4)注意事项和细节说明:

1)读写顺序要一致
2)要求实现序列化或反序列化对象,需要实现Serializab!e
3)序列化的类中建议添加SerialVersionUID为了提高版本的兼容
4)序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员

5)序列化对象时,要求里面属性的类型也需要实现序列化接口
6)序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实现了序列化

9、标准输入输出流:

package file;

import java.util.Scanner;

public class InputAndOutput {
    public static void main(String[] args) {
        //System.in——》public final static InputStream in=null;
        //System.in编译类型:InputStream
        //System.in运行类型:BufferedInputStream
        //表示的是标准输入 键盘
        System.out.println(System.in.getClass());

        //1、System.out——》public final static PrintStream out=null;
        //2、编译类型PrintStream
        //3、运行类型PrintStream
        //4、表示标准输出 显示器
        System.out.println(System.out.getClass());
        Scanner scanner=new Scanner(System.in);
        System.out.println("输入内容:");
        String next=scanner.next();
        System.out.println("next="+next);

    }
}
//class java.io.BufferedInputStream
//class java.io.PrintStream
//输入内容:
//hello
//next=hello

10、转换流:InputStreamReader和OutputStreamWriter

(1)基本介绍:
1)InputStreamReader:Reader的子类,可以将InputStream(字节流)包装成Reader(字符流)
2)OutputStreamWriter:writer的子类,实现将OutputStream(字节流)包装成Writer(字符流)
3)当处理纯文本数据时,如果使用字符流效率更高,并且可以有效解决中文问题,所以建议将字节流转换成字符流
4)可以在使用时指定编码格式(比如utf-8,gbk,gb2312,1S08859-1等)

 (2)应用实例:

package transformation;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CodeQuestion {
    public static void main(String[] args) throws IOException {
        //读取e:\\a.txt文件到程序,中文乱码问题
        //思路:
        //1、创建字符输入流BufferedReader[处理流]
        //2、使用BufferedReader对象读取a.txt
        //3、默认情况下,读取文件是按照utf-8编码
        String filePath="e:\\a.txt";
        BufferedReader br=new BufferedReader(new FileReader(filePath));

        String s=br.readLine();
        System.out.println("读取到的内容:"+s);
        br.close();
    }
}
//读取到的内容:hello,world!��˳ƽ
package transformation;

import java.io.*;

/**
 * 演示使用InputStreamReader转换流解决中文乱码问题
 * 将字节流FileInputStream转成字符流InputStreamReader,指定编码gbk/utf-8
 */
public class InputStreamReader_ {
    public static void main(String[] args) throws IOException {

        String filePath = "e:\\a.txt";
        //1、把 FileInputStream 转成 InputStreamReader
        //2、指定编码gbk
        InputStreamReader isr=new InputStreamReader(new FileInputStream(filePath),"gbk");
        //3、把InputStreamReader转入BufferedReader
        BufferedReader br = new BufferedReader(isr);

        //将2和3合在一起写也可以
//        BufferedReader br = new BufferedReader(new InputStreamReader(
//                                                    new FileInputStream(filePath), "gbk"));

        //4、读取
        String s=br.readLine();
        System.out.println("读取内容:"+s);
        //5、关闭外层流
        br.close();

    }
}
//读取内容:hello,world!韩顺平

按照指定的编码保存文件


package transformation;

import java.io.*;

/**
 * 演示使用OutputStreamWriter
 * 把FileOutputStream字节流转成字符流OutputStreamWriter
 * 指定处理的编码gbk/utf-8/utf8
 */
public class OutputStreamWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath="e:\\hsp.txt";
        String charSet="utf-8";
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), charSet);
        osw.write("hi,韩顺平教育");
        osw.close();
        System.out.println("按照 "+charSet+" 保存文件成功");
    }
}

10、打印流--PrintStream和PrintWriter

(打印流只有输出流,没有输入流)

 

package transformation;

import java.io.IOException;
import java.io.PrintStream;

/**
 * 演示PrintStream(字符打印流/输出流)
 */
public class PrintStream_ {
    public static void main(String[] args) throws IOException {
        PrintStream out =System.out;
        //在默认情况下,PrintStream输出数据的位置是标准输出位置,即显示器
        /*源码:
        public void print(String s) {
            write(String.valueOf(s));
        }*/
        out.println("john,hello");

        //因为print底层使用的是write,所以我们可以直接调用write进行打印/输出
        out.write("韩顺平,你好".getBytes());
        out.close();

        //修改打印流输出的位置/设备
        //hello,韩顺平教育 会输出到e:\f1.txt
        /*源码:
        public static void setOut(PrintStream out) {
            checkIO();
            setOut0(out);//native方法,修改了out
        }*/
        System.setOut(new PrintStream("e:\\f1.txt"));
        System.out.println("hello,韩顺平教育");
    }
}
package transformation;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintWriter_ {
    public static void main(String[] args) throws IOException {
        PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt"));
        printWriter.print("hi,北京你好~");
        printWriter.close();//flush+关闭流,才会将数据写入到文件
    }
}

11、Properties类

(1)引入:

 //传统方式:

 

 (2)基本介绍:

1)专门用于读写配置文件的集合类,

配置文件的格式:

键=值

键=值

2)注意:键值对不需要有空格,值不需要用引号一起来,默认类型是String

3)Properties的常见方法:

1)load:加载配置文件的键值对到Properties对象

2)list:将数据显示到指定设备

3)getProperty(key)根据键获取值

4)setProperty(key.value):设置键值对到Properties对象

5)store:将Properties中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码

Unicode编码转换 - 站长工具 (chinaz.com)https://tool.chinaz.com/tools/unicode.aspx---->unicode码查询工具

(3)应用案例:

package transformation;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Properties02 {
    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);
    }
}
//-- listing properties --
//ip=192.168.100.100
//pwd=12345
//user=root
//用户名:root
//密码是:12345
package transformation;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Properties03 {
    public static void main(String[] args) throws IOException {
        //使用Properties类来创建配置文件,修改配置文件内容
        Properties properties = new Properties();

        //创建
        //1、如果该文件没有key,就是创建
        //2、如果该文件有key,就是修改
        // Properties父类是Hashtable,底层是Hashtable核心方法
        properties.setProperty("charset","utf8");
        properties.setProperty("user","汤姆");//注意保存时,是中文的unicode码值
        properties.setProperty("pwd","abc111");

        //将k-v存储文件中即可
        properties.store(new FileOutputStream("src\\mysql2.properties"),null);

        //properties.store(new FileOutputStream("src\\mysql2.properties"),"hello world");
        //相当于加了一个注释在上面:
        //#hello world
        //#Sun Dec 04 18:34:17 CST 2022
        //charset=utf8
        //pwd=abc111
        //user=\u6C64\u59C6

        System.out.println("保存配置文件成功");
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/60795.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

XXL-Job海量数据处理-分片任务实战

文章目录一、需求1. 场景2. 分析3. 案例二、什么是分⽚任务2.1. 分⽚路由策略2.2. 海量数据处理2.3. 分片数量2.4. 分片值颁发2.5. 案例三、解决思路3.1. 数据拆分3.2. 分片数量3.3. 分⽚⽅式3.4. 路由策略3.5. 程序实战一、需求 1. 场景 有⼀个任务需要处理100W条数据&#…

【JavaScript 逆向】极验四代无感验证码逆向分析

前言 四代无感验证码相较于滑块验证码区别就是没有底图&#xff0c;一键通过模式&#xff0c;所以不需要轨迹以及计算缺口距离&#xff0c;步骤更少&#xff0c;四代滑块可以阅读&#xff1a;【JavaScript 逆向】极验四代滑块验证码逆向分析 声明 本文章中所有内容仅供学习交…

C++最后一次实验及实验总结

忙活了大半个学期&#xff0c;终于学完了C&#xff0c;虽然很基础&#xff0c;但是至少算是写完了实验&#xff0c;开心~~ 实验一 实验二 实验三 实验四 实验五 实验六 题目一 一、分析下面的程序&#xff0c;写出其运行时的输出结果。上机运行该程序&#xff0c;观察运行…

[附源码]计算机毕业设计ssm新能源电动汽车充电桩服务APP

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

ROS action客户端和服务端通信(Ubuntu )

ROS action客户端和服务端通信 gcusms ROS 一般都是用 service 和 topic 进行数据之间的交互传输&#xff0c;因为这种通信方式无法满数据实时反馈的要求&#xff0c;所以采用 action 动作消息反馈通信机制&#xff08;实时反馈的任务进度&#xff0c;并且可以随时终止运行&am…

用 AWTK 和 AWPLC 快速开发嵌入式应用程序 (6)-在线调试

AWPLC 目前还处于开发阶段的早期&#xff0c;写这个系列文章的目的&#xff0c;除了用来验证目前所做的工作外&#xff0c;还希望得到大家的指点和反馈。如果您有任何疑问和建议&#xff0c;请在评论区留言。 1. 背景 AWTK 全称 Toolkit AnyWhere&#xff0c;是 ZLG 开发的开源…

vuex学习记录

为什么要用vuex 由于vue本身的特点。及页面是由多个组件构成。而组件又呈现一个二叉树状态。然后父向子需要进行通信。那如果是非父子关系&#xff0c;应该如何传值呢&#xff1f; 什么是vuex 专门为vue.js应用程序开发的状态管理模式。它采用集中式存储管理数据&#xff0c…

详解 Go 语言中的 init () 函数

阅读目录Go init 函数的详细说明包初始化Go init 函数的详细说明 初始化每个包后&#xff0c;会自动执行 init&#xff08;&#xff09;函数&#xff0c;并且执行优先级高于主函数的执行优先级。 init 函数通常用于&#xff1a; 变量初始化检查 / 修复状态注册器运行计算 包…

c语言零基础入门(完整版)

1软件下载 官网下载: https://sourceforge.net/projects/orwelldevcpp/ 百度网盘&#xff1a;https://pan.baidu.com/s/1mhHDjO8 提取密码&#xff1a;mken 推荐用百度网盘&#xff0c;官方下载太慢了 开始安装 首先双击打开刚刚下载的软件 点击0k 因为在安装过程中不能使用…

【计算机视觉】图像形成与颜色

图像形成与颜色 光照及阴影 辐射度学 颜色 颜色信息反映了入射光的能量分布与波长&#xff0c;可见光的波长在400nm到760nm之间。 RGB RGB分别代表三个基色&#xff08;R-红色、G-绿色、B-蓝色&#xff09;&#xff0c;如(0,0,0)表示黑色、(255, 255, 255)表示白色。其中2…

TS装饰器bindThis优雅实现React类组件中this绑定

初学React类组件时&#xff0c;最不爽的一点应该就是 this 指向问题了吧&#xff01;初识React的时候&#xff0c;肯定写过这样错误的demo。 import React from react; export class ReactTestClass extends React.Component {constructor(props) { super(props); this.state …

程序员如何进化成架构师?

作为程序员的上端&#xff0c;每一个架构师都承担着艰巨的任务&#xff1a;不仅仅要和产品进行周旋&#xff0c;还需要有十分坚实的技术作为基础打底。 那么&#xff0c;架构师都在考虑什么呢&#xff1f; 说到底还是程序架构、结构性的问题。 比如最近十分火爆的分布式系统…

STM32CubeIDE(stm32f767)添加DSP库

对于stm32f4 系列可以使用STM32Cube添加相应的库&#xff0c;自动生成代码。可以参考下面写的文章 (1条消息) STM32CubeMX关于添加DSP库的使用_W_oilpicture的博客-CSDN博客_cubemx dsp库 不过&#xff0c;对于stm32f767等M7的内核目前可能并不适用&#xff0c;需要自己手动添…

CS162 shell

本文记录我在做shell这个作业时用到有关资源&#xff0c;如Linux系统调用、Linux基础知识、C语言知识等。 这里只是非常简略地记录了一下&#xff0c;并且可能有理解不正确的地方&#xff0c;你可以把本文当作一个索引和没有思路时的启发&#xff0c;详细的信息可以再去查&…

SVM(支持向量机)基本形式推导

据说在dl之前是SVM撑起了ml的半片天&#xff0c;学习后发现SVM是由纯粹的数学推导、转化、求解、优化“堆砌”而来&#xff0c;不如说是数学撑起了ml&#xff0c;ml是数学的学科。以下根据老师ppt上讲解的思路讲讲个人对SVM基本形式推导的理解。 margin&#xff08;间隔&#x…

[附源码]计算机毕业设计现代诗歌交流平台Springboot程序

项目运行 环境配置&#xff1a; Jdk1.8 Tomcat7.0 Mysql HBuilderX&#xff08;Webstorm也行&#xff09; Eclispe&#xff08;IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持&#xff09;。 项目技术&#xff1a; SSM mybatis Maven Vue 等等组成&#xff0c;B/S模式 M…

2. IMU原理及姿态融合算法详解

文章目录2. IMU原理及姿态融合算法详解一、组合二、 原理a) 陀螺仪b) 加速度计c) 磁力计三、 旋转的表达a) 欧拉角b) 旋转矩阵c) 四元数d) 李群 SO(3)\text{SO}(3)SO(3) 及 李代数 so(3)\text{so}(3)so(3)四、 传感器的噪声及去除a) 陀螺仪b) 加速度计c) 磁力计五、姿态解算原理…

CSS基础-选择器进阶,背景相关属性(颜色/图片)

CSS基础-选择器进阶,背景相关属性(颜色/图片) 目标&#xff1a;能够理解 复合选择器 的规则&#xff0c;并使用 复合选择器 在 HTML 中选择元素 学习路径&#xff1a;1. 复合选择器2. 并集选择器3. 交集选择器4. hover伪类选择器5. Emmet语法 本次我们所学的内容&#xff1a; 1…

Kafka - 15 Kafka Offset | 自动和手动提交Offset | 指定Offset消费 | 漏消费和重复消费 | 消息积压

文章目录1. Offset 的默认维护位置2. 自动提交 Offset3. 手动提交 Offset1. 同步提交 offset2. 异步提交 offset4. 指定 Offset 消费5. 指定时间消费6. 漏消费和重复消费7. 消费者事务8. 数据积压&#xff08;消费者如何提高吞吐量&#xff09;1. Offset 的默认维护位置 Kafka…