io java-based cache read and write bytes bytes

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 new byte [1024]; // TODO ONETIME Read bytes length !!! 
         int num = 0; 
         // TODO -1 indicates the file pointer to the end of the file, the file is returned on behalf of other num current pointer position, OFF offset indicates the byte array storage 
         // read () * @ returns the total number of bytes read into the buffer, or 
        @ -1 indicates if the end since there is no more data, the stream has been reached. 
         StringBuilder builder = new StringBuilder (); // String str = ""; may be 
         the while (! (NUM = in.read (bytes, 0, bytes.length)) = -. 1) { 
              // new new String STR + = ( bytes, 0, num); can 
              builder.append (new string (bytes, 0 , num)); // number of bytes is converted to a string, line breaks, etc. reserved 

        } 
         System.out.println (Builder); // todo with newline 
         return builder.toString (); 
    } 

    @Deprecated 
    static void writeMethod (path String, String Nullable @ 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不存在自动创建;
        OUT = new new a FileOutputStream the OutputStream (dest); 

        // TODO disposable take many bytes 
        byte [] bytes = new new byte [1024]; 
        // read the file (byte stream cache)
        //todo 文件指针-1表示最后一行
        int num ;
        the while ((NUM = in.read (bytes, 0, bytes.length))! = -1) { 
            // TODO byte is read every time a conversion String 
            String STR = new new String (bytes, 0, NUM) ; // this conversion may be achieved byte string, more practical 
            System.out.println (STR); 
            // number of bytes per write todo 
            out.write (bytes, 0, NUM); 
        } 
        // the CLOSE the STREAM 
        in.close (); 
        the out.close (); 
    } 

    static bufferRead String (String the src) throws a FileNotFoundException, IOException { 
        byte [ ] bytes = new new byte [2048]; 
        // read content receiving (n represents the data, but is a number)

        In = new new BufferedInputStream BufferedInputStream (new new the FileInputStream (the src)); 
        // number of bytes taken todo disposable 
        int = n--1; 
        String STR = ""; 
        the while ((n-in.read = (bytes, 0, bytes.length !)) = -1) { 
            // into a string 
            STR + = new new string (bytes, 0, n-); // charset UTF-Defaults. 8 
            //System.out.println(str); 
        } 
        in.close ( ); 
        return str.toString (); 
    } 

    static void bufferWrite (the src String, String dest) throws IOException { 
        // read the file (byte stream cache) 
        BufferedInputStream in BufferedInputStream new new = (new new the FileInputStream (the src)); 
        // write into the corresponding file 
        BufferedOutputStream The new new BufferedOutputStream The OUT = (new new a FileOutputStream (dest)); 
        // number of bytes taken disposable 
        byte [] bytes = new byte [2048]; 
        // read content receiving (n represents the data, is simply a digital form) 
        int = n--1; 
        // TODO extracted data cycle, length of bytes.length 
        the while (! (n-in.read = (bytes, 0, bytes.length)) = -1) { 
            // into a string 
            string str = new string (bytes, 0, n, StandardCharsets.UTF_8); 
            System.out.println (STR); 
            // write documents 
            out.write (bytes, 0, n-); 
        } 
        // clear the cache 
        out.flush (); 
        // Close stream 
        in.close (); 
        the out.close (); 

    } 


}

  

Guess you like

Origin www.cnblogs.com/SunshineKimi/p/12594008.html