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

熱點內容
android仿ios時間選擇器 瀏覽:378
見識pdf 瀏覽:81
男孩子慰菊手冊pdf 瀏覽:531
注冊表啟動項命令 瀏覽:109
89c51單片機定時器 瀏覽:687
一般不適合做程序員的適合做啥 瀏覽:923
點在多邊形內演算法 瀏覽:494
程序員下班急忙回家 瀏覽:359
安慶php全套源碼交友類型網站源碼 瀏覽:92
浪潮伺服器公司地址 瀏覽:733
密約聊天交友app怎麼賺錢 瀏覽:280
滴滴java 瀏覽:109
phpexpires 瀏覽:218
matlab編程用什麼語言 瀏覽:70
php查找字元是否存在 瀏覽:701
可編程函數發生器 瀏覽:881
單片機正極接地視頻 瀏覽:67
利用python爬火車票 瀏覽:375
androidaidl如何工作 瀏覽:855
第三胸椎壓縮骨折什麼症狀 瀏覽:506