JAVA file is written to txt

import java.io.FileOutputStream;
import java.io.IOException;

public class TestFileOut {
   
    public static void main(String[] args) {
        //The text document will be created automatically. If it already exists, the old content will be overwritten.
        //The try code block will automatically call the close() method
        try(FileOutputStream out = new FileOutputStream("D:\\d.txt")) {
           String str = "Study hard, go up every day! zhouning123";
           //Convert the string to a byte array b
           byte[] b = str.getBytes();
           //Single byte write
                /* for(int i =0; i < b.length; i++) {
                         out.write(b[i]);
                    }
                */
           //Byte array write, more efficient
           out .write(b);
           //out.write(b, 0, b.length);
        } catch (IOException ex) {
             System.out.println(ex.getMessage());
            }
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324931627&siteId=291194637