File Transfer - JavaIO Stream

Preface

Recently, I found that I am not proficient enough in IO flow, so I spent half a day systematically practicing my hand with a set of lessons in Mou class. The notes and codes are arranged as follows, which is also mixed with my own understanding. Course Links
The code is uploaded to my GitHub in chapters:

Overview of Brain Map (Brief):

Note: Each chapter is preceded by a brain map of this chapter, which is convenient for review and can also be viewed as needed.

Chapter 1 Coding of Documents


Text files are byte sequences, which can be arbitrarily encoded byte sequences.
So everything starts with the coding of the file.

utf-8 3 bytes in Chinese and 1 byte in English
gbk Chinese 2 bytes English 1 byte
utf-16be Chinese 2 bytes English 2 bytes

Program validation:

        String s = "Jia Ming ABC";

        //Project default utf-8, Chinese takes up 3 bytes
        byte[] bytes1 = s.getBytes("utf-8");
        for (byte b : bytes1) {
            //Display bytes (converted to int) in hexadecimal format
            System.out.print(Integer.toHexString(b & 0xff) + " ");
        }
        System.out.println();

        //gbk encoding takes 2 bytes in Chinese and 1 byte in English
        byte[] gbkBytes = s.getBytes("gbk");
        for (byte gbkByte : gbkBytes) {
            System.out.print(Integer.toHexString(gbkByte & 0xff) + " ");
        }
        System.out.println();

        //java is a two-byte code utf-16be
        byte[] javaBytes = s.getBytes("utf-16be");
        //utf-16be takes 2 bytes in Chinese and 2 bytes in English.
        for (byte javaByte : javaBytes) {
            System.out.print(Integer.toHexString(javaByte & 0xff) + " ");
        }
        System.out.println();

The output results are as follows:

When your byte sequence is some kind of encoding, you want to change the byte sequence into a string at this time. You also need to use this encoding method, otherwise there will be scrambling code.

Chapter 2 Use of File Classes

Introduction to 2-1 File Class API s

_File class is only used to represent the information (name, size, etc.) of the file (directory), but can not be used to access the content of the file.

create a file

  • File file = new File("E:\\Jeremy");
    // Use double orthogonal slant (need to be escaped) or monoclinic in the path
  • File file2 = new File("E:"+File.separator+"Jeremy");
    // It is recommended that this be used, windows and linux systems are universal
  • File file 3 = new File ("E:"+File.separator+ "Jeremy", "Diary.txt");

File operation

  • file.isDirectory();
  • file.exists();
  • file.createNewFile();
  • file.mkdir();
  • file.delete();
  • file.isFile()// Is it a file?

Output Path

  • Contents of System.out.println(file); // file.toString()
  • file.getAbsoluteFile(); //file absolute path
  • file.getName(); name of // file
  • file.getParent(); //file parent directory name
  • file.getParentFile().getAbsoluteFile(); //file parent directory absolute path

2-2 traversal directory

  • String[] fileNames = dir.list(); // Returns an array of strings with a direct subdirectory name
  • File[] files = dir.listFiles(); // Returns the abstraction of direct subdirectory files
  • If you want to traverse directories under subdirectories, you need to construct File objects for recursive operations.
	/*
     * List all files in a specified directory, including its subdirectories
     */
    public static void listDirectory(File dir) throws IOException {
     	if (!dir.exists()) {
            throw new IllegalArgumentException("Catalog:" + dir + "Non-existent.");
        }
        if (!dir.isDirectory()) {
            throw new IllegalArgumentException(dir + "Not a catalogue");
        }
        File[] files = dir.listFiles();

        if(files != null && files.length != 0){
            for (File file : files) {
                if(file.isDirectory()){
                    //recursion
                    listDirectory(file);
                }
                System.out.println(file.getAbsolutePath());
            }
        }
    }

Chapter 3 Use of Random AccessFile Class


_Random Access File Java provides access to the contents of files, which can read or write files.
Random Access File supports random access to files and can access any location of files.

  • java file model
    _The file on the hard disk is stored by byte byte, which is a collection of data.

  • File Opening Mode
    There are two modes of "rw" (read-write) and "r" (read-only).
      RandomAccessFile raf = new RandomAccessFile(file,rw);

  • field name pointer
    _Open the file pointer at the beginning pointer = 0;

  • Writing Method
    _raf.write(int);Write only one byte (last 8 bits), while pointing to the next location, ready to write again

  • Reading methods
    _int b = raf.read(); read a byte

  • Close the file when it's read and written (Oracle official instructions)

		RandomAccessFile raf = new RandomAccessFile(file,"rw");
		
        raf.write('A');//Write only one byte.
        System.out.println(raf.getFilePointer());

        int i = 0x7ffffff;
        //Write only one byte at a time, and if you want to write i in, you have to write it four times.
