利用FileReader,BufferedReader,FileWriter读取某个文本的信息再写入另外一个文本中

利用FileReader,BufferedReader,FileWriter读取某个文本的信息再写入另外一个文本中

package com.example;

import java.io.*;

public class MainDemo03 {
    public static String txtString(File file) throws IOException{
        String result="";
        String FilePath="F:/test.txt";      //写出文件的文件位置
        FileReader fileReader = null;
        BufferedReader bufferedReader=null;
        try {
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader); //构造一个bufferReader类来读取文件
            String s=null;
            CreateFile(FilePath);       //调用方法CreateFile创建一个记事本文件
            while ((s=bufferedReader.readLine())!=null){
                String[] text = s.trim().split(";");  //通过分号对一行数据进行分割
                String newString=text[0]+text[1];
                int calculate= Integer.parseInt(text[2])+Integer.parseInt(text[3]);
                String newText=newString+";"+calculate+"\n";        //  "\n"换行
                WriteFile(FilePath,newText);            //调用写方法
            }
            result="ok";
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileReader!=null){
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();         //读完之后关闭   io基本操作 勿忘
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       return result;
    }

    public static void CreateFile(String FilePath) throws IOException {
        File file=new File(FilePath);
        if(!file.exists()){
            file.createNewFile();
        }
    }
    public static void WriteFile(String FilePath,String fileInfo) throws IOException {      //传入两个参数 写出的文件位置和写的信息
        /*file:要写入数据的 File 对象。
          append:如果 append 参数为 true,则将字节写入文件末尾处,相当于追加信息。如果 append 参数为 false, 则写入文件开始处。*/
        FileWriter fileWriter= null;
        try {
            fileWriter = new FileWriter(FilePath,true);
            fileWriter.write(fileInfo);
            fileWriter.flush();//每次io都会影响性能,将需要写入的内容,放入缓冲区中,然后调用flush方法,将缓冲区内容写入文件中。
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(fileWriter!=null){
                fileWriter.close();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        File file=new File("F:\\ideafile2\\FileChange\\src\\main\\resources\\test.txt");
        System.out.println(txtString(file));
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值