io流读写文件(文件内容修改)

package cn.sos.psasps;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Random {

public static void main(String[] args) {
	String filePath = "D:\\psasp\\LF.L2";
	writer(filePath,heel(filePath));
	//heel(filePath);
}

/**

  • 读取文件

  • @param filePath
    */
    public static List heel(String filePath){
    BufferedReader br = null;

     String line = null;
     List<String>list = new ArrayList<>();
     StringBuffer buf = new StringBuffer();
     
     try {
     	//根据文件路径创建缓冲输入流
     	br = new BufferedReader(new FileReader(filePath));
     	//循环读取文件的每一行,对需要进行修改的行进行修改,放入缓存对象中
     	
     	while ((line = br.readLine()) != null) {
     		//此处根据实际需要修改某些行的内容
     		buf.append(" ").append(" ").append((int)(Math.random()*2)).append(line.substring(3));
     		//使用list记录所有的数据
     		list.add(buf.toString());
     		System.out.println(buf.toString());
     		//清空可变字符串,重新记录数据
     		buf.delete(0, buf.length());
     	}
     } catch (Exception e) {
     	e.printStackTrace();
     }finally{
     	try {
     		br.close();
     		
     	} catch (IOException e) {
     		e.printStackTrace();
     	}
     }
     return list;
    

    }
    /**

  • 将文件回写到文件中
    */
    public static void writer(String filePath,List content){
    BufferedWriter bw = null;
    try {
    //根据文件路径创建缓冲输出流
    bw = new BufferedWriter(new FileWriter(filePath));
    //将内容写入到文件中
    for (int i = 0; i < content.size(); i++) {
    bw.write(content.get(i));
    bw.newLine();
    }

     } catch (Exception e) {
     	e.printStackTrace();
     }finally{
     	try {
     		bw.close();
     	} catch (IOException e) {
     		
     		e.printStackTrace();
     	}
     }
    

    }

}
注:此代码是修改文件中的第一列随机0和1

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

微微一笑满城空

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

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

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

打赏作者

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

抵扣说明:

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

余额充值