• java基础——随机访问流


    package stream;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    import org.junit.jupiter.api.Test;
    
    
    
    /*
     * RandomAccessFile:随机读写访问流
     *     既可以是输入流,也可以是输出流
     *     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("周恩杰".getBytes());
            raf1.write(sb.toString().getBytes());
            raf1.close();
        }
    }
  • 相关阅读:
    HDOJ 1202 The calculation of GPA
    HDOJ 1197 Specialized Four-Digit Numbers
    HDOJ 1196 Lowest Bit(二进制相关的简单题)
    HDOJ 1194 Beat the Spread!(简单题)
    NOIP2018游记
    CF1043
    洛谷P1280 尼克的任务
    洛谷P1155 双栈排序
    SPOJ6340 ZUMA
    chessboard
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12160914.html
Copyright © 2020-2023  润新知