您好,登錄后才能下訂單哦!
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();
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責(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)容。