Demo

20200329

RandomAccessFile

java中唯一一个用于双向操作的类:

  1. RandomAccessFile(File,String->r/rw)
  2. int read()/

文件指针:

通过指针就可以在文件的任意位置读或者写:

  1. getfilePointer()获取文件指针所在的位置
  2. seek(long)将文件指针设置到指定位置
  3. skipBytes(long)将文件向后移动多少字节

FileOutputStream

  1. FileOutputStream(String)给定具体操作的文件地址
  2. FileOutputStream(File)给定具体操作的文件
  3. FileOutputStream(String,boolean)boolean表示是否追加true表示追加
  4. FileOutputStream(File,boolean)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Am01 {
    public static void main(String[] args) {
        File file=new File("file/fos.txt");
        if (!file.exists()){
            try{
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try(
                FileOutputStream fos=new FileOutputStream(file,true);
                ) {
            fos.write('A');
            fos.write("fjhfdbsjfbj".getBytes());
            fos.write("snfjsdjfsfds".getBytes(),6,7);
            System.out.println("写出完毕");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  1. Write(int)写该整数第八位
  2. write(byte[])将整个字节写入的文件中
  3. write(byte[],off,lenth)将字节数组中的内容从指定位置开始写,写length个
        if(!file.exists()){
            System.out.println("文件不存在");
            return;
        }
        try(
                FileInputStream fis=new FileInputStream(file);
                ) {
            int i=fis.read();
            //读取一个字节
            System.out.println((char)i);
            byte[]bs=new byte[1024];

           /* int k=fis.read(bs);
            System.out.println(new String(bs,0,k));
            //尝试读取bs长度的数据,返回值表示实际读取到的字节量*/

            int j=fis.read(bs,5,10);
            //尝试读取length个字节,返回值表示实际读取到的字节量
            System.out.println(new String(bs,5,j));

        } catch (Exception e) {
            e.printStackTrace();
        }

FileInputStream

  • FileInputStream

BufferedOutputStream/BufferedInputStream

  1. 带有缓冲区的流
  2. 程序中维护一个缓冲区
  3. 降低了写文件操作的频率
  4. 提高了写操作的效率
  • BufferedOutputStream
  1. BufferedOutputStream(outputStream) 先写入缓冲区,当缓冲区慢之后在通过包装的底层流将内容写出的文件
  2. write(int)
  3. write(byte[])
  4. write(byte[],off,length)
  5. flush()手动清理缓冲区,再调用close()方法时会先调用flush()方法再关闭流

import java.io.*;

public class Am02 {
    public static void main(String[] args) {
        File file=new File("src/Am/fos.txt");
        if (!file.exists()){
            try{
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try(
                FileOutputStream fos=new FileOutputStream(file,true);
                BufferedOutputStream bos=new BufferedOutputStream(fos);
                ) {
            bos.write("sjdfskajdfjsgdfjsd".getBytes());
           // bos.flush();//将缓冲区中的内容全部写到文件,并清空缓冲区
            System.out.println("写出完毕");
        //    bos.close();//先清空缓冲区,再关闭流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • BufferedInputStream
  1. BufferedInputStream(InputStream)
  2. read()
  3. read(byte[])
  4. read(byte,off,length)

import java.io.*;

public class Am02 {
    public static void main(String[] args) {
        File file=new File("src/Am/fos.txt");
        if (!file.exists()){
           return;
        }
        try(
                FileInputStream fos=new FileInputStream(file);
                BufferedInputStream bos=new BufferedInputStream(fos);
                ) {
           byte[]bs=new byte[1024];
           int i=bos.read(bs);
            System.out.println(new String(bs,0,i));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ObjectInputStream/ObjectOutputStream

  • 对象流
  • ObjectOutputStream
  1. 写对象到文件中
  2. 对象所属类实现Serializable(序列化接口)
  3. 只要类实现序列化接口--》编译器就会自动添加一个方法--〉将对象转为字节数组
  • 持久化:将数据保留下来,将内存中的数据转移到磁盘中

  • 序列化:将对象转为字节数组

  • 反序列化:将字节数组转为对象

  • ObjectInputStream

  1. 将文件中的对象读到程序中
  2. 将字节数组转为对象--》反序列化
  3. readObject()

 

  • Reader/writer抽象类,所有字符流的父类--》char为单位

  • InputStream/OutputStream-->抽象类,所有字节流的父类--〉byte为单位

  • InputStreamReader/OutputStreamWriter转换流--》java中唯一一组可以将字节流转为字符流

  • OutputStreamWriter

  1. OutputStreamWriter(OutputStream)
  2. write(int):写低十六位
  3. write(char[])
  4. write(String)
  5. write(char[],off,length)
  6. write(String,off,length)

import java.io.*;

public class Pm01 {
    public static void main(String[] args) {
        File file=new File("fos.txt");
        if (!file.exists()){
            try{
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (
                FileOutputStream fos=new FileOutputStream(file);
                OutputStreamWriter oesw=new OutputStreamWriter(fos,"GBk");
                ){
            oesw.write('中');
            oesw.write("kdjhfshjdfkshdfksks".toCharArray(),3,3);
            oesw.write("hbdsfjhdfah",3,3);
            System.out.println("写出完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • InputStreamReader
posted @   小兵学习笔记  阅读(91)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示