Java String字符串和文件File读取写入处理

文件读取与字符串处理.

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class string_manipulation{
    // (2020-1-10 10:59:51)

    // (2020-1-10 12:01:23)
    public static void deletestrs(String file_copy_from,String file_save_to) {
        //文件操作.(2020-1-10 11:16:25)
        String teststr="";
        // https://www.jb51.net/article/143723.htm
        File file1=new File(file_copy_from);
        //输入名字.
        try {
            FileReader filereader1=new FileReader(file1);
            int ch=0;
            while((ch=filereader1.read())!=-1){
                teststr+=(char)ch;
                // System.out.print((char)ch);
            }
            filereader1.close();
            System.out.println("ok----------------------------------------------------------------");

        } catch (IOException e) {
            //TODO: handle exception
            e.printStackTrace();
            System.out.println("bug----------------------------------------------------------------");
        }
        

        //处理过程.
        String str2=teststr.replaceAll("", "");
        // 注意.赋值给新对象生效.(2020-1-10 11:15:51)
        System.out.println(teststr+"\n----------------------------------------------------------------"+str2);
        // 文件写入(2020-1-10 11:49:16)
        // https://blog.csdn.net/woodwoodxiaohei/article/details/94441131
        FileOutputStream outputstream1=null;
        try {
            File file2=new File(file_save_to);
            
            // 如果B已存在?
            // file2.createNewFile();
            outputstream1=new FileOutputStream(file2);
            outputstream1.write(str2.getBytes());
            //获取当前时间?生成文件名?
        } catch (Exception e) {
            //TODO: handle exception
            e.printStackTrace();
            System.out.println("bug-output----------------------------------------------------------------");
        } finally{
            try {
                outputstream1.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) {
        string_manipulation.deletestrs("E:\\a.txt", "E:\\b.txt");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值