JAVA创建XML格式的文件,并写入自定义的标签。所有的标签都放在了字符串中。
比如String a="......后面还有很多";将字符串a直接放到XMl的下面
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class WriteStringToTxt {
public void WriteStringToFile(String filePath, String s) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(s.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String filePath = "X:\\xxxx.xml";
String xml = "<xml><aaa>123</aaa></xml>";
new WriteStringToTxt().WriteStringToFile5(filePath, xml);
}
}
PrintWriter pw = new PrintWriter("xxx.xml", "utf-8");
pw.println("你所需要插入的文字.");
pw.close();