12:java IO流

IO
流(Stream): 将数据Byte By Byte的处理方式
InputStream
 |-- 节点流 (流开始的地方, 流的源泉)
 |    |-- FileInputStream 
 |    |-- ByteArrayInputStrem
 |-- 过滤流
 |    |-- BufferedInputStream
 |    |-- ObjectInputStream
 |    |-- ZipInputStream
 |    |-- InputStreamReader
 
OutputStream
 |-- 节点流 (流结束的地方, 流的目的)
 |    |-- FileOutputStream
 |    |-- ByteArrayOutputStream
 |-- 过滤流
 |    |-- BufferedOutputStream
 |    |-- ObjectOutputStream
 |    |-- ZipOutputStream
 |    |-- OutputStreamWriter

文本的处理(byte<->char, 编码转换)
Reader
  |-- InputStreamReader 核心 处理(byte-解码->char)
  |   如:  GBK(byte)-->Java Unicode(char)
  |-- FileReader=InputStreamReader+FileInputStream
  |-- BufferedReader 提供了readLine()

Writer
  |-- OutputStreamWriter 核心 处理(char-编码->byte)
  |   如:  Java Unicode(char) --> GBK(byte)
  |-- FileWriter=OutputStreamWriter+FileOutputStream
  |-- PrintWriter 非常常用, 有println 方法

 

一. 要点
1. java.io.File
用于表示文件(目录);只用于表示文件(目录)
的信息(名称,大小等)不能对文件的内容进行访问
2. java.io.File基本API

File(String)
long length()
long lastModified()
String getName()
String getPath()
boolean exists()
boolean dir.isFile()
boolean dir.isDirectory()
boolean mkdir()
boolean mkdirs()
boolean delete();
boolean createNewFile() throw IOException
File[] listFile()

3. 回调模式和FileFilter
File[] listFile(FileFilter)


4. java.io.RandomAccessFile
可以访问(读/写)一个文件中任意位置的字节信息
// arg0 要访问的文件
// arg1 访问的模式 "r" 只读模式,"rw" 读写模式
RandomAccessFile(File, String)
   throws FileNotFoundException
RAF 维护一个指针,指向要读写的位置
指针会随着读写自动后移
int read()
void write(int)
seek(long)
long getFilePointer()

// 读取尽可能多的字节,填充作为参数的字节数组
int read(byte[])
write(byte[], int off, int len)

5.
java.io.InputStream  输入流
java.io.OutputStream 输出流

java.io.FileInputStream(File)    FIS
java.io.FileOutputStream(File)   FOS

java.io.BufferedInputStream(InputStream)  BIS
java.io.BufferedOutputStream(OutputStream) BOS

 

文本编码:

GBK: 1-2变长编码, 英文1字节与ascII一致, 中文2个Byte
中ABC:
d6       d0       41       42       43
11010110 11010000 01000001 01000010 01000011

UTF-8: 1-4变长编码, 英文1字节与ascII一致, 中文3个Byte
中ABC:
e4       b8       ad       41       42       43
11100100 10111000 10101101 01000001 01000010 01000011

UTF-8中文编码: 1110xxxx 10xxxxxx 10xxxxxxx
xxxx为实际的Unicode编码, 其他为编码控制位

UTF-16BE: 2字节定长编码,中英文都采用固定长度表示.也是Java的内部编码,
中ABC:
4e       2d       00       41       00       42       00       43
01001110 00101101 00000000 01000001 00000000 01000010 00000000 01000011


1) 二进制文件访问
   new BIS(new FIS(...))
   new BOS(new FOS(...))
2) 文本文件的访问
  new ISR(new FIS(...),"charset")
  new OSW(new FOS(...),"charset")
  new FR(...)
  new FW(...)
3)从控制台按行读取数据
 new BR(new ISR(System.in))
4)从文件按行读取数据
 new BR(new ISR(new FIS(...),"charset"))
 new BR(new FR(...))
5) 将各种类型数据以字符串形式输出到文件
 new PW(new OSW(new FOS(...),"charset"))
 new PW(new FW(...))
6) 对象序列化和反序列化
  new OOS(new FOS(...))
  new OIS (new FIS(...))

 

InputStream 字节输入流

InputStreamReader
 InputStream (byte) -> Unicode(char)
 
Reader 字符输入流

1 打开一个文本文件:
InputStream is = new FileInputStream("gbk.txt");
Reader in = new InputStreamReader(is);

2 打开一个文本文件:
Reader in = new InputStreamReader(
 new FileInputStream("gbk.txt"));
3 打开一个文本文件:
Reader in = new FileReader("gbk.txt");

4 打开输出文本文件
Writer out = new OutputStreamWriter(
 new FileOutputStream("filename"));
 
5 打开输出文本文件
Writer out = new FileWriter("filename");
 
文本文件的输出:
PrintWriter out =
 new PrintWtirer(new FileWriter(filename)); 

out.println("HI");

System.out.println("Hello");

文本文件的读取: BufferedReader,方法:readline();


阻塞式IO
等待用户的输入完成再继续运行程序的现象叫: 阻塞式IO
BufferedReader: readLine()
Scanner: next() nextLine() nextInt() ...

对象序列化
  int -> 4个byte,流
  long -> 8个byte, 流
  String 5个字符 -> GBK编码 10个Byte流
 
  Object -> n个byte流

1 ObjectOutputStream 和 ObjectInputStream
  writeObject(obj) 和 readObject()
 
二维数组
1 声明:
  int[][] ary;
 
2 初始化
  静态初始化:
    int[][] ary = {{2,3},{4,5},{6,7,8}};
  动态初始化:
    int[][] ary = new int[3][2];
    int[][] ary = new int[3][];
    int[][] ary = new int[][]{{2,3},{4,5},{6,7,8}};

二维数组:是数组的数组, 数组变量是一个对象数组的引用, 每个
行是一个一维数组的实例.

  int[] a = ary[0];
  Object[] o = ary;

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值