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