java 基礎io讀寫字節以及緩存讀寫字節

package io;

import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.Nullable;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class iodemo {


    public static void main(String[] args)throws IOException {
        String src = "D:\\workspace\\javahmac\\leetcode\\io\\test1.txt";
        String dest= "D:\\workspace\\javahmac\\leetcode\\io\\test2.txt";
//        readThenWrite(src,dest);
          bufferWrite(src,dest);

    }
    static  String readMethod(@NotNull String path) throws IOException {
         /** Return string */
         InputStream in = new FileInputStream(path);
         byte[] bytes = new byte[1024];  //todo onetime read bytes length !!!
         int num=0;
         // todo -1 表示文件指針到文件末尾,其餘則返回num表明文件指針當前的位置,off表示byte 數組存放的偏移量
         //   read()  * @返回讀入緩衝區的字節總數,或者
        //      -1表示若是因爲末尾沒有更多數據,已到達流。
         StringBuilder builder= new StringBuilder(); // String str="";也能夠
         while ((num=in.read(bytes,0,bytes.length))!=-1){
              // str += new String(bytes, 0, num);也能夠
              builder.append(new String(bytes, 0, num)); // 一次轉換多少字節爲字符串,保留換行符等等

        }
         System.out.println(builder);  // todo 帶換行符號
         return builder.toString();
    }

    @Deprecated
    static void writeMethod(String path,@Nullable String mode,String text) throws IOException {
        /** write bytes[] to file   */
        byte[] content = text.getBytes();
        if(mode.equals("w")){
            OutputStream out =new FileOutputStream(path,false); // w
            out.write(content);
        }
        if(mode.equals("a")){
            OutputStream out =new FileOutputStream(path,true); // true 追加寫 a+
            out.write(content);
        }

    }
    /** onetime r onetime w */
    static void readThenWrite(String src,String dest) throws IOException {
        InputStream in = new FileInputStream(src); //todo append能夠追加,若是dest不存在自動建立;
        OutputStream out = new FileOutputStream(dest);

        // todo 一次性取多少字節
        byte[] bytes = new byte[1024];
        //todo 文件指針-1表示最後一行
        int num ;
        while ((num = in.read(bytes,0,bytes.length)) != -1) {
            //todo 一次轉換每次讀取的字節爲string
            String str = new String(bytes,0,num); //這裏能夠實現字節到字符串的轉換,比較實用
            System.out.println(str);
            //todo 每次寫入多少字節
            out.write(bytes, 0, num);
        }
        //CLOSE STREAM
        in.close();
        out.close();
    }

    static String bufferRead(String src) throws FileNotFoundException,IOException {

        //讀取文件(緩存字節流)
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
        //todo 一次性取多少字節
        byte[] bytes = new byte[2048];
        //接受讀取的內容(n就表明的相關數據,只不過是數字的形式)
        int n = -1;
        String str="";
        while ((n = in.read(bytes,0,bytes.length)) != -1) {
            //轉換成字符串
            str += new String(bytes,0,n);//charset utf-8 defaults
            //System.out.println(str);
        }
        in.close();
        return str.toString();
    }

    static  void  bufferWrite(String src,String dest) throws IOException {
        //讀取文件(緩存字節流)
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
        //寫入相應的文件
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
        //一次性取多少字節
        byte[] bytes = new byte[2048];
        //接受讀取的內容(n就表明的相關數據,只不過是數字的形式)
        int n = -1;
        //todo 循環取出數據,長度爲bytes.length
        while ((n = in.read(bytes,0,bytes.length)) != -1) {
            //轉換成字符串
            String str = new String(bytes,0,n, StandardCharsets.UTF_8);
            System.out.println(str);
            //寫入相關文件
            out.write(bytes, 0, n);
        }
        //清除緩存
        out.flush();
        //關閉流
        in.close();
        out.close();

    }


}
相關文章
相關標籤/搜索