zoukankan      html  css  js  c++  java
  • RandomAccessFile拆分合并文件

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.RandomAccessFile;
    import java.io.SequenceInputStream;
    import java.util.Vector;
    
    public class RandonFileAccessTest {
        public static void main(String[] args) throws IOException {
           
            splitFile("test.txt");
            mergeFile();
            mergeFile2();
        }
        
        
        //拆分文件
        public static void splitFile(String path) throws IOException{
            InputStream is = new FileInputStream(path);
            int len=0;
            byte[] buff = new byte[1024];
            int i = 1;
            while((len=is.read(buff))!=-1){
                RandomAccessFile raf = new RandomAccessFile("raf"+i+".txt", "rw");//raf1.txt raf2.txt 一共只有两个文件
                raf.write(buff,0,len);
                raf.close();
                i++;
            }
            is.close();
        }
        
      //合并文件
    public static void mergeFile() throws IOException{ RandomAccessFile raf = new RandomAccessFile("raf3.txt", "rw"); InputStream is = null; for(int i=1;i<3;i++){ int len=0; is = new FileInputStream("raf"+i+".txt"); //一共就两个文件 byte[] buff = new byte[1024]; while((len=is.read(buff))!=-1){ raf.write(buff,0,len); } } is.close(); raf.close(); }
      //另一种方式的合并文件
    public static void mergeFile2() throws IOException{ RandomAccessFile raf = new RandomAccessFile("raf4.txt", "rw"); InputStream is = null; Vector<InputStream> vt = new Vector<InputStream>(); for(int i=1;i<3;i++){ is = new FileInputStream("raf"+i+".txt");//raf1.txt raf2.txt vt.addElement(is); } SequenceInputStream sis = new SequenceInputStream(vt.elements()); int len=0; byte[] buff = new byte[1024]; while((len=sis.read(buff))!=-1){ raf.write(buff,0,len); } sis.close(); is.close(); raf.close(); } }
  • 相关阅读:
    训练深度学习网络时候,出现Nan 或者 震荡
    Jupyter Notebook 的快捷键
    pyspark RandomForestRegressor 随机森林回归
    深度学习图像标注工具VGG Image Annotator (VIA)使用教程
    python 中 with 用法
    python 报错 SyntaxError: Non-ASCII character
    YOLO 详解
    Spark与Pandas中DataFrame对比
    利用WGET下载文件,并保存到指定目录
    http 三次握手
  • 原文地址:https://www.cnblogs.com/Iqiaoxun/p/6021146.html
Copyright © 2011-2022 走看看