File类和IO流的相关面试(二)

news/2025/2/7 6:15:08/

一.IO流

1.什么是IO?

I:Input,数据的读取,数据的输入

        数据的输入,可以是从键盘输入,从文件读取,从网络接收...

O:Output,写数据,数据的输出

        数据的输出,可以是输出到控制台,写到文件,发生到网络中...

2.在java中要进行IO操作需要使用IO流类来完成

现在说的是传统的IO,阻塞式IO。

后面还会有NIO(非阻塞式IO)和AIO(异步IO)

基础阶段的IO流类:

(1)按照方向分

输入流类:

        InputStream,Reader系列

输出流类:

        OutputStream,Writer系列

(2)按照操作数据的方式,单位分:

字节流:

        InputStream,OutputStream系列

字符流:

        Reader,Writer

(3)按照角色不同

节点流:

        和数据的源头/目的地连接的IO流:

                FileInputStream,FileOutputStream,FileReader,FileWriter:读写文件

                ByteArrayInputStream,ByteArrayOutputStream,CharArrayReader,CharArrayWriter:读写数组

                ....

处理流/包装流/装饰流:

        它是在其他IO流基础上,增加功能用的

                BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter,给其他IO流增加缓冲功能。

                InputStreamReader,OutputStreamReader,给其他IO流转换类型用的,或者给其他IO编码,解码用的

                ...

区分它们的依据是创建它们的对象的方式,如果是处理流,创建调用它们的构造器创建对象时,必须传入另一个IO流对象。

        FileInputStream(String name):节点流,参数不是IO流类型

        BufferedInputStream(InputStream in):处理流,参数是IO流类型

3.IO流类的四个基类,四个超类,四个父类,其他的IO流都是从它们演变过来,是它们的子类。

        字节输入流:InputStream

        字节输出流:OutputStream

        字符输入流:Reader

        字符输出流:Writer

代码如下:

public class TestIO {@Testpublic void test02(){Scanner input = new Scanner(System.in);//从键盘输入System.out.print("输入一个姓名:");String name = input.next();System.out.println("name = " + name);input.close();}@Testpublic void test01(){System.out.println("hello");//这个也是IO操作,把数据输出到控制台}
}

4.文件IO流(输入流)

(1)FileInputStream类

        如果文件不存在,会报错java.io.FileNotFoundException:xxx(系统找不到指定的文件)

父类:InputStream类(字节输入流)

public int read():从输入流读取一个字节。返回读取的字节值。虽然读取了一个字节,但是会自动提升为int类型。如果已经到达流末尾,没有数据可读,则返回-1

public int read(byte[] b):从输入流中读取一些字节数,并将它们存储到字节数组 b 中。每次最多读取b.length个字节。返回实际读取的字节个数。如果已经到达流末尾,没有数据可读,则返回-1.

public int read(byte[] b,int off,int len):从输入流中读取一些字节数,并将它们存储到字节数组 b 中,从b[off]开始存储,返回实际读取的字节个数。如果已经到达流末尾,没有数据可读,则返回-1

public void close():关闭此输入流并释放与此流相关联的任何系统资源。

(2)FileOutputStream类

        如果文件不存在,会自动创建

        如果文件已存在,会覆盖原来的内容