/*      raf.write(i>>>24);//High 8 digits
        raf.write(i>>>16);
        raf.write(i>>>8);
        raf.write(i);
        System.out.println(raf.getFilePointer());*/

        //Write an int directly
        raf.writeInt(i);

        String s = "in";
        byte[] gbk = s.getBytes("gbk");
        raf.write(gbk);
        System.out.println(raf.length());

        //To read a file, you must move the pointer to the head
        raf.seek(0);
        //Read all the contents of the file into a byte array at one time
        byte[] buf = new byte[(int)raf.length()];
        raf.read(buf);

        //Output as a string
        System.out.println(Arrays.toString(buf));
        //Output in hexadecimal code
        for (byte b : buf) {
            System.out.print(Integer.toHexString(b & 0xff)+ " ");
        }

        raf.close();

Chapter 4 Byte Stream

FileInputStream-1 FileInputStream-1, FileInputStream-1, FileInputStream-1, FileInputStream-1, FileInputStream-1, FileInputStream-1

  • IO Flow Classification

    • Input and output streams
    • Byte stream, character stream
  • Byte stream

    • InputStream,OutputStream
      • InputStream abstracts the way an application reads data
      • OutputStream abstracts the way an application writes data
    • EOF = End read - 1 read to the end
    • Basic Method of Input Flow
      • int b = in.read(); // Read a byte unsigned to fill in the lower eight bits of int
      • in.read(byte[] buf); // read data to fill in the byte array buf
      • in.read(byte[] buf,int start,int size);
        // Read the data to the byte array buf, starting at the start of buf, and store the size-length data
    • Basic Method of Output Flow
      • out.write(int b); // Write a byte to stream, low 8 bits of B
      • out.write(byte[] buf); // Write all buf byte arrays to the stream
      • out.write(byte[]buf,int start,int size);
    • FileInputStream_Specifically realizes reading data on files

FileInputStream-2, FileInputStream-2, FileInputStream-2, FileInputStream-2, FileInputStream-2, FileInputStream-2, FileInputStream-2

  • Two Methods of Reading Documents
    - While ((b = in. read ())!= - 1) // Read directly into int b
    - while ((bytes = in.read(buf,0,buf.length))!=-1)
    // Reads into the byte array buf, and when the byte array is full, the while loop is re-executed.

File Output Stream, File Output Stream, File Output Stream, 4-3 byte stream

FileOutoutStream implements the method of writing byte data to files

4-4 byte stream-valued data input-output stream

  • DataOutputStream & DataInputStream
    • Expansion of the "stream" function makes it easier to read int, long, character and other types of data.
    • DataOutputSteam: writeInt() / writeDouble() / writeUTF()
    • Initialization: Data Output Stream DOS = new Data Output Stream (new File Output Stream (file));
		String file = "demo/dos.dat";
        DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
        dos.writeInt(10);
        dos.writeInt(-10);
        dos.writeLong(10l);
        dos.writeDouble(10.5);
        //Write with utf-8 code
        dos.writeUTF("China");
        //Write with utf-16be code
        dos.writeChars("China");

        dos.close();

4-5 byte buffer stream

  • BufferedInputStream&BufferedOutputStream
    These two stream classes provide buffer operation for IO. When opening a file for write or read operation, buffer will be added. This flow mode improves the performance of IO.

  • Putting input into a file from an application program is equivalent to pouring a cylinder of water into another cylinder:

    • The FileOutputStream write() method is equivalent to "transferring" water drop by drop.
    • The DataOutputStream writeXxx() method would be more convenient, equivalent to "transferring" water one by one.
    • Buffered Output Stream write() method is more convenient, which is equivalent to putting a ladle in a barrel, and then pouring it into another cylinder from the barrel.
  • Performance comparison of three ways of copying files

    /*
     * Copy files, using buffered byte streams
     */
    public static void copyFileByBuffer(File srcFile,File destFile)throws IOException{
        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        int b;

        while ((b=bin.read())!=-1){
            bos.write(b);
            bos.flush();//Must be added
        }
        bin.close();
        bos.close();
    }
    
	/*
     * Copy files, read bytes in batches
     */
    public static void copyFile(File srcFile,File destFile)throws IOException{
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);

        int b;
        byte[] buf=new byte[8*1024];//Declare an 8M array
        while ((b=in.read(buf,0,buf.length))!=-1){
            out.write(buf,0,b);
            out.flush();//Better add
        }
        in.close();
        out.close();
    }

    /*
     * Copy files, read byte by byte
     */
    public static void copyFileByByte(File srcFile,File destFile)throws IOException{
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);

        int b;
        while ((b=in.read())!=-1){
            out.write(b);
            out.flush();//Better add
        }
        in.close();
        out.close();
    }

