導航:首頁 > 編程語言 > java保存txt

java保存txt

發布時間:2023-05-16 19:41:16

『壹』 java 如何把數據保存到TXT文件,

Java通過使用I/O文件操作類,來創建輸入輸出流,將數據保存在file tet文件裡面。示例如下:

package*&####&*_1_*&####&*;

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;

publicclassWriteFileExample{
publicstaticvoidmain(String[]args){

FileOutputStreamfop=null;
Filefile;
Stringcontent="Thisisthetextcontent";

try{

file=newFile("c:/newfile.txt");
fop=newFileOutputStream(file);

//iffiledoesntexists,thencreateit
if(!file.exists()){
file.createNewFile();
}

//getthecontentinbytes
byte[]contentInBytes=content.getBytes();

fop.write(contentInBytes);
fop.flush();
fop.close();

System.out.println("Done");

}catch(IOExceptione){
e.printStackTrace();
}finally{
try{
if(fop!=null){
fop.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}

『貳』 java如何實現文本保存

try{ FileOutputStream fos=new FileOutputStream("test.txt",true);//true表明會追加內容 PrintWriter pw=new PrintWriter(fos); pw.write(你想寫入的內容); pw.flush(); }catch(FileNotFoundException e){ e.printStackTrace(); }finally{ try{ pw.close(); }catch(Exception e){ e.printStackTrace(); } }

『叄』 java中怎樣將輸入的文件保存為txt文檔

是吧這個代碼輸入到文本文件中對么?給你提個醒IO流的東西,很簡單

『肆』 如何把一個java數據保存到txt裡面

/**
*Createdbyjackon2017/1/16.
*/
publicclassFileDemo{
publicstaticvoidmain(String[]args){
try{
//如果文件存在,則追加內容;如果文件不存在,則創建文件
Filef=newFile("test.txt");
FileWriterfw=newFileWriter(f,true);

PrintWriterpw=newPrintWriter(fw);
//java數據,可以轉成json字元串存儲
pw.println("{"key":"value"}");

pw.flush();
pw.close();
fw.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}

閱讀全文

與java保存txt相關的資料

熱點內容
jquery源碼講解 瀏覽:280
寶馬app如何發帖 瀏覽:861
重慶伺服器託管商雲空間 瀏覽:439
浦發銀行app如何調流水 瀏覽:677
玉石鑒賞pdf 瀏覽:842
為什麼小度APP一直連不上網路 瀏覽:163
pdf模板java 瀏覽:40
現代瑞納的壓縮比 瀏覽:128
網吧里的ftp伺服器有什麼用 瀏覽:872
程序員年終總結工作體會 瀏覽:153
pdf可以直接列印 瀏覽:661
android刷wp8 瀏覽:912
歷史地圖集pdf 瀏覽:926
快手app極速版怎麼掃碼 瀏覽:805
qq程序員玩法 瀏覽:96
1是什麼門電路app 瀏覽:867
博之輪運動手錶用什麼app 瀏覽:646
asp視頻聊天源碼 瀏覽:85
網路游戲編程pdf 瀏覽:534
360壓縮出錯 瀏覽:848