首页 > 技术文章 > Java写入到文件末尾而不清空文件

bricyang 2017-03-31 17:13 原文

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;

public class CpFile {

  public static void main(String[] args) {
    String path1 = "D:\\data\\LinkedGeoData\\lgd-elements.nt";
    String path2 = "D:\\data\\LinkedGeoData\\lgd-complete.nt";
    try {
        FileInputStream fi = new FileInputStream(path1);
        FileOutputStream fo = new FileOutputStream(path2,true);
        Scanner sc = new Scanner(fi,"UTF-8");
        BufferedOutputStream bo = new BufferedOutputStream(fo);
        while(sc.hasNextLine()){
            String str=sc.nextLine()+"\n";
            bo.write(str.getBytes());
        }
        sc.close();
        fi.close();
        bo.close();
        fo.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
  }

}

推荐阅读