package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.util.ArrayList; import java.util.List; public class Operate_Txt { public static void writeFile(String fileName, String fileContent) { try { File file = new File(fileName); if (!file.exists()) { file.createNewFile(); } OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(file,true),"utf-8"); BufferedWriter writer=new BufferedWriter(write); writer.write(fileContent); writer.close(); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("null") public static String[] read_txt(String fileName){ List<String> list = new ArrayList<String>(); File file = new File(fileName); try{ InputStreamReader read = new InputStreamReader(new FileInputStream(file),"utf-8"); BufferedReader br = new BufferedReader(read);//构造一个BufferedReader类来读取文件 String line = null; while((line = br.readLine())!=null){//使用readLine方法,一次读一行 list.add("#####换行#####"+line.toString()); } br.close(); }catch(Exception e){ e.printStackTrace(); } String[] result = list.toString().replace("[", "").replace("]", "").split("#####换行#####"); return result; } }
package createhtml; public class CreateHtml { public String add_str(String content,String addstr){ content = content + " " + addstr; return content; } public void createhtml(String readfile){ String content = ""; Operate_Txt operate_txt = new Operate_Txt(); String data[] = operate_txt.read_txt("D:\"+readfile+".txt"); System.out.println(); String writefile = "D:\"+readfile+".html"; content = add_str(content,"<html>"); content = add_str(content,"<body>"); content = add_str(content,"<table border='1'>"); for(int i=0;i<data.length;i++){ if(i==0){ String[] line_data = data[i].split(","); content = add_str(content,"<tr>"); for(int j=0;j<line_data.length;j++){ content = add_str(content,"<th>"+line_data[j]+"</th>"); } content = add_str(content,"</tr>"); }else{ String[] line_data = data[i].split(","); content = add_str(content,"<tr>"); for(int j=0;j<line_data.length;j++){ content = add_str(content,"<td>"+line_data[j]+"</td>"); } content = add_str(content,"</tr>"); } } content = add_str(content,"</table>"); content = add_str(content,"</body>"); content = add_str(content,"</html>"); operate_txt.writeFile(writefile, content); } }