溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java字符串寫入文件三種方式的實現(xiàn)

發(fā)布時間:2020-09-28 17:38:26 來源:腳本之家 閱讀:181 作者:孫_悟_空 欄目:編程語言

 Java字符串寫入文件三種方式的實現(xiàn)

1、使用FileWriter

String str="hello world!";
    FileWriter writer;
    try {
      writer = new FileWriter("E:/token.txt");
      writer.write(str);
      writer.flush();
      writer.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

2、使用FileOutPutStream

File txt=new File("E:/log1.txt");
       if(!txt.exists()){ 
         txt.createNewFile(); 
      } 
       byte bytes[]=new byte[512];  
       bytes=str.getBytes(); 
       int b=bytes.length;  //是字節(jié)的長度,不是字符串的長度
       FileOutputStream fos=new FileOutputStream(txt); 
       fos.write(bytes,0,b); 
       fos.write(bytes);
       fos.close();

3、使用FileOutPutStream追加寫入文件

FileOutputStream fos = new FileOutputStream("E:/log.txt",true);
//true表示在文件末尾追加 
fos.write(log.getBytes()); 
fos.close();

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI