• java-读写文件


    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);
            
        }
        
    }
  • 相关阅读:
    linux 、windows、mac、dns刷新
    Nginx日志切割及其各种服务日志随便切
    https是如何防劫持的
    梁启超-少年中国说
    mongodb-4.2-隋唐笔迹
    connection closed by foreign host
    磁盘类型查看
    魅力男神之解说
    jenkins 更新脚本之expect交互
    nginx 之特殊端口转目录访问
  • 原文地址:https://www.cnblogs.com/yanzhe/p/7819553.html
Copyright © 2020-2023  润新知