        如果要追加,在创建FileOutputStream类对象时,加一个参数true

它的父类是OutputStream类,字节输出流

public void write(int b):将指定的字节输出。虽然参数为int类型四个字节,但是只会保留一个字节的信息写出。

public void write(byte[] b):将b.length字节从指定的字节数组写入此输出流。

public void write(byte[] b,int off,int len):从指定的字节数组写入 len 字节,从偏移量 off 开始输出到此输出流。

public void close():关闭此输入流并释放与此流相关联的任何系统资源。

(3)FileReader类

父类 Reader类:

public int read(): 从输入流读取一个字符。 虽然读取了一个字符,但是会自动提升为int类型。返回该字符的Unicode编码值。如果已经到达流末尾了,则返回-1。

public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。每次最多读取cbuf.length个字符。返回实际读取的字符个数。如果已经到达流末尾,没有数据可读,则返回-1。

public int read(char[] cbuf,int off,int len):从输入流中读取一些字符,并将它们存储到字符数组 cbuf中,从cbuf[off]开始的位置存储。每次最多读取len个字符。返回实际读取的字符个数。如果已经到达流末尾,没有数据可读,则返回-1。

public void close():关闭此流并释放与此流相关联的任何系统资源。

(4)FileWriter类

父类Writer类

public void write(int c):写入单个字符。

public void write(char[] cbuf):写入字符数组。

public void write(char[] cbuf, int off, int len):写入字符数组的某一部分,off数组的开始索引,len写的字符个数。

public void write(String str):写入字符串。

public void write(String str, int off, int len):写入字符串的某一部分,off字符串的开始索引,len写的字符个数。

public void flush():刷新该流的缓冲。

public void close():关闭此流,但要先刷新它。

5.IO流操作的步骤:

① 先选择合适的IO流类,创建它的对象

② 读或写操作

③ 关闭IO流

        因为IO流类的对象会在JVM内存中开辟空间。这个部分的内存,可以有JVM的GC自动回收

        但是IO流的操作还会在JVM内存的外部开辟空间,因为IO操作需要用到操作系统的一些函数,这些内存GC是无法回收的。

