如何將字符串寫入文件?


package constxiong.interview;

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

/**
 * 測試寫入字符串到文件
 * @author ConstXiong
 * @date 2019-11-08 12:05:49
 */
public class TestWriteStringToFile {

    public static void main(String[] args) {
        String cx = "ConstXiong";
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("E:/a.txt");
            fos.write(cx.getBytes());//注意字符串編碼
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}

 

  

來一道刷了進BAT的面試題?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM