IO流对应代码

package com.seecen.javaApi;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;

import org.junit.Test;

public class TestRandomAccessFile {
    
    
    public void testWrite() throws Exception{
//        1.创建能够读写的RandomAccessFile类的对象
        RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
//        2.使用write方法向文件raf.txt写入数组1
//        该方法写出的是一个字节,写出的是int值的低8位
        raf.write(111);
        raf.close();
    }
    
    
    public void testRead()throws Exception {
//        1.创建能够读写的RandomAccessFile类的对象
        RandomAccessFile raf = new RandomAccessFile("raf.txt","r");
//        2.读取一个字节
        int a = raf.read();
        System.out.println(a);
        raf.close();
    }
    
    
    public void testGetFilePointer() throws Exception{
//        1.创建能够读写的RandomAccessFile类的对象
        RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
        raf.write('A');//字母占一个字节
        System.out.println(raf.getFilePointer());
        raf.writeInt(3);//int类型的数据占4个字节
        System.out.println(raf.getFilePointer());
        
//        测试Seek方法
//        将指针移动到文件的开始处(第一个字节的位置)
        raf.seek(0);
        System.out.println(raf.getFilePointer());
        raf.close();
    }
    
    
    
//    测试批量写出
    public void testWriteByteArray() throws Exception{
//        1.创建能够读写的RandomAccessFile类的对象
        RandomAccessFile raf = new RandomAccessFile("raf.txt","rw");
//        2.使用write(byte[])方法将字符串"HelloWorld"写入
        byte[] buf = "HelloWorld".getBytes();
        raf.write(buf);
//        查看大小
        System.out.println(raf.length());
        raf.close();
    }
    
    
    
//    测试批量读取
    public void testReadByteArray() throws Exception{
//        1.创建能够读写的RandomAccessFile类的对象
        RandomAccessFile raf = new RandomAccessFile("raf.txt","r");
//        创建一个字节数组,长度为10
        byte[] buf = new byte[10];
        int len = raf.read(buf);
        System.out.println("读取到了:"+len+"个字节");
        System.out.println(new String(buf));
//        获取文件raf.txt指针
        System.out.println("指针的位置:"+raf.getFilePointer());//10
//        设置指针读取world
        raf.seek(0);
        raf.skipBytes(5);
        System.out.println("指针的位置:"+raf.getFilePointer());//5
//        
        byte[] buf1 = new byte[5];
        int len1 = raf.read(buf1);
        System.out.println(new String(buf1));
        raf.close();
        }
        
    
    public void testFileOutputStream() throws Exception{        
        FileOutputStream fos = new FileOutputStream("raf.txt");
        byte[] buf = "HelloWorld".getBytes();
        fos.write(buf);
        FileOutputStream fos1 = new FileOutputStream("raf.txt",true);
        byte[] buf1 = "你好".getBytes();
        fos1.write(buf1);
        fos.close();
        fos1.close();
        }
    
    
    @Test    
    public void testFileInputStream() throws Exception{        
        FileInputStream fis = new FileInputStream("raf.txt");
        byte[] a = new byte[2];
        fis.read(a);
        System.out.println(new String(a));
    }

}
 

猜你喜欢

转载自blog.csdn.net/CXY_ZPH/article/details/86176412
今日推荐