        那么要通过调用close方法,通知操作系统释放对应的内存。

代码如下:

FileInputStream类

public class TestFileInputStream {@Testpublic void test04() throws IOException {/*要以字节的方式读取一个文件:当前模块下的文件1.txt*/FileInputStream fis = new FileInputStream("1.txt");//相对路径byte[] data = new byte[2];while(true) {int len = fis.read(data);//数据读取到data字节数组中if(len == -1){break;}System.out.println("len = " + len);
//            System.out.println(Arrays.toString(data));//[97,98]System.out.println(new String(data,0,len));//本次读取了几个字节,就用几个字节构造字符串}fis.close();}@Testpublic void test03() throws IOException {/*要以字节的方式读取一个文件:当前模块下的文件1.txt*/FileInputStream fis = new FileInputStream("1.txt");//相对路径while(true){int read = fis.read();if(read == -1){break;}System.out.println(read);}fis.close();}@Testpublic void test02() throws IOException {/*要以字节的方式读取一个文件:当前模块下的文件1.txt*/FileInputStream fis = new FileInputStream("1.txt");//相对路径byte[] data = new byte[2];int len = fis.read(data);//数据读取到data字节数组中System.out.println("len = " + len);//2System.out.println(Arrays.toString(data));//[97,98]len = fis.read(data);System.out.println("len = " + len);//1System.out.println(Arrays.toString(data));//[99, 98]   这个98是上次读取的,本次只读取了一个字节len = fis.read(data);System.out.println("len = " + len);//-1System.out.println(Arrays.toString(data));//[99, 98]   这个99,98是上次读取的,本次只读取了一个字节fis.close();}@Testpublic void test01() throws IOException {/*要以字节的方式读取一个文件:当前模块下的文件1.txt*/FileInputStream fis = new FileInputStream("1.txt");//相对路径System.out.println(fis.read());System.out.println(fis.read());System.out.println(fis.read());System.out.println(fis.read());fis.close();}
}

FileOutputStream类(代码演示)

public class TestFileOutputStream {@Testpublic void test03() throws IOException {//输出hello到1.txt,接着原来内容写FileOutputStream fos = new FileOutputStream("1.txt",true);//已存在  true表示追加//把字符串转为字节数组fos.write("hello".getBytes());fos.close();}@Testpublic void test02() throws IOException {//输出houxiaosun到1.txtFileOutputStream fos = new FileOutputStream("1.txt");//已存在//把字符串转为字节数组fos.write("houxiaosun".getBytes());fos.close();}@Testpublic void test01() throws IOException {//输出chai到1.txtFileOutputStream fos = new FileOutputStream("1.txt");//把字符串转为字节数组fos.write("chai".getBytes());fos.close();}
}

FileReader类

public class TestFileReader {@Testpublic void test02()throws IOException{/*要以字符的方式读取一个文件:当前模块下的文件1.txt*/FileReader fr = new FileReader("1.txt");char[] data = new char[2];int len;while((len = fr.read(data)) != -1){System.out.println(new String(data,0,len));}fr.close();}@Testpublic void test01()throws IOException {/*要以字节的方式读取一个文件:当前模块下的文件1.txt*/FileInputStream fis = new FileInputStream("1.txt");//相对路径byte[] data = new byte[3];while(true) {int len = fis.read(data);//数据读取到data字节数组中if(len == -1){break;}System.out.println(new String(data,0,len));//本次读取了几个字节,就用几个字节构造字符串//new String就是解码操作,但是发现乱码了}fis.close();}
}

FileWriter类

public class TestFileWriter {@Testpublic void test()throws IOException {//输出一句话到1.txtFileWriter fw = new FileWriter("1.txt",true);//true表示追加fw.write("我爱你");fw.close();}
}

练习题:使用文件字节流复制一个视频文件

例如:D:\atguigu\javaee\JavaSE20220106\video\day0107_01video\day0107_01JavaSE阶段学什么.avi

复制到当前工程的根目录

复制文件:其实就是读一个文件,然后把读取的数据写到另一个文件

public class Exercise3 {@Testpublic void test()throws IOException{FileInputStream fis = new FileInputStream("D:\\atguigu\\javaee\\JavaSE20220106\\video\\day0107_01video\\day0107_01JavaSE阶段学什么.avi");//要求上面这个文件必须存在,否则会报错FileOutputStream fos = new FileOutputStream("day0107_01JavaSE阶段学什么1.avi");//下面这个文件可以不存在byte[] data = new byte[1024];//一次读取1KB/*    while(fis.read(data) != -1){//错误,因为这个循环会导致每次循环读了两次,一次用于判断,一次用于输出fos.write(fis.read(data));}*/int len;while((len= fis.read(data)) !=-1){fos.write(data,0,len);}fis.close();fos.close();}public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("D:\\atguigu\\javaee\\JavaSE20220106\\video\\day0107_01video\\day0107_01JavaSE阶段学什么.avi");//要求上面这个文件必须存在,否则会报错FileOutputStream fos = new FileOutputStream("day0107_01JavaSE阶段学什么.avi");//下面这个文件可以不存在byte[] data = new byte[1024];//一次读取1KBwhile(true){int len = fis.read(data);//从源文件读取len个字节if(len == -1){break;}fos.write(data,0,len);//把本次读取的len个字节写到目标文件中}fis.close();fos.close();}
}

6.IO流的问题

(1)关于直接基于"文件夹/目录"创建IO流对象是错误的

会报java.io.FileNotFoundException:d:\download(拒绝访问)

(2)输出流如果不能及时关闭(因为可能后面还要用),但是又想要数据及时输出,可以调用flush方法,但是如果后面不用啦,一定要close。因为close不仅仅是刷数据的问题,还涉及到内存回收问题。

public class TestProblem {@Testpublic void test02()throws IOException{FileWriter fw = new FileWriter("1.txt");fw.write("柴林燕");/*像FileWriter等很多的输出流的内部有自己的一个小小缓冲区,它在调用write方法时,会先将数据写到缓冲区。当缓冲区满的时候,会自动“溢出”到文件。当缓冲区没满的时候,会close方法执行时,把缓冲区的数据“清空”输出。*///如果希望数据及时写出,但是暂时还不closefw.flush();//刷新fw.write("是Java讲师");fw.flush();fw.close();fw.write("很棒");//java.io.IOException: Stream closedfw.close();//这个就是错误的,因为已经close过了}@Testpublic void test01()throws IOException {
//        FileInputStream fis = new FileInputStream("d:\\download");//java.io.FileNotFoundException: d:\download (拒绝访问。)
//        文件IO必须基于文件路径创建对象//"d:\\download"这个是一个文件夹的路径,不是一个文件的路径//        FileOutputStream fos = new FileOutputStream("d:\\download");//java.io.FileNotFoundException: d:\download (拒绝访问。)//就算是让 FileOutputStream帮你自动创建文件,也必须在IO流的构造器中,指明文件的名称。}
}

7.缓冲流:

