JAVA读取文件,并替换固定字符串。java读取并修改文件

package com.tuzi.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestFile {
    public TestFile() {
    }
    public static String read(String path) {
        StringBuffer res = new StringBuffer();
          String line = null;
          try {
           BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(path), "UTF-8"));
           while ((line = reader.readLine()) != null) {
            res.append(line + "\n");
           }
           reader.close();
          } catch (FileNotFoundException e) {
           e.printStackTrace();
          } catch (IOException e) {
           e.printStackTrace();
          }
          return res.toString();
     }
    
//     sum += 1;
//       if(sum == start){
//           res.append(line + "\n");
//           if(y < size){
//               
//           }
//       }
    public static String readFor(String path,int num,int size) {
        StringBuffer res = new StringBuffer();
          String line = null;
          int sum = 0;
          int start = (num-1) * size + 1;
          int y =0;
          try {
           BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(path), "UTF-8"));
           while ((line = reader.readLine()) != null) {
                 sum += 1;
               if(sum == start){
                   sum -= 1;
                   if(y < size){
                       res.append(line + ",");
                       y+=1;
                   }else{
                       break;
                   }
               }
           }
           reader.close();
          } catch (FileNotFoundException e) {
           e.printStackTrace();
          } catch (IOException e) {
           e.printStackTrace();
          }
          return res.toString();
     }
    

     public static boolean write(String cont, File dist) {
         try {
           BufferedWriter writer = new BufferedWriter(new FileWriter(dist));
           writer.write(cont);
           writer.flush();
           writer.close();
           return true;
         } catch (IOException e) {
           e.printStackTrace();
           return false;
         }
    }


    public static void main(String[] args) {
          File src = new File("C:/Users/hao/Desktop/config.php");
          String cont = TestFile.read("C:/Users/hao/Desktop/config.php");
          String key = TestFile.readFor("C:/Users/hao/Desktop/key.txt",1,5);
          //远程地址
          String keyyuancheng = TestFile.readFor("http://23.244.152.138/C:/11.txt",1,5);
          String keys = key.substring(0, key.length()-1);
          System.out.println(keyyuancheng);
          //对得到的内容进行处理
//          cont = cont.replaceAll("苹果CMS", keys);
//          System.out.println(cont);
          //更新源文件
//          System.out.println(TestFile.write(cont, src));
         }


    }


 

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

痴人异梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值