InputStream、OutputStream是对字节流的操作,Reader、Writer是对字符流的操作
Java程序语言使用Unicode来表示字符串和字符
Reader和Writer这两个抽象类主要用来读写字符流
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)//从输入设备上读入一行数据,没有输入数据则为null
{
System.out.println(strLine);
}
br.close();
}
}
字符集的编码:
ASCII(American Standard Code for Information Interchange,美国信息互换标准代码),是基于常用的英文字符的一套电脑编码系统。我们知道英文中经常使用的字符、数字符号被计算机处理时都是以二进制码的形式出现的。这种二进制码的集合就是所谓的ASCII码。每一个ASCII码与一个8位(bit)二进制数对应。其最高位是0,相应的十进制数是0-127。如,数字“0”的编码用十进制数表示就是48。另有128个扩展的ASCII,最高位都是1,由一些指标符和其他符号组成。ASCII是现今最通用的单字节编码系统。
GB2312:GB2312码是中华人民共和国国家汉字信息交换用编码,全称《信息交换用汉字编码字符集—基本集》。主要用于给每一个中文字符制定相应的数字,也就是进行编码。一个中文字符用两个字节的数字来表示,为了和ASCII码有所区别,将中文字符每一个字节的最高位置都用1来表示
GBK:为了对更多的字符进行编码,国家又发布了新的编码系统GBK(GBK的K是“扩展”的汉语拼音第一个字母)。在新的编码系统中,除了完全兼容GB2312外,还对繁体中文、一些不常用的汉字和许多符号进行了编码
ISO-8859-1:是西方国家所使用的字符编码集,是一种单字节的字符集,而英文实际上只用了其中数字小于128的部分
Unicode:这是一种通用的字符集,对所有语言的文字进行了统一编码,对每一个字符都用2个字节来表示,对于英文字符采取前面加"0"字节的策略实现等长兼容。如"a"的ASCII码为0x61,UNICODE就为0x00,0x61
UTF-8:Eight-bit UCS Transformation Format,(UCS,Universal Character Set,通用字符集,UCS是所有其他字符集标准的一个超集)。一个7位的ASCII码值,对应的UTF码是一个字节。如果字符是0x0000,或在0x0080与0x007f之间,对应的UTF码是两个字节,如果字符在0x0800与0xffff之间,对应的UTF码是三个字节
在Java中字符都是用Unicode表示的。我们获取一段Unicode,是解码;将字符或字符串转换为字节就是编码过程
------------------------------------------------------------------------------------------
import java.util.*;
import java.nio.charset.*;
class CharsetTest//解决乱码显示问题
{
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会按ISO-8859-1把得到的字符解码成Unicode,但输入的时候是按中文操作系统的字符集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解码回来
System.out.println(strGBK);
}
}
-----------------------------------------------------------------------------------------------
RandomAccessFile类
RandomAccessFile类同时实现了DataInput和DataOutput接口(所以可以读取和写入),提供了对文件随机存取的功能,利用这个类可以在文件的任何位置读取或写入数据
RandomAccessFile类提供了一个文件指针,用来标志要进行读写操作的下一数据的位置
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);//写入第三个Student信息的时候,指针已经指向末尾下面还要进行读取,所以要设置一下文件指针
for(long i=0;i
{
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不建议使用,中文字符只写入第一个字节无法识别;writeChars(String str)和writeUTF(String str)都可以,但是writeUTF会在一开始记录写入数据长度,所以RandomAccessFile还提供了readUTF()
raf.writeDouble(score);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
}
}
--------------------------------------------------------------------------------------
对象序列化
将对象转换为字节流保存起来,并在日后还原这个对象,这种机制叫做对象序列化(可用于在网络上发送对象)
将一个对象保存到永久存储设备上称为持续性
一个对象要想能够实现序列化,必须实现Serializable接口(标识接口)或Externalizable接口
当一个对象被序列化时,只保存对象的非静态成员变量,不能保存任何的成员方法和静态的成员变量
如果一个对象的成员变量是一个对象,那么这个对象的数据成员也会被保存
如果一个可序列化的对象包含对某个不可序列化的对象的引用,那么整个序列化操作将会失败,并且会抛出一个NotSerializableException。我们可以将这个引用标记为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实现了DataOutput,所以它提供了往输出流当中写入Java基本数据类型的功能
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,所以要做类型转换,否则报错;当反序列化的时候,并不会调用该对象任何的构造函数,只是根据先前所保存的信息在内存中重新还原对象
System.out.println(e.name+":"+e.age+":"+e.salary);
}
ois.close();
}
}
class Employee implements Serializable
{
String name;
int age;
double salary;//若不像让某个变量被序列化保存起来,就可以也将其声明为transient
transient Thread t=new Thread();//Thread本身不可序列化,标记为transient后就不会再参与到序列化中
public Employee(String name,int age,double salary)
{
this.age=age;
this.name=name;
this.salary=salary;
}
//写如下两个方法,可以在序列化的时候调用自己写的方法,这里两个方法虽声明为private,但是可以在类外调用,是个特例
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:字节流的输入和输出。从这两个类派生出了一些类:FileInputStream、BufferedInputStream、DataInputStream、PipedInputStream、ObjectInputStream;FileOutputStream、BufferedOutputStream、DataOutputStream、PipedOutputStream、ObjectOutputStream
Reader和Writer:字符流的输入和输出。从这两个类派生出了一些类:BufferedReader、InputStreamReader;BufferedWriter、OutputStreamWriter
流的链接(Java I/O库的设计原则)