        作用是给其他IO流增加缓冲区,提高效率

缓冲流的使用必须基于其他的IO流,也就是说,缓冲流它只能是装修(包装)别的流。

BufferedInputStream:只能包装InputStream系列的IO流

BufferedOutputStream:只能包装OutputStream系列的IO流

BufferedReader:只能包装Reader系列的IO流

BufferedWriter:只能包装Writer系列的IO流

原理:所有的缓存流在内部会开辟一块更大的缓存区,默认大小是8192字节/字符(本质就是一个8192长度的byte/char数组)

        另外,对于BufferedReader和BufferedWriter来说,它除了提高效率之外,

还可以使用下面两个方法,使得读写更方便:

BufferedReader类的  String readLine() 读取一整行

BufferedWriter类的void newLine()  写入一整行

代码演示:

public class TestBuffered {@Testpublic void test05()throws IOException{//一行一行的写数据FileWriter fw = new FileWriter("1.txt");BufferedWriter bw = new BufferedWriter(fw);bw.write("hello");bw.newLine();//一行一行的写数据bw.write("java");bw.newLine();bw.write("world");bw.newLine();bw.write("aa");bw.close();fw.close();}@Testpublic void test04()throws IOException{//一行一行的写数据FileWriter fw = new FileWriter("1.txt");fw.write("hello");fw.write("\r\n");fw.write("java");fw.write("\r\n");fw.write("world");fw.write("\r\n");fw.write("aa");fw.close();}@Testpublic void test03()throws IOException {FileReader fr = new FileReader("1.txt");BufferedReader br = new BufferedReader(fr);//包装frString str;while((str=br.readLine())!=null){//一行一行的读数据System.out.println(str);}br.close();fr.close();}@Testpublic void test02()throws IOException {long start = System.currentTimeMillis();/*演示 BufferedInputStream包装FileInputStreamBufferedOutputStream包装FileOutputStream来完成复制文件。D:\software\IntelliJ IDEA\ideaIU-2020.3.1.win.zip*/FileInputStream fis = new FileInputStream("D:\\software\\IntelliJ IDEA\\ideaIU-2020.3.1.win.zip");BufferedInputStream bis = new BufferedInputStream(fis);//BufferedInputStream包装FileInputStream的对象FileOutputStream fos = new FileOutputStream("D:\\idea2.zip");BufferedOutputStream bos = new BufferedOutputStream(fos);byte[] data = new byte[1024];int len;while((len = bis.read(data)) != -1){bos.write(data,0,len);}bis.close();fis.close();bos.close();fos.close();long end = System.currentTimeMillis();System.out.println("耗时:" + (end-start));//耗时:6164}@Testpublic void test01()throws IOException {long start = System.currentTimeMillis();/*演示 BufferedInputStream包装FileInputStreamBufferedOutputStream包装FileOutputStream来完成复制文件。D:\software\IntelliJ IDEA\ideaIU-2020.3.1.win.zip*/FileInputStream fis = new FileInputStream("D:\\software\\IntelliJ IDEA\\ideaIU-2020.3.1.win.zip");FileOutputStream fos = new FileOutputStream("D:\\idea3.zip");byte[] data = new byte[1024];int len;while((len = fis.read(data)) != -1){fos.write(data,0,len);}fis.close();fos.close();long end = System.currentTimeMillis();System.out.println("耗时:" + (end-start));//42063}
}

有无缓冲区的不同:

8.IO流的关闭顺序:

IO流有依赖关系。外层的包装流依赖于内存的被包装流。
        比如:BufferedInputStream 依赖于 FileInputStream
                   BufferedOutputStream 依赖于 FileOutputStream
 
             如果把内层的IO先关闭了,外层IO流就失去了依赖,就会报错。
         比喻:我坐在凳子上,我依赖于凳子,如果把凳子先撤了,我就会摔倒。
         先让我站起来,然后撤凳子。
 
         关闭的顺序是:先关闭外层的包装流,再关闭内层的被包装流。
 
         本质:
            这段代码数据的流向: 源文件 -> fis -> bis -> data数组  -> bos -> fos ->目标文件.
            BufferedOutputStream和FileOutputStream,如果把fos先关了,
            bos中的缓冲的数据没有办法完全写出。因为close时,会清空缓冲区。
 
