zoukankan      html  css  js  c++  java
  • 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);
            
        }
        
    }
  • 相关阅读:
    弹出层layer的使用
    SQL Server SQL分页查询
    C#过滤html标签
    SQLServer ForXmlPath应用
    js调用soapWebService服务
    MediaWiki使用指南
    阿里云金融云服务器配置
    VS无法启动 IISExpress web 服务器
    mysql服务突然丢失解决方案
    [k8s]通过openssl生成证书
  • 原文地址:https://www.cnblogs.com/yanzhe/p/7819553.html
Copyright © 2011-2022 走看看