java 读取本地文件 更改

部分参考

https://blog.csdn.net/Bancroft_boy/article/details/81126478

package IO;
 
import java.io.*;
 
public class test {
 
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("D://1.txt");
        InputStreamReader isr=new InputStreamReader(fis,"GB2312");
        BufferedReader br = new BufferedReader(isr);
        
        FileOutputStream fos =new FileOutputStream("E://2.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos,"GB2312");
        BufferedWriter bw =new BufferedWriter(osw);
        
        
        int i=0;
        while((i=br.read())!=-1){
            bw.write(i);
        }
        bw.close();
        br.close();
        
 
    }
 
}

 

来说我自己做的

首先是文件处理方法

我选择的是字符流  不然的话会出现乱码情况

 
           /**
            * @Title: MakeHtml 
            * @Description: 创建html
            * @param    filePath 设定模板文件
            * @param    conent 替换的内容
            * @param    disrPath  生成html的存放路径
            * @param    fileName  生成html名字 
            * @return void    返回类型 
            * @throws
            */
           public static void MakeHtml(String filePath,String conent,String disrPath,String fileName ){
               InputStreamReader bis = null;
               try {
                   System.out.print(filePath);
                   String templateContent = "";
                   FileInputStream fileinputstream = new FileInputStream(filePath);// 读取模板文件  
                   StringBuffer sb = new StringBuffer();
                   bis = new InputStreamReader(fileinputstream,"UTF-8");
                   BufferedReader br = new BufferedReader(bis);
                   int i=0;
                       while((i=br.read())!=-1){
                           sb.append((char)i);
                       }
                   templateContent = sb.toString();
                   templateContent = templateContent.replaceAll("模板内容", conent);
                   String fileame = fileName + ".html";
                   fileame = disrPath+"/" + fileame;// 生成的html文件保存路径。
                   FileOutputStream fileoutputstream = new FileOutputStream(fileame);// 建立文件输出流
                   System.out.print("文件输出路径:");
                   System.out.print(fileame);
                   /*byte tag_bytes[] = templateContent.getBytes();
                   fileoutputstream.write(tag_bytes);
                   fileoutputstream.close();*/
                   OutputStreamWriter osw = new OutputStreamWriter(fileoutputstream,"UTF-8");
                      BufferedWriter bw =new BufferedWriter(osw);
                      /*int j=0;
                      while((j=br.read())!=-1){
                          bw.write(j);
                      }*/
                      bw.write(templateContent);
                      bw.close();
               } catch (Exception e) {
                   System.out.print(e.toString());
                   e.printStackTrace();
               }finally{
                   try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
               }
           }

 

然后调用

        FileUtil.MakeHtml("E:\\testExport\\template.html", "111111", "E:\\testExport\\export", "aaa");

 

posted @ 2020-08-05 17:43  不懂技术的雷  阅读(721)  评论(0编辑  收藏  举报