InputStream IO stream of Detailed

1 IO

  • BigDecimal/BigInteger

Overview:

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

Create an object:

  • BigDecimal.valueOf(2);
  • Common methods: add (BigDecimal bd): do adder
  • substract (BigDecimal bd): do subtraction
  • multiply (BigDecimal bd): do multiplication
  • divide (BigDecimal bd): to do the divisions
  • divide (BigDecimal bd, reservations digits, rounding): In addition to the use of different
  • setScale (reservation digits, rounding): Same as above
  • pow (int n): data of several power demand
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);//不精确              
              System.out.println(===上面的除法不精确===);
              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);//报错除不尽
              //保留位数和舍入方式
              bd3=bd1.divide(bd2,5,BigDecimal.ROUND_HALF_UP);
              bd3=bd3.setScale(2, BigDecimal.ROUND_HALF_UP);//保留两位
              System.out.println(bd3.doubleValue());              
       }
}
  • IO Introduction

Inheritance structure:

  • in / out with respect to the program in terms of the input (read) and process output (written) is.
  • In Java, depending on the data processing units, into a byte stream and character stream
    File

Byte stream: for binary files

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

Character stream: for text files. Write prone to distortion phenomena, the best set of read and write to specify the encoding is utf-8

>  Writer
>   	-- BufferedWriter
>    	-- OutputStreamWriter Reader
>     	-- BufferedReader
>      	-- InputStreamReader
>       	-- PrintWriter/PrintStream
  • Concept of flow
  • Read and write data into abstract data flow in the pipeline.
  • Stream can flow only in one direction
  • Used to read in the input stream
  • Out written to the output stream
  • Data can only be read from beginning to end of the primary sequence

  • File file stream

Overview:

  • A disk package path string, one operation may be performed on this path. Can be used to encapsulate the file path, the folder path, the path does not exist.

Create an object:

  • File(String pathname)
  • To create a new File instance by converting the given pathname string into an abstract pathname.

Common methods:

  • File, folder attributes length (): the amount of bytes of the file
  • exists (): if there is, returns true presence
  • isFile (): Whether the file, the file returns true
  • isDirectory (): whether the folder is a file folder returns true
  • getName (): Get File / Folder name
  • getParent (): Gets the path to the parent folder
  • Get the full path of the file: getAbsolutePath ()

Create, Delete:

  • createNewFile (): create a new file, folder does not exist abnormally, the file already exists, false
  • mkdirs (): New multilayer folder does not exist \ a \ b \ c
  • mkdir (): New single-layer non-existent folder \ a
  • delete (): Delete files, delete empty folders

Folder List:

  • list (): Returns the String [], including the file name
  • listFiles (): returns the File [], contains the file object

Read byte stream:

  • Is a byte stream of bytes, character by character in the stream
  • Java in character consists of two bytes. Byte stream is the most basic, all InputStream and OutputStream subclasses are used primarily for working with binary data.

Streaming refers primarily to the entire three-dimensional media audio and video and other multimedia files parsed into a specific compressed packet after compression, real-time transmission from the video server to a user computer or sequentially. In the system using streaming mode, the user does not have to wait so as to download the entire file using all the download is complete, but only after a few seconds or tens of seconds of delay starting to use the device in a decompression of the user's computer compressed a / V, 3D and other multimedia files for playback and viewing after decompression. At this point the remaining part of the multimedia files will continue to download in the background server.

Character stream reads:

  • Commonly used in the plain text data.
  • Reader Abstract class
  • Abstract class for reading character streams.
   常用方法:
    -- int read() :读取单个字符。 
    -- int read(char[] cbuf) :将字符读入数组。 
    -- abstract  int read(char[] cbuf, int off, int len) :将字符读入数组的某一部分。 
    -- int read(CharBuffer target) :试图将字符读入指定的字符缓冲区。 
    -- abstract  void close() :关闭该流并释放与之关联的所有资源。

Read the file

public class tt {
    public static void main(String[] args) throws Exception {
       method1();// 字节读取
       method2();//字符读取
    }
     
    private static void method2() throws Exception {
       //字符流读图片乱码
//     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读到/n/r       
       String line = "";
       while((line = in.readLine())!=null) {//一行一行读
           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 36 original articles · won praise 13 · views 1066

Guess you like

Origin blog.csdn.net/weixin_44598691/article/details/104774395