Learn java - Day12 - InputStream details of IO stream

Keywords: Java encoding Attribute REST

1 IO

  • BigDecimal/BigInteger

Summary:

  • BigDecimal: used to solve precise floating-point operations.
  • BigInteger: used to solve very large integer operations.

Create object:

  • BigDecimal.valueOf(2);
  • Common method: add(BigDecimal bd): add
  • Subtract (BigDecimal BD): subtract
  • multiply(BigDecimal bd): do multiplication
  • divide(BigDecimal bd): divide
  • divide(BigDecimal bd, reserved digits, rounding method): used when the division is endless
  • Setscale: the same as above
  • pow(int n): several powers of data
public class Test2_BigD {
       public static void main(String[] args) {
              double a = new Scanner(System.in).nextDouble();
              double b = new Scanner(System.in).nextDouble();
              System.out.println(a+b);
              System.out.println(a-b);
              System.out.println(a*b);
              System.out.println(a/b);//Imprecise              
              System.out.println("===The division above is not accurate===");
              BigDecimal bd1 = BigDecimal.valueOf(a);
              BigDecimal bd2 = BigDecimal.valueOf(b);
              BigDecimal bd3;
              bd3=bd1.add(bd2);
              System.out.println(bd3.doubleValue());              
              bd3=bd1.subtract(bd2);
              System.out.println(bd3.doubleValue());              
              bd3=bd1.multiply(bd2);
              System.out.println(bd3.doubleValue());
              
//           bd3=bd1.divide(bd2); / / endless error division is reported
              //Reserved digits and rounding method
              bd3=bd1.divide(bd2,5,BigDecimal.ROUND_HALF_UP);
              bd3=bd3.setScale(2, BigDecimal.ROUND_HALF_UP);//Keep the two position.
              System.out.println(bd3.doubleValue());              
       }
}
  • IO introduction

Inheritance structure:

  • The process of inputting (reading) and outputting (writing) relative to a program.
  • In Java, it can be divided into byte stream and character stream according to different data units
    File

Byte stream: for binary

> InputStream 
> --FileInputStream
> --BufferedInputStream 
> --ObjectInputStream OutputStream
> --FileOutputStream
> --BufferedOutputStream
> --ObjectOutputStream

Character stream: for text files. Reading and writing are prone to garbled code. It is better to specify utf-8 as the encoding set when reading and writing

>  Writer
>   	-- BufferedWriter
>    	-- OutputStreamWriter Reader
>     	-- BufferedReader
>      	-- InputStreamReader
>       	-- PrintWriter/PrintStream
  • The concept of flow
  • The reading and writing of data is abstracted into data and flows in the pipeline.
  • Flow can only flow in one direction
  • Input stream is used to read in
  • Output stream is used to write Out out
  • Data can only be read and written once in sequence from beginning to end
  • File file stream

Summary:

  • Encapsulates a disk path string, which can be operated once. It can be used to encapsulate file path, folder path and nonexistent path.

Create object:

  • File(String pathname)
  • Create a new File instance by converting the given pathname string to an abstract pathname.

Common methods:

  • File, folder attribute length(): byte size of file
  • exists(): whether it exists or not, and returns true if it exists
  • isFile(): whether it is a file, return true if it is a file
  • isDirectory(): whether it is a folder, return true if it is a folder
  • getName(): get file / folder name
  • getParent(): get the path of the parent folder
  • getAbsolutePath(): get the full path of the file

Create, delete:

  • createNewFile(): create a new file. If the folder does not exist, an exception will occur. If the file already exists, false will be returned
  • mkdirs(): create a new multi-level folder that does not exist \ a\b\c
  • mkdir(): create a new folder without a single layer \ a
  • delete(): delete files, delete empty folders

Folder list:

  • list(): return String [], including file name
  • listFiles(): return File [], including File object

Byte stream read:

  • Byte stream is composed of bytes, character stream is composed of characters
  • In Java, characters are composed of two bytes. Byte stream is the most basic. All subclasses of InputStream and OutputStream are mainly used to process binary data.

Streaming transmission mainly refers to the whole audio and video and three-dimensional media and other multimedia files are analyzed into a compressed package by a specific compression method, and then the video server transmits them to the user's computer in order or in real time. In the system of streaming transmission, users don't need to wait until the whole file is downloaded as they do in the way of downloading. Instead, they can decompress the compressed A/V, 3D and other multimedia files on users' computers and play and watch them after decompressing them with decompressors after a few seconds or tens of seconds of startup delay. The rest of the multimedia file will continue to be downloaded in the background server.

Character stream read:

  • It is often used to process plain text data.
  • Reader abstract class
  • An abstract class for reading character streams.
   Common methods:
    --int read(): read a single character. 
    --int read(char[] cbuf): reads characters into an array. 
    --Abstract int read (char [] cbuf, int off, int len): read characters into a part of the array. 
    --int read(CharBuffer target): an attempt was made to read a character into the specified character buffer. 
    --Abstract void close(): closes the stream and releases all resources associated with it.

Reading of files

public class tt {
    public static void main(String[] args) throws Exception {
       method1();// Byte read
       method2();//Character reading
    }
     
    private static void method2() throws Exception {
       //Character stream reading picture scrambling
//     BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\teach\\1.jpg"))));

       BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\teach\\a\\1.txt"))));
//     System.out.println(in.readLine());
//     System.out.println(in.readLine());//null read to / n/r       
       String line = "";
       while((line = in.readLine())!=null) {//Read line by line
           System.out.println(line);
       }       
       in.close();
    }
 
    private static void method1() throws Exception {
       long s = System.currentTimeMillis();
       InputStream in = new FileInputStream("D:\\teach\\1.jpg");

       int b = 0;
       while ((b = in.read()) != -1) {
           // System.out.println(b);
       }

       s = System.currentTimeMillis() - s;
       System.out.println(s + "--");// 7515

       long ss = System.currentTimeMillis();
       InputStream in2 = new BufferedInputStream(new FileInputStream("D:\\teach\\1.jpg"));
       int b2 = 0;
       while ((b2 = in2.read()) != -1) {
           // System.out.println(b2);
       }

       ss = System.currentTimeMillis() - ss;
       System.out.println(ss + "==");// 32
       
       in.close();
       in2.close();
    }
}
Published 22 original articles, won praise 3, visitors 312
Private letter follow

Posted by archangel_617b on Tue, 10 Mar 2020 00:50:08 -0700