• 将文本1的内容写入到文本2中


    package com.pdcss.io;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    public class IOTest {

        /**
         * file1>>file2
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            File infile = new File("D:\1.txt");//这个必须有,自己在里面写点内容
            File outfile = new File("D:\2.txt");//可以没有,会自动建
            FileReader fr = null ;
            FileWriter fw = null;
            try {
                fr = new FileReader(infile);
                fw = new FileWriter(outfile);
                
                BufferedReader br = new BufferedReader(fr);
                BufferedWriter bw = new BufferedWriter(fw);
                
                String line = null;
                String[] oStr = null;
                
                while((line=br.readLine())!= null){
                    bw.write(line+System.getProperty("line.separator"));
                }
                bw.close();
                br.close();
                fw.close();
                fr.close();
                
            } catch (FileNotFoundException e) {
                System.out.println("文件找不到");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("写入失败");
                e.printStackTrace();
            }
            System.out.println("写入成功");
        }

    }

  • 相关阅读:
    HDU1720 A+B Coming
    HDU1390 ZOJ1383 Binary Numbers
    HDU1390 ZOJ1383 Binary Numbers
    HDU2504 又见GCD
    HDU2504 又见GCD
    HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking
    HDU1335 POJ1546 UVA389 UVALive5306 ZOJ1334 Basically Speaking
    HDU1020 ZOJ2478 Encoding
    HDU1020 ZOJ2478 Encoding
    HDU2097 Sky数
  • 原文地址:https://www.cnblogs.com/hj918/p/3460101.html
Copyright © 2020-2023  润新知