更新時(shí)間:2022-12-12 16:13:56 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽4339次
Java寫(xiě)入數(shù)據(jù)進(jìn)txt文件,需求:多條數(shù)據(jù)追加進(jìn)文件,且需要處理中文編碼問(wèn)題。
以下代碼只能處理向文件添加數(shù)據(jù)的功能,但是會(huì)覆蓋掉之前的數(shù)據(jù)
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Write {
public static void main(String[] args) throws IOException {
String word = "hahahahhahahahhah";
FileOutputStream fileOutputStream = null;
File file = new File("F:\test\test.txt");
if(!file.exists()){
file.createNewFile();
}
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(word.getBytes("gbk"));
fileOutputStream.flush();
fileOutputStream.close();
}
}
正確實(shí)例
public class Test{
public static void main(String[] args) throws IOException {
String path = "F:\test\test.txt";
String word = "試試";
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(path,true)));
out.write(word+"
");
out.close();
}
}
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"基礎(chǔ)入門學(xué)習(xí):Java把內(nèi)容寫(xiě)入txt文件",希望對(duì)大家有幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為您務(wù)。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743