Java IO编程 - IO操作深入

IO操作深入

1、字符编码

在进行编码的时候如果想要正确显示出内容则一定需要解码,那么编码和解码就需要使用统一的标准,如果不统一就会出现乱码

开发之中常用的编码:

  • GBK/GB2312:国标编码,可以描述中文信息,其中GB2312只描述简体中文,GBK可以描述简体中文和繁体中文
  • ISO8859-1:国际通用编码,可以用其描述所有的字母信息,如果是象形文字则需要进行编码处理
  • UNICODE :采用十六进制的方式存储,可以描述所有的文字信息
  • UTF:文字部分使用十六进制编码,而普通的字母采用的是ISO8859-1编码,优势在于适合快速传输,节约带宽,成为了开发之中的首选,主要使用“UTF-8”

列出本机属性:

public class JavaDemo {
   public static void main(String[] args) throws IOException {
      System.getProperties().list(System.out);
   }
}

不设置编码的时候会采用默认的编码进行

项目中乱码的问题就是编码和解码的标准不统一,最好的解决方式就是所有的编码都用UTF-8

2、内存操作流

假设需要使用IO操作,但是不希望产生文件(临时文件)则就可以以内存为终端进行处理

在Java里面提供有两类内存操作流

  • 字节内存操作流:ByteArrayOutputStream、ByteArrayInputStream
  • 字符内存操作流:CharArrayWriter、CharArrayReader

以ByteArrayOutputStream和ByteArrayInputStream类为主进行内存的使用分析

  • ByteArrayOutputStream构造方法:public ByteArrayOutputStream()
  • ByteArrayInputStream构造方法:public ByteArrayInputStream(byte[] buf)

ByteArrayOutputStream类里面有一个重要的方法,这个方法可以获取全部的保存在内存流中的信息,方法为:

  • 获取数据:public byte[] toByteArray()
  • 使用字符串的形式来获取:Public String toString()

利用内存流实现一个小写字母转大写字母的操作

public class JavaDemo {
   public static void main(String[] args) throws IOException {
      String str = "hello";
      //将内容保存在内存流
      ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes());
      //读取内存中的数据
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      int data = 0;
      //每次读取一个字节
      while ((data = input.read()) != -1) {
         //保存数据
         output.write(Character.toUpperCase(data));
      }
      System.out.println(output);
      input.close();
      output.close();
   }
}

如果现在不希望只是以字符串的形式返回,因为可能存放的是其他的二进制的数据,那么可以利用ByteArrayOutputStream子类的扩展功能获取数据

public class JavaDemo {
   public static void main(String[] args) throws IOException {
      String str = "hello";
      //将内容保存在内存流
      ByteArrayInputStream input = new ByteArrayInputStream(str.getBytes());
      //读取内存中的数据
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      int data = 0;
      //每次读取一个字节
      while ((data = input.read()) != -1) {
         //保存数据
         output.write(Character.toUpperCase(data));
      }
      byte[] result = output.toByteArray();
      //自己处理字节数据
      System.out.println(new String(result));
      input.close();
      output.close();
   }
}

在最初的时候可以利用ByteArrayOutputStream实现大规模文本文件的读取

3、管道流

管道流的主要功能是实现两个线程之间的IO处理操作

管道流也分为两类:

  • 字节管道流:PipedOutputStream、PipedInputStream
    • 连接处理:public void connect(PipedInputStream snk) throws IOException
  • 字符管道流:PipWriter、PipReader
    • 连接处理:public void connect(PipedReader snk) throws IOException

实现管道操作

public class JavaDemo {
   public static void main(String[] args) throws IOException {
      SendThread send = new SendThread();
      ReceiveThread receive = new ReceiveThread();
      send.getOutput().connect(receive.getInput());
      new Thread(send,"消息发送线程").start();
      new Thread(receive,"消息接收线程").start();
   }
}
class SendThread implements Runnable{
   private PipedOutputStream output;   //管道输出流
   public SendThread(){
      this.output = new PipedOutputStream();  //实例化管道输出流
   }
   
   @Override
   public void run() {
         try {   //利用管道实现数据的发送处理
            output.write(("发送消息:" + Thread.currentThread().getName() +"123\n").getBytes());
         } catch (Exception e) {
            e.printStackTrace();
         }
         try {
            output.close();
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
   }
   
   public PipedOutputStream getOutput() {
      return output;
   }
}
class ReceiveThread implements Runnable {
   private PipedInputStream input;
   public ReceiveThread() {
      this.input = new PipedInputStream();
   }
   
   @Override
   public void run() {
      byte[] data = new byte[1024];
      ByteArrayOutputStream bos = new ByteArrayOutputStream();  //所有数据保存到内存输出流
      int len = 0;
         try {
            while ((len = input.read(data)) != -1) {
               bos.write(data, 0, len);    //所有数据保存到内存流
            }
            System.out.println(Thread.currentThread().getName() + "接收消息\n" + new String(bos.toByteArray()));
            bos.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
      try {
         input.close();
      }catch (IOException e) {
          e.printStackTrace();
      }
   }
   
   public PipedInputStream getInput() {
      return input;
   }
}

管道就类似于医院打点滴效果,一个只负责发送,一个负责接收,中间靠一个管道连接

4、RandomAccessFile

对于文件内容的处理操作主要是通过InputStream (Reader)、OutputStream( Writer)来实现,但是利用这些类实现的内容读取只能够将数据部分部分读取进来

如果有一个文件,假设是几十个G,那么按照传统的IO操作进行读取分析根本不可能完成。因此在java.io包里提供有RandomAccessFile类。可以实现文件跳跃式读取,前提条件是数据的保存位置要确定好

RandomAccessFile类里面定义有如下操作方法:

  • 构造方法:public RandomAccessFile(File file,String mode)throws FileNotFoundException
    • 文件处理模式:r、rw

实现文件的保存

public class JavaDemo {
   public static void main(String[] args) throws IOException {
      File file = new File("E:" + File.separator + "hello" + File.separator + "666.txt"); //定义操作文件
      RandomAccessFile raf = new RandomAccessFile(file, "rw");    //读写模式
      String[] names = {"zhangsan", "wangwu", "lisi"};
      int[] ages = {10, 20, 30};
      for (int i = 0; i < names.length; i++) {
         raf.write(names[i].getBytes()); //写入字符串
         raf.write(ages[i]);
      }
      raf.close();
   }
}

RandomAccessFile最大的特点是在于数据的读取处理上,因为所有的数据是按照固定的长度进行的保存,所以读取的时候就可以跳跃读取:public int skipByte(int n) throws IOException

读取数据

public class JavaDemo {
   public static void main(String[] args) throws IOException {
      File file = new File("E:" + File.separator + "hello" + File.separator + "666.txt"); //定义操作文件
      RandomAccessFile raf = new RandomAccessFile(file, "rw");    //读写模式
      {   //读取李四的数据,跳过24位
         raf.skipBytes(24); //写入字符串
         byte[] data = new byte[8];
         int len = raf.read();
         System.out.println(new String(data , 0 , len).trim() + raf.readInt());
      }
      raf.close();
   }
}

整体的使用之中由用户自行定义要读取的位置,而后按照指定的结构进行数据的读取

下一篇:输入与输出支持

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MyRedScarf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值