import java.io.*;
class StreamTest
{
public static void main(String[] args) throws Exception
{
/*從屏幕輸入,輸入什么回車(chē)后會(huì )直接在屏幕上輸出,ctrl+C退出
int data;
while((data=System.in.read())!=-1)
{
System.out.write(data);
}
*/
/*往文件中寫(xiě)入
FileOutputStream fos=new FileOutputStream("1.txt");//FilterOutputStream(OutputStream out):Creates an output stream filter built on top of the specified underlying output stream
fos.write("
http://lilac0610.spaces.msn.com".getBytes());//FileOutputStream的write方法只接受byte和int型 void write(byte[] b):Writes b.length bytes to this output stream;byte[] getBytes():Encodes this String into a sequence of bytes using the platform‘s default charset, storing the result into a new byte array.
fos.close();//void close():Closes this output stream and releases any system resources associated with the stream.
*/
/*文件輸入流讀取數據,輸出到屏幕上
FileInputStream fis=new FileInputStream("1.txt");//protected FilterInputStream(InputStream in):Creates a FilterInputStream by assigning the argument in to the field this.in so as to remember it for later use.
byte[] buf=new byte[100];
int len=fis.read(buf);//int read(byte[] b):Reads up to byte.length bytes of data from this input stream into an array of bytes.
System.out.println(new String(buf,0,len));//void println(String x):Print a String and then terminate the line;String(byte[] bytes, int offset, int length):Constructs a new String by decoding the specified subarray of bytes using the platform‘s default charset.
fis.close();// void close():Closes this input stream and releases any system resources associated with the stream.
*/
/*BufferedOutputStream緩沖輸出流,需要使用已經(jīng)存在的節點(diǎn)流來(lái)構造(從 FilterOutputStream繼承)
FileOutputStream fos=new FileOutputStream("1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);//BufferedOutputStream(OutputStream out):Creates a new buffered output stream to write data to the specified underlying output stream with a default 512-byte buffer size.
bos.write("
http://lilac0610.spaces.msn.com".getBytes());//數據寫(xiě)入緩沖區
bos.flush();//void flush():Flushes this buffered output stream.將緩沖區中數據寫(xiě)入文件
//bos.close();//也可將緩沖區中數據寫(xiě)入文件,但此時(shí)就不能再繼續寫(xiě)入數據了
*/
/*BufferedInputStream緩沖輸入流
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
byte[] buf=new byte[100];
int len=bis.read(buf);
System.out.println(new String(buf,0,len));//void println(String x):Print a String and then terminate the line;String(byte[] bytes, int offset, int length):Constructs a new String by decoding the specified subarray of bytes using the platform‘s default charset.
bis.close();
*/
/*DataOutputStream提供了讀寫(xiě)Java中的基本數據類(lèi)型的功能
FileOutputStream fos=new FileOutputStream("1.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
DataOutputStream dos=new DataOutputStream(bos);//此時(shí)相當于把三個(gè)流鏈接了起來(lái),把bos鏈接到了fos,又把dos鏈接到了bos,此時(shí)帶緩沖并可寫(xiě)入基本數據類(lèi)型
byte b=3;
int i=78;
char ch=‘a(chǎn)‘;
float f=4.5f;
dos.writeByte(b);
dos.writeInt(i);
dos.writeChar(ch);
dos.writeFloat(f);
dos.close();
*/
/*DataInputStream
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
DataInputStream dis=new DataInputStream(bis);
System.out.println(dis.readByte());//讀取的順序要與寫(xiě)入的一致
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readFloat());
dis.close();
*/
}
---------------------------------------------------------------------------------------
import java.io.*;
class PipedStreamTest
{
public static void main(String[] args)
{
PipedOutputStream pos=new PipedOutputStream();
PipedInputStream pis=new PipedInputStream();
try
{
pos.connect(pis);
new Producer(pos).start();
new Consumer(pis).start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Producer extends Thread
{
private PipedOutputStream pos;
public Producer(PipedOutputStream pos)
{
this.pos=pos;
}
public void run()
{
try
{
pos.write("Hello".getBytes());
pos.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class Consumer extends Thread
{
private PipedInputStream pis;
public Consumer(PipedInputStream pis)
{
this.pis=pis;
}
public void run()
{
try
{
byte[] buf=new byte[100];
int len=pis.read(buf);
System.out.println(new String(buf,0,len));
pis.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Java I/O庫的設計原則
Java的I/O庫提供了一個(gè)稱(chēng)作鏈接的機制,可以將一個(gè)流與另一個(gè)流首尾相接,形成一個(gè)流管道的鏈接。這種機制實(shí)際上是一種被稱(chēng)為Decorator(裝飾)設計模式的應用
通過(guò)流的鏈接,可以動(dòng)態(tài)的增加流的功能,而這種功能的增加是通過(guò)組合一些流的基本功能而動(dòng)態(tài)獲取的
我們要獲取一個(gè)I/O對象,往往需要產(chǎn)生多個(gè)I/O對象(例如InputStream鏈:FileInputStream從文件中獲取輸入字節—BufferedInputStream增加了緩沖的功能—DataInputStream增加了讀取Java基本數據類(lèi)型的功能;OutputStream鏈:DataOutputStream可以往輸出流中寫(xiě)入Java基本數據類(lèi)型—BufferedInputStream提供數據寫(xiě)入到緩沖區的功能—FileInputStream將數據寫(xiě)入到文件中),這也是Java I/O庫不太容易掌握的原因,但在I/O庫中Decorator模式的運用,給我們提供了實(shí)現上的靈活性
InputStream、OutputStream是對字節流的操作,Reader、Writer是對字符流的操作
Java程序語(yǔ)言使用Unicode來(lái)表示字符串和字符
Reader和Writer這兩個(gè)抽象類(lèi)主要用來(lái)讀寫(xiě)字符流
import java.io.*;
class StreamTest
{
public static void main(String[] args) throws Exception
{
/*
FileOutputStream fos=new FileOutputStream("1.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
bw.write("
http://lilac.spaces.msn.cn");
bw.close();
FileInputStream fis=new FileInputStream("1.txt");
InputStreamReader isr= new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
System.out.println(br.readLine());
br.close();
*/
InputStreamReader isr= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
String strLine;
while((strLine=br.readLine())!=null)//從輸入設備上讀入一行數據,沒(méi)有輸入數據則為null
{
System.out.println(strLine);
}
br.close();
}
}
字符集的編碼:
ASCII(American Standard Code for Information Interchange,美國信息互換標準代碼),是基于常用的英文字符的一套電腦編碼系統。我們知道英文中經(jīng)常使用的字符、數字符號被計算機處理時(shí)都是以二進(jìn)制碼的形式出現的。這種二進(jìn)制碼的集合就是所謂的ASCII碼。每一個(gè)ASCII碼與一個(gè)8位(bit)二進(jìn)制數對應。其最高位是0,相應的十進(jìn)制數是0-127。如,數字“0”的編碼用十進(jìn)制數表示就是48。另有128個(gè)擴展的ASCII,最高位都是1,由一些指標符和其他符號組成。ASCII是現今最通用的單字節編碼系統。
GB2312:GB2312碼是中華人民共和國國家漢字信息交換用編碼,全稱(chēng)《信息交換用漢字編碼字符集—基本集》。主要用于給每一個(gè)中文字符制定相應的數字,也就是進(jìn)行編碼。一個(gè)中文字符用兩個(gè)字節的數字來(lái)表示,為了和ASCII碼有所區別,將中文字符每一個(gè)字節的最高位置都用1來(lái)表示
GBK:為了對更多的字符進(jìn)行編碼,國家又發(fā)布了新的編碼系統GBK(GBK的K是“擴展”的漢語(yǔ)拼音第一個(gè)字母)。在新的編碼系統中,除了完全兼容GB2312外,還對繁體中文、一些不常用的漢字和許多符號進(jìn)行了編碼
ISO-8859-1:是西方國家所使用的字符編碼集,是一種單字節的字符集,而英文實(shí)際上只用了其中數字小于128的部分
Unicode:這是一種通用的字符集,對所有語(yǔ)言的文字進(jìn)行了統一編碼,對每一個(gè)字符都用2個(gè)字節來(lái)表示,對于英文字符采取前面加"0"字節的策略實(shí)現等長(cháng)兼容。如"a"的ASCII碼為0x61,UNICODE就為0x00,0x61
UTF-8:Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS是所有其他字符集標準的一個(gè)超集)。一個(gè)7位的ASCII碼值,對應的UTF碼是一個(gè)字節。如果字符是0x0000,或在0x0080與0x007f之間,對應的UTF碼是兩個(gè)字節,如果字符在0x0800與0xffff之間,對應的UTF碼是三個(gè)字節
在Java中字符都是用Unicode表示的。我們獲取一段Unicode,是解碼;將字符或字符串轉換為字節就是編碼過(guò)程
------------------------------------------------------------------------------------------
import java.util.*;
import java.nio.charset.*;
class CharsetTest//解決亂碼顯示問(wèn)題
{
public static void main(String[] args) throws Exception
{
/*輸出JVM所支持的字符集
Map m=Charset.availableCharsets();
Set names=m.keySet();
Iterator it=names.iterator();//迭代器
while(it.hasNext())
{
System.out.println(it.next());
}
*/
Properties pps=System.getProperties();//static Properties getProperties():Determines the current system properties.
pps.list(System.out);
pps.put("file.encodeing","ISO-8859-1");
int data;
byte[] buf=new byte[100];
int i=0;
while((data=System.in.read())!=‘q‘)
{
buf[i]=(byte)data;
i++;
}
String str=new String(buf,0,i);//Java會(huì )按ISO-8859-1把得到的字符解碼成Unicode,但輸入的時(shí)候是按中文操作系統的字符集GBK碼讀入的 String(byte[] bytes, int offset, int length):Constructs a new String by decoding the specified subarray of bytes using the platform‘s default charset.
System.out.println(str);
String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");//把內容按ISO-8859-1編碼回去,再按GBK解碼回來(lái)
System.out.println(strGBK);
}
}
-----------------------------------------------------------------------------------------------
RandomAccessFile類(lèi)
RandomAccessFile類(lèi)同時(shí)實(shí)現了DataInput和DataOutput接口(所以可以讀取和寫(xiě)入),提供了對文件隨機存取的功能,利用這個(gè)類(lèi)可以在文件的任何位置讀取或寫(xiě)入數據
RandomAccessFile類(lèi)提供了一個(gè)文件指針,用來(lái)標志要進(jìn)行讀寫(xiě)操作的下一數據的位置
import java.io.*;
class RandomFileTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"zs",98.5);
Student s2=new Student(2,"ls",99.5);
Student s3=new Student(3,"ww",95.5);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);//寫(xiě)入第三個(gè)Student信息的時(shí)候,指針已經(jīng)指向末尾下面還要進(jìn)行讀取,所以要設置一下文件指針
for(long i=0;i<raf.length();i=raf.getFilePointer())//long length():Returns the length of this file;long getFilePointer():Returns the current offset in this file.
{
s.readStudent(raf);
System.out.println(s.num+":"+s.name+":"+s.score);
}
raf.close();
}
}
class Student
{
int num;
String name;
double score;
public Student()
{}
public Student(int num,String name,double score)
{
this.num=num;
this.name=name;
this.score=score;
}
public void writeStudent(RandomAccessFile raf) throws IOException
{
raf.writeInt(num);
raf.writeUTF(name);//writeBytes(String str):……by discarding its high eight bits不建議使用,中文字符只寫(xiě)入第一個(gè)字節無(wú)法識別;writeChars(String str)和writeUTF(String str)都可以,但是writeUTF會(huì )在一開(kāi)始記錄寫(xiě)入數據長(cháng)度,所以RandomAccessFile還提供了readUTF()
raf.writeDouble(score);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
}
}
--------------------------------------------------------------------------------------
對象序列化
將對象轉換為字節流保存起來(lái),并在日后還原這個(gè)對象,這種機制叫做對象序列化(可用于在網(wǎng)絡(luò )上發(fā)送對象)
將一個(gè)對象保存到永久存儲設備上稱(chēng)為持續性
一個(gè)對象要想能夠實(shí)現序列化,必須實(shí)現Serializable接口(標識接口)或Externalizable接口
當一個(gè)對象被序列化時(shí),只保存對象的非靜態(tài)成員變量,不能保存任何的成員方法和靜態(tài)的成員變量
如果一個(gè)對象的成員變量是一個(gè)對象,那么這個(gè)對象的數據成員也會(huì )被保存
如果一個(gè)可序列化的對象包含對某個(gè)不可序列化的對象的引用,那么整個(gè)序列化操作將會(huì )失敗,并且會(huì )拋出一個(gè)NotSerializableException。我們可以將這個(gè)引用標記為transient,那么對象仍然可以序列化
import java.io.*;
class ObjectSerialTest
{
public static void main(String[] args) throws Exception
{
Employee e1=new Employee("zs",25,3000.50);
Employee e2=new Employee("ls",24,3200.40);
Employee e3=new Employee("ww",27,3800.55);
FileOutputStream fos=new FileOutputStream("employee.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);//ObjectOutputStream實(shí)現了DataOutput,所以它提供了往輸出流當中寫(xiě)入Java基本數據類(lèi)型的功能
oos.writeObject(e1);//writeObject()完成對象序列化
oos.writeObject(e2);
oos.writeObject(e3);
oos.close();
FileInputStream fis=new FileInputStream("employee.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Employee e;
for(int i=0;i<3;i++)
{
e=(Employee)ois.readObject();//Object readObject()返回的是Object,所以要做類(lèi)型轉換,否則報錯;當反序列化的時(shí)候,并不會(huì )調用該對象任何的構造函數,只是根據先前所保存的信息在內存中重新還原對象
System.out.println(e.name+":"+e.age+":"+e.salary);
}
ois.close();
}
}
class Employee implements Serializable
{
String name;
int age;
double salary;//若不像讓某個(gè)變量被序列化保存起來(lái),就可以也將其聲明為transient
transient Thread t=new Thread();//Thread本身不可序列化,標記為transient后就不會(huì )再參與到序列化中
public Employee(String name,int age,double salary)
{
this.age=age;
this.name=name;
this.salary=salary;
}
//寫(xiě)如下兩個(gè)方法,可以在序列化的時(shí)候調用自己寫(xiě)的方法,這里兩個(gè)方法雖聲明為private,但是可以在類(lèi)外調用,是個(gè)特例
private void writeObject(java.io.ObjectOutputStream oos) throws IOException
{
oos.writeInt(age);
oos.writeUTF(name);
System.out.println("Write Object");
}
private void readObject(java.io.ObjectInputStream ois) throws IOException, ClassNotFoundException
{
age=ois.readInt();
name=ois.readUTF();
System.out.println("Read Object");
}
}
總結:
InputStream和OutputStream:字節流的輸入和輸出。從這兩個(gè)類(lèi)派生出了一些類(lèi):FileInputStream、BufferedInputStream、DataInputStream、PipedInputStream、ObjectInputStream;FileOutputStream、BufferedOutputStream、DataOutputStream、PipedOutputStream、ObjectOutputStream
Reader和Writer:字符流的輸入和輸出。從這兩個(gè)類(lèi)派生出了一些類(lèi):BufferedReader、InputStreamReader;BufferedWriter、OutputStreamWriter
流的鏈接(Java I/O庫的設計原則)