導航:首頁 > 編程語言 > 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相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:617
php5314 瀏覽:357
完美國際安裝到哪個文件夾 瀏覽:667
什麼app可以掃一掃做題 瀏覽:538
程序員編碼論壇 瀏覽:923
淘點是什麼app 瀏覽:658
中國高等植物pdf 瀏覽:453
51單片機時間 瀏覽:182
後台如何獲取伺服器ip 瀏覽:267
單片機流水燈程序c語言 瀏覽:234
程序員第二職業掙錢 瀏覽:238
運行里怎麼輸入伺服器路徑 瀏覽:840
pythonstepwise 瀏覽:509
劉一男詞彙速記指南pdf 瀏覽:64
php認證級別 瀏覽:368
方舟編譯啥時候推送 瀏覽:1011
php手機驗證碼生成 瀏覽:675
哲學思維pdf 瀏覽:14
凌達壓縮機有限公司招聘 瀏覽:534
weblogic命令部署 瀏覽:36