Java核心类库——IO原理和用法
Java IO流(InputStream/OutputSteam) 甚么是IO流? 简单说 : in是读取文件的 OutputStream:抽象了写出数据的进程 包括写出方法write(); 简单说 : out是写入文件的
基本的byte流 OutputStream(抽象方法write()) 注意:除节点流外都是过滤器流 字符流,可以处理字符编码,底层依赖于byte流
2) EOF = End of File = ⑴ (文件读到末尾会返回⑴) 3) 输入流的基本方法 4)输出流的基本方法: 1.FileInputStream (read()在文件上读取) 节点流 1 import java.io.FileInputStream; 2 import java.io.IOException; 3 import java.io.InputStream; 4 5 public class InputStreamDemo { 6 static void main(String[] args) 7 throws IOException { 8 String file = "out.txt"; 9 InputStream in = new FileInputStream(file); 10 int b; 11 while((b=in.read())!=⑴){//read()方法 12 System.out.print(Integer.toHexString(b) + " "); 13 } 14 in.close(); 15 16 in = 17 in.available() 可以读取的数据个数,小文件1般是文件长度 18 byte[] buf = new byte[in.available()]; 19 in.read(buf);read(byte[] buf)方法重载 20 in.close(); 21 for (byte c : buf) { 22 System.out.print(Integer.toHexString(c & 0xff) + " "); 23 c & 0xff --->将16进制写成0xff的格式 24 ffffffd6---> d6 25 22222111 22222111 22222111 11010110 &对应相乘 26 00000000 00000000 00000000 22222111 0xff 27 00000000 00000000 00000000 11010110 28 } 29 } 30 }
import java.io.*;
2
class OutputStreamDemo {
4 5 throws IOException{
6 String file = "out.txt";
7 OutputStream out = new FileOutputStream(file);
8 out.write(65);在文件中是以16进制存储的,对应0x41
9 out.write(66);0x42
byte[] buf = {(byte)0xd6,(byte)0xd0};
11 out.write(buf);
12 out.flush();刷出缓存,清算缓冲区,保证可靠写
13 out.close();
14 }
15 }
3.BufferedInputStream和BufferedOutputStream 的 用法 BufferedInputStream(FileInputStream in) import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
4 import java.io.FileOutputStream;
6 7
8 class BufferedStreamDemo {
9 void main(String[] args) BufferedInputStream普通写法
11 String file = "out.txt";
12 InputStream ins = 13 BufferedInputStream bufin= new BufferedInputStream(ins);
14 15 while((b=bufin.read())!=⑴){
16 System.out.println(Integer.toHexString(b));
17 }
经常使用写法,只要用到FileInputStream的地方都可以套1个BufferedInputStream用来提升性能
19 BufferedInputStream in = new BufferedInputStream(
20 new FileInputStream("out.txt"));
21
22 BufferedOutputStream
23 BufferedOutputStream out = new BufferedOutputStream(
24 new FileOutputStream("out.txt"));
25 out.write(65);
26 }
27 }
4.基本类型数据的写出和读入 1 例子
class DataOutDemo {
void main(String[] args)
5 throws IOException{
6 String file = "data.dat";项目文件夹
8 DataOutputStream 实现基本类型数据的序列化
9 将基本类型数据拆开为byte序列,写入到out流中
10 DataOutputStream dos = new DataOutputStream(out);
11 dos.write(⑵);
12 dos.writeInt(⑵);
13 dos.writeLong(⑵);
14 dos.writeByte(⑵);
15 dos.writeDouble(⑵);
16 dos.writeShort(⑵);
17 dos.writeFloat(⑵);
18 dos.writeBoolean(true);
19 dos.writeChar('中');
20 dos.close();
21
22 }
23 }
DataInputStream 方法: writeInt() writeChar() 等8种 import java.io.DataInputStream;
5
class DataInDemo {
7 9
10 String file = "data.dat";
11
12 InputStream in = 13 DataInputStream 从基本流中读取基本类型数据,实现基本
类型数据的反序列化
15 DataInputStream dis = new DataInputStream(in);
16 int b = dis.read();
int i = dis.readInt();
long l= dis.readLong();
19 byte bx = dis.readByte();
20 double d = dis.readDouble();
short s = dis.readShort();
float f = dis.readFloat();
23 boolean bol = dis.readBoolean();
24 char c = dis.readChar();
25 dis.close();
26 System.out.print( b +" ");254 fe
27 System.out.print(i+" ");
28 System.out.print(l+" ");
29 System.out.print(bx+" ");
30 System.out.print(d+" ");
31 System.out.print(s+" ");
32 System.out.print(f+" ");
33 System.out.print(bol+" ");
34 System.out.print(c+" ");
35
36 }
37 }
5 字符串的序列化(文字的编码方案) 如: utf⑻:国际标准,是将unicode编码为byte序列的方案,采取变长编码 1-N方案,其中英文1个byte,中文3个byte 以0开头的是英文(0⑴27) GBK: 中国标准,支持20000+中日韩文,英文编码1byte,中文2byte ISO8859⑴:只支持255个英文字符,不支持中文(Sun服务器默许编码,如tomcat等) 例子 import java.io.OutputStream;
class CharIODemo {
8 String str = "ABCD中国";
9 System.out.println(str);
Java 的字符是16位 Unicode值,而文件是8位Byte序列
11
12 GBK
13 System.out.println("GBK编码方案,对字符编码");
14 String file = "gbk.txt";
15 OutputStream out = new FileOutputStream(file);默许GBK编码方案
byte[] gbk = str.getBytes("GBK");
17 out.write(gbk);
18 out.close();
19 IOUtils.print(file);
UTF⑴6BE,每一个编码是2个字节
21 System.out.println("UTF⑴6BE编码方案,128); line-height:1.5!important">22 file = "utf⑴6be.txt";
23 out = byte[] utf16be = str.getBytes("UTF⑴6BE");
25 out.write(utf16be);
26 out.close();
27 IOUtils.print(file);
28
29 UTF⑻,英文是1个字节,中文是3个字节
30 System.out.println("UTF⑻编码方案,128); line-height:1.5!important">31 file = "utf⑻.txt";
32 out = 33 byte[] utf8 = str.getBytes("UTF⑻");编码string -> byte[]
34 out.write(utf8);
35 out.close();
36 IOUtils.print(file);
37
38 byte[] buf = IOUtils.read("utf⑻.txt");
39 new String(buf,"UTF⑻"),构造器可以将 byte编码序列
40 解码为 char序列(字符串)
41 String s = new String(buf,"UTF⑻");解码byte-> String
42 System.out.println(s);
43 }
44 }
6 字符流IO(Reader Writer) 例子: import java.io.BufferedReader;
import java.io.InputStreamReader;
6
7 class TestReaderDemo {
8 Scanner BufferedReader都是流的功能扩大,是过滤器
不能单独使用,终究需要依赖于基本byte流(in)
Scanner 提供了nextLine()方法Java5以后
BufferedReader 提供了 readLine()方法,读取1行
readLine()读取到文件末尾返回null
逐行读取文本文件,显示到系统控制台
工作中经常使用
18 String file = "in.txt"; 为当前工作区workspace/项目名/in.txt
19 BufferedReader in = new BufferedReader(
new InputStreamReader(
21 22 new FileInputStream(file)),"gbk"));
23 String str;
while((str = in.readLine()) != null){
25 System.out.println(str);
26 }
27 in.close();
28 }
import java.io.PrintWriter;
import java.util.Scanner;
class SyncWriteDemo {
8 Scanner in = new Scanner(System.in);
9 String file = "sync.txt";
10 PrintWriter out = new PrintWriter(file,"UTF⑻");
while(true){
12 String str = in.nextLine();
13 out.println(str);
14 if("q".equalsIgnoreCase(str)){
15 break;
16 }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |