如何用java生成一个XML文件,并且将该文件压
1个回答
展开全部
package com.test.io;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CreateXmlFile {
private static final String XML_TITLE = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
private static final String FILE_PATH = "d:";
private static final String FILE_SPE = "/";
private static final String CHANGE_LINES = "\n";
private static final String FILE_NAME = "text.xml";
public static void main(String[] args) {
String xml = appendStr();
try {
File file = createXmlFile(FILE_PATH + FILE_SPE + FILE_NAME);
writeDoc(file, xml);
System.out.println("文件已经创建:");
System.out.println("目录:" + FILE_PATH + FILE_SPE + FILE_NAME);
System.out.println("文件内容为:");
System.out.println(xml);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 组装XML内容
* @return
*/
private static String appendStr(){
StringBuffer sb = new StringBuffer();
sb.append(XML_TITLE).append(CHANGE_LINES);
sb.append("<book>").append(CHANGE_LINES);
sb.append("<name>").append(CHANGE_LINES);
sb.append("《好玩》").append(CHANGE_LINES);
sb.append("</name>").append(CHANGE_LINES);
sb.append("</book>").append(CHANGE_LINES);
return sb.toString();
}
/**
* 将内容写入文件
* @param file
* @param str
*/
private static void writeDoc(File file,String str){
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(str.getBytes());
}catch(Exception ex){
//
ex.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 创建文件
* @param path
* @return
* @throws IOException
*/
private static File createXmlFile(String path) throws IOException{
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
return file;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class CreateXmlFile {
private static final String XML_TITLE = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
private static final String FILE_PATH = "d:";
private static final String FILE_SPE = "/";
private static final String CHANGE_LINES = "\n";
private static final String FILE_NAME = "text.xml";
public static void main(String[] args) {
String xml = appendStr();
try {
File file = createXmlFile(FILE_PATH + FILE_SPE + FILE_NAME);
writeDoc(file, xml);
System.out.println("文件已经创建:");
System.out.println("目录:" + FILE_PATH + FILE_SPE + FILE_NAME);
System.out.println("文件内容为:");
System.out.println(xml);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 组装XML内容
* @return
*/
private static String appendStr(){
StringBuffer sb = new StringBuffer();
sb.append(XML_TITLE).append(CHANGE_LINES);
sb.append("<book>").append(CHANGE_LINES);
sb.append("<name>").append(CHANGE_LINES);
sb.append("《好玩》").append(CHANGE_LINES);
sb.append("</name>").append(CHANGE_LINES);
sb.append("</book>").append(CHANGE_LINES);
return sb.toString();
}
/**
* 将内容写入文件
* @param file
* @param str
*/
private static void writeDoc(File file,String str){
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(str.getBytes());
}catch(Exception ex){
//
ex.printStackTrace();
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 创建文件
* @param path
* @return
* @throws IOException
*/
private static File createXmlFile(String path) throws IOException{
File file = new File(path);
if(!file.exists()){
file.createNewFile();
}
return file;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询