一、IO流
你要弄懂所谓输入输出针对的对象是谁,懂了你就可以更好的接受IO流的学习。
输入:将磁盘文件输入CPU里的内存(对应读文件)
输出:将内存里的数据输出到磁盘文件(对应写文件)
我们是站在电脑的角度(或者内存的角度来看)
以上你还不理解,那接下来我说的你一定熟悉。平时我们刷代码题时,是不是有输入和输出的要求。从键盘输入,从屏幕显示(输出),你看输入的对象是不是内存,输出给外设。
输入针对的是将数据输入内存,输出针对的是将数据从内存输出到磁盘文件/网络/屏幕等。
以下就是我要讲解的IO流内容(初学者要掌握的内存):
1、字节流
(1)字节输入流
--InputStream
介绍:public abstract class InputStream extends Object implements Closeable
This abstract class is the superclass of all classes representing an input stream of bytes.
Applications that need to define a subclass of InputStream
must always provide a method that returns the next byte of input.
这个你只需要记住它是一个抽象类,是所有输入字节流的类的超类(基类或父类)
--FileInputStream
介绍:public class FileInputStream extends InputStream
A FileInputStream
obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream
is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader
.
对文件的操作,按字节将文件数据输入内存,继承了父类(InputStream)的方法。可以用多态的写法定义对象。
--BufferedInputStream
介绍:public class BufferedInputStream extends FilterInputStream
A BufferedInputStream
adds functionality to another input stream-namely, the ability to buffer the input and to support the mark
and reset
methods. When the BufferedInputStream
is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark
operation remembers a point in the input stream and the reset
operation causes all the bytes read since the most recent mark
operation to be reread before new bytes are taken from the contained input stream.
也是对文件的操作,按字节将文件数据输入内存,继承了父类(FileInputStream)的方法。因为我们通常用的的是BufferedInputStream重写的方法,不用多态的写法定义对象。
(2)字节输出流
--OutputStream
介绍:public abstract class OutputStream extends Object implements Closeable, Flushable
This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink.
Applications that need to define a subclass of OutputStream
must always provide at least a method that writes one byte of output.
这个你也只需要记住它是一个抽象类,是所有输出字节流的类的超类(基类或父类)
--FileOutputStream
介绍:public class FileOutputStream extends OutputStream
A file output stream is an output stream for writing data to a File
or to a FileDescriptor
. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream
(or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
FileOutputStream
is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter
.
对文件的操作,按字节将内存数据写入文件,继承了父类(OutputStream)的方法。可以用多态的写法定义对象。
--BufferedOutputStream
介绍:public class BufferedOutputStream extends FilterOutputStream
The class implements a buffered output stream. By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.
也是对文件的操作,按字节将内存数据写入文件,继承了父类(FileOutputStream)的方法。因为我们通常用的的是BufferedOutputStream重写的方法,不用多态的写法定义对象。
2、 字符流
(1)字符输入流
--Reader
介绍:public abstract class Reader extends Object implements Readable, Closeable
Abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
这个你只需要记住它是一个抽象类,是所有输入字符流的类的超类(基类或父类)
--FileReader
介绍:public class FileReader extends InputStreamReader
Reads text from character files using a default buffer size. Decoding from bytes to characters uses either a specified charset or the default charset.
The FileReader
is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream
.
对文件的操作,按字符将文件数据输入内存。
--BufferedReader
介绍:public class BufferedReader extends Reader
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
性能优化,对文件的操作,按字符将文件数据输入内存。
(2)字符输出流
--Writer
介绍:public abstract class Writer extends Object implements Appendable, Closeable, Flushable
Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
这个你也只需要记住它是一个抽象类,是所有输出字符流的类的超类(基类或父类)
--FileWriter
介绍:public class FileWriter extends OutputStreamWriter
Writes text to character files using a default buffer size. Encoding from characters to bytes uses either a specified charset or the default charset.
Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileWriter
(or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.
对文件的操作,按字符将内存数据输入文件。
--BufferedWriter
介绍:public class BufferedWriter extends Writer
Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.
性能优化,对文件的操作,按字符将内存数据输入文件。
二、文件的读写操作
1、读
(1)定义一个输入流对象用来与源文件接通
(2)开始读取文件中的数据
(3)关闭流
2、写
(1)定义一个输出流对象用来与源文件接通
(2)开始将数据写入文件
(3)关闭流
三、异常处理
1、直接抛出异常
2、try-catch-finally
3、try-with-resource
四、随机数
1、Random
Random r = new Random();
r.nextInt(10); //随机生成0-9的数
nextInt()里面的参数n,代表生成[0,n)的数。
2、Math.Random
int num = (int)Math.Random()*10;//num是随机生成0-9的数
Math.Random()随机生成double类型[0,1)的数,乘以n再强制转换成int类型,即可随机生成[0,n)的数。