JAVA IO

一、基本概念

Java.io 包定义了多个流类型(类或抽象类)来实现输入输出功能。

按数据流的方向: 输入流                                           输出流

按处理数据单位: 字节流                                           字符流

按功能不同:         节点流(直接连接数据流)       处理流(连接节点流)


二、具体实例

                                               字节流                           字符流

输入流                             InputStream                      Reader

输出流                            OutputStream                    Writer


节点流类型

File                                    字节流                              字符流(可以处理中文)

In                                      FileInputStream               FileReader

Out                                   FileOutputStream            FileWriter



处理流类型

Buffer(可以read one line)                                 字节流                                          字符流

In                                                                         BufferedInputStream                BufferedReader

Out                                                                      BufferedOutputStream             BufferedWriter


三、使用

要在异常处理块内使用。

import java.io.*;
public class CopyFile {

   public static void main(String args[]) throws IOException {  
      FileInputStream in = null;
      FileOutputStream out = null;

      try {
         in = new FileInputStream("input.txt");
         out = new FileOutputStream("output.txt");
         
         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}
Reference from:https://www.tutorialspoint.com/java/java_files_io.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值