Copy a 10M audio file and test the execution time:

Chapter 5 Character Stream

5-1 byte character converter

  • InputStreamReader & OutputStreamWriter
  • Construction method
    InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"gbk");
  • Coding issues
    • Read in accordance with the encoding of the file being read
    • The encoding method for writing is the encoding of the generated file.
  • Text and Text Documents
    • java's text (char) is a 16-bit unsigned integer, unicode encoding of characters (double byte encoding)
    • The file is byte byte byte... Data Sequence
    • Text file is the result of serialization of text (char) sequence into byte storage according to some coding scheme (utf-8,utf-16be,gbk)
  • The character stream (Reader Writer) operates on text files
    • Character processing, one character at a time
    • The bottom of the character is still the basic sequence of bytes.
    • Basic Implementation of Character Stream
      • InputStreamReader completes byte stream parsing to char stream, parsing according to coding
      • Output Stream Writer provides char flow to byte flow, which is processed by encoding

File Read-Write Stream of 5-2 Character Stream

  • FileReader &FileWriter
  • Construction method
    FileReader fr = new FileReader("fileName");
    • Without encoding parameters, can only parse files with the same item encoding
		//Character Stream Implementing File Copy
		FileReader fr = new FileReader("D:\\Thunderbolt Download\\idea Keyboard shortcut 1.txt");
        FileWriter fw = new FileWriter("D:\\Thunderbolt Download\\idea Keyboard shortcut 2.txt");

        char[] buffer = new char[2056];
        int c;
        while ((c = fr.read(buffer,0,buffer.length))!=-1){
            System.out.println(new String(buffer,0,c));

            fw.write(buffer,0,c);
            fw.flush();
        }

        fr.close();
        fw.close();

Filters for 5-3 character streams

  • BufferedReader & BufferedWriter/PrintWriter
  • Construction method
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("fileName"), "gbk"));
  • BufferedReader readLine() Read one line at a time
  • BufferedWriter/PrintWriter_Write a line
    • Generally, PrintWriter is more convenient and convenient for line change.
		//Character Stream Filter for File Copy
		BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream("D:\\Thunderbolt Download\\idea Shortcut keys.txt"), "gbk"));

        PrintWriter pw = new PrintWriter("D:\\\\Thunderbolt Download\\\\idea Keyboard shortcut 4.txt");

        String line;
        while ((line = br.readLine()) != null) {
           System.out.println(line);//Reading one line at a time does not recognize newlines
           pw.println(line);
           pw.flush();
        }

        br.close();
        pw.close();

Chapter 6 Object serialization and deserialization

6-1 Serialization Basic Operations

  • Object Serialization (Object byte Sequence) and Deserialization (byte Sequence Object)
    • Object Output Stream - write Object
    • Object InputStream - readObject
    • Serializable
      • Objects must be serialized by implementing serialization interfaces, otherwise exceptions will occur
      • This interface, no way, is just a standard.

6-2 transient and ArrayList Source Code Analysis

  • transient keyword
    This element will not be serialized by default in jvm it can be serialized by itself
    • writeObject()
    • readObject()
  • Application of transient in ArrayList
    Manual serialization of array elements Invalid elements need not be serialized
	//ArrayList source code
    private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException{
        // Write out element counts and anything hidden.
        int expectedModCount = modCount;
        s.defaultWriteObject();

        // Take list size as capacity
        s.writeInt(size);

        // Write all the elements in the right order
        for (int i=0; i<size; i++) {
            s.writeObject(elementData[i]);
        }

        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }
   
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        elementData = EMPTY_ELEMENTDATA;

        // Read the list size and anything hidden
        s.defaultReadObject();

        // Read list capacity
        s.readInt(); // ignored

        if (size > 0) {
            // Like clone(), arrays are allocated according to size rather than capacity
            ensureCapacityInternal(size);

            Object[] a = elementData;
            // Read all elements
            for (int i=0; i<size; i++) {
                a[i] = s.readObject();
            }
        }
    }

6-3 Sequencing Neutron Parent Constructor Problem

  • A class implements a serialization interface, so its subclasses can be serialized
  • Does deserialization call the constructor of the parent class?
    If its parent class does not implement a serialized interface, the constructor of its parent class is called

Brain Map (Complete)

Tags: Programming encoding Java github Windows

Posted on Tue, 13 Aug 2019 05:08:17 -0700 by richinsc