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");
    }
}
发布了65 篇原创文章 · 获赞 16 · 访问量 5058

猜你喜欢

转载自blog.csdn.net/gwdfff/article/details/103922043
今日推荐