FileInputStream及FileReader

FileInputStream及Filereader

Some methods:

abstract int read()
reads the next byte of data from the input stream.
int read(byte[] b)
reads some bytes from the input stream and stores them in the buffer b
void close()
closes this input stream and releases any system resources associated with the stream.
public class FileInputStream extends InputStreamA FileInputStream obtains input bytes from a file in the file system. What files are available depends on the host environment.
FileInputStream is used to read raw byte streams such as image data. To read strings, consider using FileReader.
Construction method:
FileInputStream (File file)
creates a FileInputStream by opening the connection with the actual file, the file is named by the File object file in the file system.
FileInputStream(String name)
creates a FileInputStream by opening a connection with the actual file. The file is named by the path name name in the file system.
The role of the constructor:
a FileInputStream object
will be created, and the FileInputStream object will be assigned to the file in the constructor

Creation steps:

1. Create a FileinputStream object, pass the writing destination in the construction method
2. Call the method in the FileinputStream object to write the data to the file
3. Release resources (stream usage will take up a certain amount of memory, after use Clear the memory to improve the efficiency of use)

Code block

public class Demo01InputStream {
    public static void main(String[] args) throws IOException {
        //创建一个FileinputStream对象,构造方法中传递写入的目的地
        FileInputStream fos=new FileInputStream("src\\cn\\itcast\\File\\123\\tang1.txt");//没有文件创建文件,有了直接写入!
        //调用FileinputStream对象中的方法,把数据写入到文件中
        int read = fos.read();//一次读取一个字节
        System.out.println(read);//65
        read=fos.read();
        System.out.println(read);//66
        read=fos.read();
        System.out.println(read);//67
        //释放资源(流使用会占用一定的内存,使用完毕后要把内存清空,提高使用效率)
        /*发现是一个重复的过程,所以可以使用循环,但是又不知道长度,已经知道没有字节用-1表示,则可以不严谨难过的创造一个循环
        */
        /*int len;
        while((len=fos.read())!=-1)
        {
            System.out.println((char) len);//读取中文的字符出现乱码
        }
        fos.close();*/
        /*public abstract class Reader用于读取字符流的抽象类。 子类必须实现的唯一方法是read(char [],int,int)和close()。
        * public class FileReader extends InputStreamReader extends Reader阅读字符文件的便利课。
          该类的构造函数假定默认字符编码和默认字节缓冲区大小是适当的。 要自己指定这些值,请在FileInputStream上构造一个InputStreamReader。
          作用:把硬盘文件中的数据以字符的方式读取到内存中
          * 构造方法:
          * FileReader(File file)
           创建一个新的 FileReader ,给出 File读取。

            FileReader(String fileName)
            创建一个新的 FileReader ,给定要读取的文件的名称。
             */
        /*字符输入流的使用步骤:
        * 1.创建一个FileReader对象,构造方法中绑定要读取的数据源
        * 2.使用FileReader对象中的read读取文件
        * 3.释放资源*/
        FileReader fr=new FileReader("src\\cn\\itcast\\File\\123\\tang1.txt");
        /*int len1=0;
        while ((len1=fr.read())!=-1)
        {
            System.out.print((char) len1);
        }*/
        /*使用字符数组提高读取效率*/
        int len1=0;
        char cs[]=new char[1024];
        while ((len1=fr.read(cs))!=-1)
        {
            /*String(char[] value)
分配一个新的 String ,以便它表示当前包含在字符数组参数中的字符序列。
String(char[] value, int offset, int count)
分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。
*/
            System.out.print(new String(cs,0,len1));
        }
        fr.close();
    }

}

annotation:

The above introduces a reading method in units of 1024 bytes: in order to enable readers to understand better, the following explanation and examples are given:
int read(byte[] b)
read some bytes from the input stream Count them and store them in the buffer b.
Clear two things:
1. Create a byte[] array to play
a buffer role.
The length of multiple byte arrays read each time from storage is generally defined as 1024 (1kb ) Or an integer multiple of 1024
2. What is the return value of the method int?
Number of valid bytes read each time

Creation steps:

1. Create a FileinputStream object, pass the writing destination in the construction method
2. Call the method in the FileinputStream object to write the data to the file
3. Release resources (stream usage will take up a certain amount of memory, after use Clear the memory to improve the efficiency of use)

Code piece

public class Demo02InputStream {
    public static void main(String[] args) throws IOException {
        //创建一个FileinputStream对象,构造方法中传递写入的目的地
        FileInputStream fos=new FileInputStream("src\\cn\\itcast\\File\\123\\tang1.txt");//没有文件创建文件,有了直接写入!
        //调用FileinputStream对象中的方法,把数据写入到文件中
        //byte bytes[]={1,2,3};长度为3
        /*byte bytes[]=new byte[10];//长度为10
        int read = fos.read(bytes);//返回的是每次读取的个数,并将读取的数存放在bytes数组中,存放形式根据ASCII和gbk转换为2进制再转换为十进制
        System.out.println(read);//10
        System.out.println(Arrays.toString(bytes));//[65, 66, 67, 68, 69, 66, 67, -28, -67, -96]
        //String(byte[] bytes);通过使用平台的默认字符集解码指定的字节数组来构造新的 String 把一个字节数组转换为字符串,0-127根据ASCII码,其他根据gbk
        // String(byte[] bytes, int offset, int length)
        //通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。部分字符数组转变,offset表示起始位置,length表示转换的长度!。
        System.out.println(new String(bytes));
         read = fos.read(bytes);
        System.out.println(read);
        System.out.println(Arrays.toString(bytes));
        System.out.println(new String(bytes));*/
        /*发现规律*/
        byte bytes[]=new byte[1024];
        int len=0;
        while ((len=fos.read(bytes))!=-1)//-1代表数组啥都读取不到
        {
            System.out.println( new String(bytes,0,len));
            //通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
            // 部分字符数组转变,offset表示起始位置,length表示转换的长度!。
        }
        //3.释放资源(流使用会占用一定的内存,使用完毕后要把内存清空,提高使用效率)
        fos.close();
    }

}

Schematic (picture from the Internet):

Insert picture description here

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/104281069