簡單的txt轉換xml
package com.liu;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
public class TxtToXml {
private String strTxtFileName;
private String strXmlFileName;
public TxtToXml() {
strTxtFileName = new String();
strXmlFileName = new String();
}
public void createXml(String strTxt, String strXml) {
strTxtFileName = strTxt;
strXmlFileName = strXml;
String strTmp;
try {
BufferedReader inTxt = new BufferedReader(new FileReader( strTxtFileName)); BufferedWriter outXml = new BufferedWriter(new FileWriter(
strXmlFileName));
outXml.write("");
outXml.newLine();
outXml.write("
while ((strTmp = inTxt.readLine()) != null) {
StringTokenizer strToken = new StringTokenizer(strTmp, ",");
String arrTmp[];
arrTmp = new String[3];
for (int i = 0; i < 3; i++)
arrTmp[i] = new String("");
int index = 0;
outXml.newLine();
outXml.write("
while (strToken.hasMoreElements()) {
strTmp = (String) strToken.nextElement();
strTmp = strTmp.trim();
arrTmp[index++] = strTmp;
}
outXml.newLine();
outXml.write("
outXml.newLine();
outXml.write("
outXml.newLine();
outXml.write("
outXml.newLine();
outXml.write(" ");
}
outXml.newLine();
outXml.write("");
outXml.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String txtName = "testtxt.txt";
String xmlName = "testxml.xml";
TxtToXml thisClass = new TxtToXml();
thisClass.createXml(txtName, xmlName);
}
}
相關推薦:計算機等級考試二級Java編程的三十個基本規(guī)則北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內蒙古 |