        好记:
            fis被包装在里面的,比喻成内衣,先穿内衣,再穿外衣。
            bis相当于外套。
 
            fos是内衣,bos是外套。
 
            关闭好比是脱衣服。
            先脱外套,再脱内衣。
 
           或者,先new的后关。
 
           fis和bis有顺序关系
           fos和bos有顺序关系
 
            fis和bis 、 fos和bos是没有顺序关系。

演示代码:

public class TestClose {@Testpublic void test02()throws IOException {FileInputStream fis = new FileInputStream("D:\\atguigu\\javaee\\JavaSE20220106\\video\\day0107_01video\\day0107_01JavaSE阶段学什么.avi");BufferedInputStream bis = new BufferedInputStream(fis);//BufferedInputStream包装FileInputStream的对象FileOutputStream fos = new FileOutputStream("JavaSE.avi");BufferedOutputStream bos = new BufferedOutputStream(fos);/*这段代码数据的流向: 源文件 -> fis -> bis -> data数组  -> bos -> fos ->目标文件*/byte[] data = new byte[1024];int len;while((len = bis.read(data)) != -1){bos.write(data,0,len);}fis.close();fos.close();bis.close();bos.close();//java.io.IOException: Stream Closed//应该先关闭外层,再关闭内层}
}

 9.编码与解码问题

使用FileReader和FileWriter去操作和当前程序编码不一致的文件时,就会出现乱码的问题,怎么解决?

使用InputStreamReader:输入流

        OutputStreamReader:输出流

构造方法摘要:

InputStreamReader(InputStream in):创建一个使用默认字符集的 InputStreamReader。

InputStreamReader(InputStream in,Charset cs):创建使用给定字符集的 InputStreamReader。

InputStreamReader(InputStream in,CharsetDecoder dec):创建使用给定字符集解码器的 InputStreamReader。

InputStreamReader(InputStream in,String charsetName):创建使用指定字符集的 InputStreamReader。

代码演示:

public class TestInputStreamReader {@Testpublic void test04()throws IOException{//输出尚硅谷太棒了到GBK编码的1.txt文件中//加入缓冲流提高效率FileOutputStream fos = new FileOutputStream("1.txt");//(1)可以加入这个位置BufferedOutputStream bos = new BufferedOutputStream(fos);//因为被包装的fos是字节流OutputStreamWriter osw = new OutputStreamWriter(bos,"GBK");//把字符数据用GBK编码转为字节数据输出//(2)也可以加入这儿BufferedWriter bw = new BufferedWriter(osw);//因为osw是字符流bw.write("尚硅谷太棒了");bw.close();osw.close();bos.close();fos.close();}@Testpublic void test03()throws IOException{//输出尚硅谷太棒了到GBK编码的1.txt文件中FileOutputStream fos = new FileOutputStream("1.txt");OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");//把字符数据用GBK编码转为字节数据输出osw.write("尚硅谷太棒了");osw.close();fos.close();}@Testpublic void test02()throws IOException {FileInputStream fis = new FileInputStream("1.txt");InputStreamReader isr = new InputStreamReader(fis,"GBK");//文件的编码是GBK,用GBK解从文件读取的数据char[] data = new char[10];int len;while((len = isr.read(data)) != -1){System.out.println(new String(data,0,len));}isr.close();fis.close();}@Testpublic void test01()throws IOException {FileReader fr = new FileReader("1.txt");//1.txt的编码是GBKchar[] data = new char[10];int len;while((len = fr.read(data)) != -1){System.out.println(new String(data,0,len));}fr.close();}
}

10.对象序列化和反序列化

IO流:

        字节流,可以读/写  图片,视频,文本等。

        字符流,只能读/写  文本(字符串,char)

如果要处理Java程序中的各种数据类型的数据,就需要用

DataInputStream:读Java各种基本数据类型的数据+String。

DataOutputStream:写Java各种基本数据类型的数据+String。

ObjectInputStream:读Java任意类型,包括对象。

ObjectOutputStream:写Java任意类型,包括对象。

序列化:把Java对象直接转化为字节序列过程,这个过程称为序列化,要求这个对象的类型必须实现java.io.Serializable接口,否则无法支持序列化。

反序列化:把字节序列转为Java对象的过程。

反序列化失败问题:

