Chinaunix首页 | 论坛 | 博客
  • 博客访问: 226860
  • 博文数量: 45
  • 博客积分: 3010
  • 博客等级: 中校
  • 技术积分: 915
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-18 16:03
文章分类

全部博文(45)

文章存档

2011年(1)

2008年(44)

我的朋友

分类: Java

2008-07-30 09:46:23

package com.demoxml;

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(" " + arrTmp[0] + "");
    outXml.newLine();
    outXml.write(" " + arrTmp[1] + "");
    outXml.newLine();
    outXml.write(" " + arrTmp[2] + "");
    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"; //txt测试文件
  String xmlName = "testxml.xml"; //转换后成功生成的xml文件
  TxtToXml thisClass = new TxtToXml();
  thisClass.createXml(txtName, xmlName);
 }
}


阅读(2813) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~