zoukankan      html  css  js  c++  java
  • RandomAccessFile

    import java.io.File;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.Arrays;
    
    public class RafDemo {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
    
            File demo = new File("demo");
            
            if(!demo.exists())
                demo.mkdir();
            
            File file = new File(demo,"raf.dat");
            if(!file.exists())
                file.createNewFile();
            
            RandomAccessFile raf = new RandomAccessFile(file,"rw");
            
            raf.write('A');
            System.out.println(raf.getFilePointer());
            raf.write('B');
            System.out.println(raf.getFilePointer());
            
            int i = 0x7fffffff;
            
    //        int j = i >>> 24;
    //        System.out.println( Integer.toHexString(j));
            
            raf.write(i >>> 24);
            raf.write(i >>> 16);
            raf.write(i >>> 8);
            raf.write(i);
            System.out.println(raf.getFilePointer());
            
            raf.writeInt(i);
            
            String s = "中";
            byte[] gbk = s.getBytes("gbk");
            raf.write(gbk);
            
            System.out.println(raf.length());
            
            
            raf.seek(0);
            
            byte[] buf = new byte[(int)raf.length()];
            
            raf.read(buf);
            System.out.println(Arrays.toString(buf));
            
            String s1 = new String(buf,"gbk");
            System.out.println(s1);
            
            raf.close();
        }
    
    }

    Console:

    1
    2
    6
    12
    [65, 66, 127, -1, -1, -1, 127, -1, -1, -1, -42, -48]
    AB??????中

  • 相关阅读:
    TensorFlow实现LeNet5模型
    jmeter+influxdb+grafana性能测试可视化报告
    jmeter命令执行脚本
    jmeter JSON Extractor使用
    jmeter上传文件
    jenkins登录信息无效,忘记密码
    XML
    JMeter函数和变量
    jmeter配置CSV Data Set Config
    jmeter发送Query String Parameters格式参数报错
  • 原文地址:https://www.cnblogs.com/limeina/p/5467257.html
Copyright © 2011-2022 走看看