• java 随机读写访问流及seek方法


    package stream;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    import org.junit.jupiter.api.Test;
    
    
    
    /*
     * RandomAccessTile:随机读写访问流
     *     既可以是输入流,也可以是输出流
     *     r|w|d|s:读|写|更新数据|元数据更新 
     *     
     *     w:是对开头对文件内容进行覆盖
     * 
     * */
    
    public class RandomAccessFileTest {
        
        @Test
        public void test1(){
            
            RandomAccessFile raf1 = null;
            RandomAccessFile raf2 = null;
            try {
                //1.流对象
                raf1 = new RandomAccessFile(new File("hello.txt"),"r");
                raf2 = new RandomAccessFile(new File("hello2.txt"),"rw");
                
                //2.读写
                byte[]     buffer = new byte[1024];
                int len;
                while((len = raf1.read(buffer))!=-1) {
                    raf2.write(buffer,0,len);
                }
            } catch (Exception e) {
                
                e.printStackTrace();
            }
            finally {
                try {
                    if(raf1!=null)
                        raf1.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    if(raf2!=null)
                        raf2.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        
        /*
         * seek随机访问|插入方法
         * 
         * */
        @Test
        public void test2() throws IOException {
            RandomAccessFile raf1 = new RandomAccessFile("hello.txt", "rw");
            /*raf1.seek(3); 
            raf1.write("abc".getBytes());
        */
            /*
             * 使用StringBuilder
             * */
            StringBuilder sb = new StringBuilder((int)(new File("hello.txt").length()));
            raf1.seek(3L);
            int len;
            byte[] buffer = new byte[20];
            while((len = raf1.read(buffer))!=-1) {
                sb.append(new String(buffer,0,len));
            }
            
            raf1.seek(3L);
            raf1.write("xyz".getBytes());
            raf1.write(sb.toString().getBytes());
            raf1.close();
        }
        
    }
  • 相关阅读:
    软件补丁问题(网络流24题)
    飞行员配对方案问题(网络流24题)
    [NOIp普及组2011]瑞士轮
    如何在Linux上安装QQ
    [洛谷P2420] 让我们异或吧
    高斯消元
    [SCOI2014]方伯伯的OJ
    [USACO1.3]虫洞
    KMP算法讲解
    [洛谷P1382] 楼房
  • 原文地址:https://www.cnblogs.com/zsben991126/p/11881233.html
Copyright © 2020-2023  润新知