zoukankan      html  css  js  c++  java
  • RandomAccessFile 学习

    package com.java.xuelei;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;
    
    public class TestRandomAccessFile {
        public static void main(String[] args) {
            try {
                RandomAccessFile raf = new RandomAccessFile("c:/123.txt", "rw");
    //            raf.seek(6);   //指针偏移量为6    指针从第6个字节开始读取
                raf.seek((raf.length()));  //定义指针到尾部   相当于追加
                raf.writeBytes("hahaha");  //写入"hahaha"
                String a=null;
                while((a=raf.readLine())!=null){    //readLine()方法从此文件读取文本的下一行。
                    System.out.println(a);
                }
                long len = raf.length();    //返回文件的长度   返回值为long类型    换行占用2个长度
                System.out.println("此文件的长度为"+len);
                
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();    
            }catch (Exception e) {
                System.out.println("异常");
            }
        }
    
    }

    package com.java.xuelei;

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.RandomAccessFile;

    public class TestRandomAccessFile {
        public static void main(String[] args) {
            try {
                RandomAccessFile raf = new RandomAccessFile("c:/123.txt", "rw");
    //            raf.seek(6);   //指针偏移量为6    指针从第6个字节开始读取
                raf.seek((raf.length()));  //定义指针到尾部   相当于追加
                raf.writeBytes("hahaha");  //写入"hahaha"
                String a=null;
                while((a=raf.readLine())!=null){    //readLine()方法从此文件读取文本的下一行。
                    System.out.println(a);
                }
                long len = raf.length();    //返回文件的长度   返回值为long类型    换行占用2个长度
                System.out.println("此文件的长度为"+len);
                
                
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();    
            }catch (Exception e) {
                System.out.println("异常");
            }
        }

    }

  • 相关阅读:
    210111做个期望值低的人
    error_1 springboot `com.mysql.jdbc.Driver'问题
    error_2 springboot mysql时区设置
    17_springboot Restful风格
    15_JSON springboot
    13_springboot文件上传
    12_springboot myBatis crud
    11_springboot JPA crud
    Swagger导出MarkDown
    Docker 使用中的一些问题
  • 原文地址:https://www.cnblogs.com/wojiaoxuelei/p/6539192.html
Copyright © 2011-2022 走看看