• 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);
            
        }
        
    }
  • 相关阅读:
    崩漏
    李翰卿治疗小儿五更咳嗽经验
    34名国宝级名老中医秘方集锦(一)
    黄煌儿科经方
    中医临床参考
    HTML 动态云启动画面
    CANVAS 水波动态背景
    PHP MYSQL单向同步方案
    PHP 获取指定目录下所有文件(包含子目录)
    JS 深度拷贝 Object Array
  • 原文地址:https://www.cnblogs.com/yanzhe/p/7819553.html
Copyright © 2020-2023  润新知