Java IO output operation

import java.io.*;

public class OutPutStreamDemo {
    
    
    public static void main(String[] args) throws Exception {
    
    
        File file=new File("F:"+File.separator+"AAAa"+File.separator+"AaAAAA.txt");
        if(!file.getParentFile().exists()){
    
    //观察文件夹是否存在,不存在则创建文件夹
            file.getParentFile().mkdirs();
        }
       try {
    
    
           for(int i=0;i<1;i++){
    
    
               OutputStream outputStream=new FileOutputStream(file,true);//文件处理关系,向上转型,两类构造方法,一个是追加,一个是覆盖。
               String str="www.abc.com\r\n";
               outputStream.write(str.getBytes(),5,5);//返回的是byte数组,将子字符串转变为数组
           }

       }catch (IOException e){
    
    
           e.printStackTrace();
       }

    }
}

1. Find the file that needs to be manipulated through the File class.
2. Through subclass instantiation, FileOutputstream
will be transformed upward. 3. Convert the string to a byte array, output
4. Output and close the resource
. Create a file automatically without using creatfile

Guess you like

Origin blog.csdn.net/lyl140935/article/details/108564605