zoukankan      html  css  js  c++  java
  • Java-文件合并,文件追加

    因为有人问怎么写,就写了个demo,亲测可用

        @Test
        public void appendFile() throws IOException {
            File fileout = new File("/www.cnblogs.com/tomcatandjerry/abc1.txt");
            try (
                    RandomAccessFile raf = new RandomAccessFile(fileout, "rw");
              //这里分开写,不要FileChannel fc = new FileInputStream().getChannel(),这样只会关闭fc,但是FileInputStream没有关闭
                    FileInputStream fis = new FileInputStream("/www.cnblogs.com/tomcatandjerry/abc.txt");
                    FileChannel fc2 = fis.getChannel();
            ) {
                raf.seek(raf.length());//移动位置到文件末,这样后面追加数据就不会覆盖原来的内容
                fc2.transferTo(0, fis.available(), raf.getChannel());
            }
        }
    

      

     使用jdk7的try with resource语法,让代码看上去精简一些

  • 相关阅读:
    线程的补充
    线程
    进程
    操作系统和进程
    socketserver模块实现并发和连接合法性验证
    socketserver实例化过程
    粘包现象和解决方法
    网络通信协议
    初探网络
    Python网络编程
  • 原文地址:https://www.cnblogs.com/tomcatandjerry/p/12030261.html
Copyright © 2011-2022 走看看