JAVA 写文件操作

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import junit.framework.TestCase;

/*import org.apache.oro.text.regex.MatchResult;
 import org.apache.oro.text.regex.PatternCompiler;
 import org.apache.oro.text.regex.PatternMatcher;
 import org.apache.oro.text.regex.PatternMatcherInput;
 import org.apache.oro.text.regex.Perl5Compiler;
 import org.apache.oro.text.regex.Perl5Matcher;*/

public class TestCenter
{

 static File sourceFile = new File("E://JavaProjects//WZCRM_V10//WebRoot");
 static int i = 0;
 static BufferedWriter writer = null;
 
 TestCenter(String fileName)
 {
  try
  {
   File file = new File("D://crm//" + fileName);
   if (!file.exists()) file.createNewFile();
   writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charset.forName("utf-8")));
  }
  catch (IOException e)
  {
   e.printStackTrace();
  }
 }

 public static void main(String[] args)
 {
  try
  {
   File[] arr = sourceFile.listFiles();
   Document document = DocumentHelper.createDocument();
   Element _root = document.addElement("languages");
   
   for (int i = 0; i < arr.length; i++)
   {
    //arr[i] = new File("E://JavaProjects//WZCRM_V10//WebRoot//sales//quotation_audit.jsp");
    String fileName = arr[i].getName();
    if(null==arr[i] || !arr[i].isFile()) continue;
    Element jsp = _root.addElement("jsp");
    jsp.addAttribute("ID", fileName);
    jsp.addAttribute("name", "");
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(arr[i]), Charset.forName("utf-8")));
    String str = null;
    String res = null;
    //TestCenter t = new TestCenter(fileName);
    while (null != (str = reader.readLine()))
    {
     parse(str, fileName,jsp);
     //System.out.println(res);
     //t.export(writer, res);
    }
    //writer.flush();
    //writer.close();
   }
   write(document);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
 }

 private static void parse(String line, String fileName,Element jsp) throws Exception
 {
  // String
  // patternStrs="(<//s*a//s+(?:[^//s>]//s*){0,})href//s*=//s*(/"|'|)([^//2//s>]*)//2((?://s*[^//s>]){0,}//s*>)";
  // String patternStrs = "<[^<|^>|^%^!]*>";
  String idName = fileName.substring(0, fileName.indexOf("."));
  String patternStrs = "[^/"]*<[^<|^>|^%|^/]+(>(//s*[[//w][^<|^>|^%|^;|^/"]]+//s*)<)/";
  String pattern = "id=([/"|']{1}(//w+)[/"|']{1})";
  String cnStr = "[/u4e00-/u9fa5]+";
  Pattern idp = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
  Pattern htmp = Pattern.compile(patternStrs);
  Pattern chp = Pattern.compile(cnStr);
  Matcher m1 = htmp.matcher(line);
  Matcher m2 = null;
  Matcher m3 = null;
  while (m1.find())
  {
   String group = m1.group();
   if (group.replaceAll("//s", "").equals("></")) break;
   m2 = idp.matcher(group);
   if (m2.find())
   {
    Element lang = jsp.addElement("lang");
    String temp = m1.group(2);
    m3 = chp.matcher(temp);
    lang.addAttribute("ID", m2.group(2));
    if(m3.matches()){
     lang.addAttribute("en", "");
     lang.addAttribute("gb", m1.group(2));
    }else{
     lang.addAttribute("en", m1.group(2));
     lang.addAttribute("gb", "");
    }
    
    //line = line.replaceFirst(m1.group(1), " id=/"" + idName + (i++) + "/" " + m1.group(1));
    
   }
  }
  //return line;
 }

 private void export(BufferedWriter w, String line)
 {
  try
  {
   w.write(line);
   w.write("/r/n");
  }
  catch (IOException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void write(Document doc) throws FileNotFoundException, IOException {
  if (doc == null) { return; }
  OutputFormat format = OutputFormat.createPrettyPrint();
  format.setEncoding("UTF-8");
  format.setSuppressDeclaration(true);
  format.setIndent(true);
  format.setIndent("");
  format.setNewlines(true);
  XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File("D://crm//wz.xml")),format);
  doc.setXMLEncoding("UTF-8");
        xmlWriter.write(doc);
        xmlWriter.flush();
        xmlWriter.close();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值