‘壹’ 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();
}
}
}