java怎麼寫文件
① java中如何將生成的數據寫入到文件中
packagecom.pig.database.file.txt;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.PrintStream;
/**
*@authorzhuruhong
*
*若要變更這個產生的類別註解的範本,請移至
*視窗>喜好設定>Java>程式碼產生>程式碼和註解
*/
publicclassWriteTxtFileByName{
privateStringfilename=null;
publicWriteTxtFileByName(Stringfilename){
this.filename=filename;
}
publicvoidwriteFileByName(Stringcontent){
FiledocFile=newFile(filename);
try{
docFile.createNewFile();
FileOutputStreamtxtfile=newFileOutputStream(docFile);
PrintStreamp=newPrintStream(txtfile);
p.println(content);
txtfile.close();
p.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
publicstaticvoidmain(String[]args){
WriteTxtFileByNamewfbn=newWriteTxtFileByName("title");
wfbn.writeFileByName("content");
}
}
給你一個實例。
② JAVA如何寫XML文件
import java.io.*;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DOM4JTest {
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl href='students.xsl'");
Element root = doc.addElement("students");
Element eltStu1 = root.addElement("student").addAttribute("sn", "01");
Element eltName1 = eltStu1.addElement("name");
Element eltAge1 = eltStu1.addElement("age");
eltName1.setText("張三");
eltAge1.setText("20");
Element eltStu2 = root.addElement("student").addAttribute("sn", "02");
Element eltName2 = eltStu2.addElement("name");
Element eltAge2 = eltStu2.addElement("age");
eltName2.setText("李四");
eltAge2.setText("18");
try {
OutputFormat format = new OutputFormat(" ", true);
format.setEncoding("gb2312");
// 可以把System.out改為需要的流。
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);
xmlWriter.write(doc);
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
③ java如何創建文件
public void createFile(){x0dx0ax0dx0a//path表示你所創建文件的路徑x0dx0aString path = "d:/tr/rt";x0dx0aFile f = new File(path);x0dx0aif(!f.exists()){x0dx0a f.mkdirs();x0dx0a} x0dx0a// fileName表示你創建的文件名;為txt類型;x0dx0aString fileName="test.txt";x0dx0aFile file = new File(f,fileName);x0dx0aif(!file.exists()){x0dx0atry {x0dx0afile.createNewFile();x0dx0a} catch (IOException e) {x0dx0a// TODO Auto-generated catch blockx0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0a}x0dx0a//現在你可以在d:/tr/rt 目錄下找到test.txt文件
④ java代碼 如何向TXT文件寫入內容
向txt文件寫入內容基本思路就是獲得一個file對象,新建一個txt文件,打開I/O操作流,使用寫入方法進行讀寫內容,示例如下:
packagecommon;
importjava.io.*;
importjava.util.ArrayList;
publicclassIOTest{
publicstaticvoidmain(Stringargs[]){
ReadDate();
WriteDate();
}
/**
*讀取數據
*/
publicstaticvoidReadDate(){
Stringurl=「e:/2.txt」;
try{
FileReaderread=newFileReader(newFile(url));
StringBuffersb=newStringBuffer();
charch[]=newchar[1024];
intd=read.read(ch);
while(d!=-1){
Stringstr=newString(ch,0,d);
sb.append(str);
d=read.read(ch);
}
System.out.print(sb.toString());
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
/**
*寫入數據
*/
publicstaticvoidWriteDate(){
try{
Filefile=newFile(「D:/abc.txt」);
if(file.exists()){
file.delete();
}
file.createNewFile();
BufferedWriteroutput=newBufferedWriter(newFileWriter(file));
ArrayListResolveList=newArrayList();
for(inti=0;i<10;i++){
ResolveList.add(Math.random()*100);
}
for(inti=0;i
output.write(String.valueOf(ResolveList.get(i))+「 」);
}
output.close();
}catch(Exceptionex){
System.out.println(ex);
}
}
}
原文出自【比特網】,轉載請保留原文鏈接:http://soft.chinabyte.com/database/303/12439303.shtml