        当数据已经序列化好之后,对Student类做了修改,导致反序列化的代码运行失败;报错:        java.io.InvalidClassException(无效的类异常): com.atguigu.object.Student;

local(本地) class incompatible(不相容的;矛盾的;):stream(流) classdesc(类描述) serialVersionUID(序列化版本ID) = -3979294235569238736,local(本地) class serialVersionUID(序列化版本ID) = 5212389082514962991

        如果在类声明并实现java.io.Serializable接口时,如果没有固定serialVersionUID(序列化版本ID)值,那么会在每一次编译类的时候,会产生一个新的serialVersionUID(序列化版本ID)值。

        修改Student类,就会导致类重新编译。

如何解决这个问题?

        在类声明并实现java.io.Serializable接口时,固定 serialVersionUID(序列化版本ID)值。

不参与序列化的字段:

① transient修饰的属性字段

② static修饰的属性字段

Student类代码:

public class Student implements Serializable {private static final long serialVersionUID = -3979294235569238736L;private static int total = 1;private int id;private String name;private int age;private transient int score;//transient瞬时的,临时,序列化时遇到transient标记的字段,会忽略它private String tel;public Student(String name, int age, int score) {id=total++;this.name = name;this.age = age;this.score = score;}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;}public int getScore() {return score;}public void setScore(int score) {this.score = score;}public static int getTotal() {return total;}public static void setTotal(int total) {Student.total = total;}@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", score=" + score +", tel='" + tel + '\'' +'}';}
}

测试代码:TestObjectIO类:

public class TestObjectIO {@Testpublic void test04()throws Exception{//在stu.dat文件中增加一个对象FileInputStream fis = new FileInputStream("stu.dat");ObjectInputStream ois = new ObjectInputStream(fis);ArrayList<Student> list = (ArrayList<Student>) ois.readObject();list.add(new Student("沈彦",22,100));System.out.println(list);ois.close();fis.close();FileOutputStream fos = new FileOutputStream("stu.dat");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(list);oos.close();fos.close();}@Testpublic void test03()throws Exception{Student s1 = new Student("李四",24,88);Student s2= new Student("王五",24,90);Student s3 = new Student("赵六",24,100);ArrayList<Student> list = new ArrayList<>();list.add(s1);list.add(s2);list.add(s3);//同时序列化多个对象时,怎么办?//放到集合里面。FileOutputStream fos = new FileOutputStream("stu.dat");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(list);oos.close();fos.close();}@Testpublic void test02() throws IOException, ClassNotFoundException {Student s1 = new Student("李四",24,0);Student s2= new Student("王五",24,0);Student s3 = new Student("赵六",24,0);System.out.println(Student.getTotal());FileInputStream fis = new FileInputStream("game.dat");ObjectInputStream ois = new ObjectInputStream(fis);//读的顺序要与写的顺序一致String name = ois.readUTF();int age = ois.readInt();char gender = ois.readChar();int energy = ois.readInt();double price = ois.readDouble();boolean relive = ois.readBoolean();Object object = ois.readObject();System.out.println("name = " + name);System.out.println("age = " + age);System.out.println("gender = " + gender);System.out.println("energy = " + energy);System.out.println("price = " + price);System.out.println("relive = " + relive);System.out.println("object = " + object);System.out.println(Student.getTotal());ois.close();fis.close();}@Testpublic void test01()throws IOException {String name ="巫师";int age = 300;char gender ='男';int energy = 5000;double price = 75.5;boolean relive = true;Student stu = new Student("张三",23,89);//输出到文件FileOutputStream fos = new FileOutputStream("game.dat");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeUTF(name);oos.writeInt(age);oos.writeChar(gender);oos.writeInt(energy);oos.writeDouble(price);oos.writeBoolean(relive);oos.writeObject(stu);//java.io.NotSerializableException: com.atguigu.object.StudentSystem.out.println(Student.getTotal());oos.close();fos.close();}
}

11.Scanner类与IO流

java.util.Scanner类,不是只能用来从键盘输入数据,它可以从任意流中扫描数据。

如果没有修改过System.in,那么Scanner  input = new Scanner(System.in);默认是从键盘输入

在创建Scanner对象时,指定了其他的IO流,会从其他IO流中读取文本数据。

示例代码:

public class TestScanner {@Testpublic void test01()throws Exception{//从2.txt文件中获取数据Scanner input = new Scanner(new FileInputStream("2.txt"));while(input.hasNextLine()){String line = input.nextLine();System.out.println(line);}input.close();}

12.System类与IO流

System类中有三个常量对象:

① public final static InputStream in = null;

② public final static PrintStream out = null;

③ public final static PrintStream err = null;

常量对象正常是以大写字母开头,但System的三个常量对象是以小写字母开头,而且用 final 修饰的变量是不可变的,但是System类的底层有set方法,可以设置in,out,err的值,即下面方法:

① public static void setIn(InputStream in)

② public static void setOut(PrintStream out)

③ public static void setErr(PrintStream err)

底层是用native修饰的,来更改in,out,err的值。

private static native void setIn0(InputStream in);

private static native void setOut0(PrintStream out);

private static native void setErr0(PrintStream err);

示例代码如下:

public class TestSystem {@Testpublic void test02()throws IOException {System.setIn(new FileInputStream("2.txt"));//此时默认是从 2.txt 文件中获取数据Scanner input = new Scanner(System.in);while(input.hasNextLine()){String line = input.nextLine();System.out.println(line);}input.close();}@Testpublic void test01(){InputStream in = System.in;PrintStream out = System.out;PrintStream err = System.err;out.println("hello");err.println("world");}
}

13.PrintStream类

PrintStream打印流:

        ① 可以支持各种数据类型的打印

        ② 可以支持输出换行,输出不换行,还支持格式化输出

print(xxx);输出不换行

println(xxx);输出换行

printf(xx);格式化输出

        ③ 输出可以输出到各种其他IO流中,也可以直接输出到文件,也可以输出到控制台。

public class TestPrintStream {@Testpublic void test01()throws IOException {//以 UTF-8 的编码形式,输入到 3.txt 文件中PrintStream ps = new PrintStream("3.txt","UTF-8");ps.println("hello");ps.println("world");ps.close();}
}

14.try...catch新语法自动关闭IO流

JDK1.7版本之前:

代码如下:

public class TestTryCatch {@Testpublic void test01(){FileInputStream fis = null;InputStreamReader isr = null;BufferedReader br = null;FileOutputStream fos = null;OutputStreamWriter osw = null;BufferedWriter bw = null;try {//GBK编码的1.txt,复制UTF-8的2.txt文件fis = new FileInputStream("1.txt");isr = new InputStreamReader(fis,"GBK");br = new BufferedReader(isr);fos = new FileOutputStream("2.txt");osw = new OutputStreamWriter(fos, "UTF-8");bw = new BufferedWriter(osw);String line;while((line = br.readLine()) != null){bw.write(line);bw.newLine();}} catch (IOException e) {e.printStackTrace();}finally{/*           try {bw.close();osw.close();fos.close();br.close();isr.close();fis.close();} catch (IOException e) {e.printStackTrace();}*/try {if(bw!=null) {bw.close();}} catch (IOException e) {e.printStackTrace();}try {if(osw != null) {osw.close();}} catch (IOException e) {e.printStackTrace();}try {if(fos != null){fos.close();}} catch (IOException e) {e.printStackTrace();}try {if(br != null){br.close();}} catch (IOException e) {e.printStackTrace();}try {if(isr != null) {isr.close();}} catch (IOException e) {e.printStackTrace();}try {if(fis != null) {fis.close();}} catch (IOException e) {e.printStackTrace();}}}
}

这种try...catch非常麻烦,finally里面如果有异常的话,需要一个一个来try...catch很麻烦,所以JDK1.7之后,就有了新的方法

try(需要自动关闭的资源的对象的声明和创建){

        需要异常检查的业务逻辑代码

}catch(异常类型 e){

        异常处理代码

}finally{

        这里写的是其他的必须执行的代码,但是不是资源关闭的代码。

}

try()中不是所有类型的对象的声明和创建都可以放进去的,只能放实现了 AutoClose 接口的类型

只有在try()中声明的才会自动关闭。不在这里声明的不会自动关闭。

在try()中声明的变量,会自动加 final

public class TestTryCatch {@Testpublic void test02(){//GBK编码的1.txt,复制UTF-8的2.txt文件try(FileInputStream fis = new FileInputStream("1.txt");InputStreamReader isr = new InputStreamReader(fis,"GBK");BufferedReader br = new BufferedReader(isr);FileOutputStream fos = new FileOutputStream("2.txt");OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");BufferedWriter bw = new BufferedWriter(osw);) {String line;while ((line = br.readLine()) != null) {bw.write(line);bw.newLine();}}catch(IOException e){e.printStackTrace();}//自动关闭,不需要写了/* bw.close();osw.close();fos.close();br.close();isr.close();fis.close();*/}
}

http://www.ppmy.cn/news/85629.html

相关文章

2023-05-25 关于不将重点放在问题解决反而放在维护自尊上-反思

摘要: 最近在做事的时候遇到将重点放在维护自身上的&#xff0c;十分有碍于问题的解决。 为了对这个现象引起足够的警惕&#xff0c;本文对这个现象做分析。 表象: 直接的表现其实非常容易辨认&#xff0c;因为这种行为的目的在潜意识中包含着一种毁灭对方战胜对方的企图, 这…

【新日语】第4課 これわ何ですか?

基本句例 此物乃教科书是也。那物乃 MP3 player 是也。这物乃公司的照相机是也。那物乃王的车&#x1f697;是也。这幢建筑乃总部公司大楼是也。那个人乃Michael是也。田中乃公司职员&#xff0c;亦日本数据通信的职员是也。那一位乃佐藤老师&#xff0c;亦小学校的教师是也。…

水平触发与边沿触发

Epoll 的工作模式 LT 模式 &#xff08;水平触发&#xff09; LT&#xff08;level - triggered&#xff09;是缺省的工作方式&#xff0c;并且同时支持 block 和 no-block socket。在这种做法中&#xff0c;内核告诉你一个文件描述符是否就绪了&#xff0c;然后你可以对这个就…

无领导群面如何精准得分?

其实进入面试环节&#xff0c;大家脱颖而出的机会可以视为均等的&#xff0c;在面试环节表现突出&#xff0c;即使学历经历有些劣势&#xff0c;一样可以拿到offer。 但缺乏一定无领导技巧&#xff0c;在面对高学历对手时&#xff0c;心理压力也比较大。今天就面试环节“刷人最…

C语言中的extern关键字

C语言中的extern关键字 作用扩展 作用 在C语言中&#xff0c;extern关键字用于声明外部变量或函数。它通常用于在一个源文件中引用另一个源文件中定义的全局变量或函数。 当您在一个源文件中声明一个变量或函数为extern时&#xff0c;它表示该变量或函数的定义存在于另一个源…

智能家居新选择,雷达感应器成品方案,人体存在感应控制照明联动

在现代社会&#xff0c;随着智能化技术的不断推进&#xff0c;人体存在感应控制照明已经成为了一项必备的技术手段。 ISE101T-5雷达感应器成品&#xff0c;作为一款拥有多种特点和优势的设备&#xff0c;不仅可以感应到室内的人体存在&#xff0c;还能够通过控制照明实现更为舒…

Unity DOTS从原理到应用

什么是DOTS? Unity的DOTS&#xff08;Data-Oriented Technology Stack&#xff09;是一种数据驱动的技术堆栈&#xff0c;旨在提高Unity游戏引擎的性能和可伸缩性。它使用C# Job System&#xff0c;Burst Compiler和Entity Component System&#xff08;ECS&#xff09;来组合…

生成VLC 及其模块的全面帮助

使用vlc.exe -H命令生成VLC帮助文件vlc-help.txt -h, --help, --no-help 打印 VLC 帮助 (可以和 --advanced 和 --help-verbose 一起使用) (默认禁用) -H, --full-help, --no-full-help VLC 及…