标签:style class blog code java http
IO中的继承图表
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。 字节流和字符流的区别:
结论:只要是处理纯文本数据,就优先考虑使用字符流。 除此之外都使用字节流。
对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。
1.输入字节流InputStreamIO 中输入字节流的继承图可见上图,可以看出:
IO 中输出字节流的继承图可见上图,可以看出:
图中蓝色的为主要的对应部分,红色的部分就是不对应部分。紫色的虚线部分代表这些流一般要搭配使用。从上面的图中可以看出Java IO 中的字节流是极其对称的。“存在及合理”我们看看这些字节流中不太对称的几个类吧!
1. java.io.PushbackInputStream拥有一个PushBack缓冲区,从PushbackInputStream读出数据后,只要PushBack缓冲区没有满,就可以使用unread()将数据推回流的前端。
2. 假设一个文本文件中同时存储有ASCII码范围的英文字符与BIG5范围的中文字符。想要判断那些位置是ASCII而哪些位置是BIG5中文字符,BIG5中文字符使用两个字节来表示一个中文字,而ASCII只使用一个字节来表示英文字符。
3. Big5中文为了与ASCII兼容,低字节范围内0xA4-0xF9,而高字节为0x40--0x7E以及0xA1--0xFE。存储时低字节先存,再存高字节,所以读取时只要先读到字节是在0xA4--0xF9,就表示它可能是一个中文字符的前半数据。
4. 下面的范例说明PushbackInputStream的功能,一次从文件中读取两个字节,并检查两个字节合并后的整数值是否在0xA440--0xFFFF之间,这样可以简单地判断其两个字节合并后是否为BIG码。如果是BIG5码则使用这两个字节产生String实例以显示汉字字符;如果不在这个范围之内,则可能是个ASCII范围内的字符,您可以显示第一个字节的字符表示,并将第二个字节推回流,以待下一次可以重新读取。
package ysu.hxy;
import java.io.*;
public class PushbackInputStreamDemo
{
public static void main(String[] args)
{
try
{
PushbackInputStream pushbackInputStream =
new PushbackInputStream(new FileInputStream(args[0]));
byte[] array = new byte[2];
int tmp = 0;
int count = 0;
while((count = pushbackInputStream.read(array))!=-1)
{
//两个字节转换为整数
tmp = (short)((array[0] << 8) | (array[1] & 0xff));
tmp = tmp & 0xFFFF;
//判断是否为BIG5,如果是则显示BIG5中文字
if(tmp >= 0xA440 && tmp < 0xFFFF)
{
System.out.println("BIG5:" + new String(array));
}
else
{
//将第二个字节推回流
pushbackInputStream.unread(array,1,1);
//显示ASCII范围的字符
System.out.println("ASCII: " + (char)array[0]);
}
}
pushbackInputStream.close();
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("请指定文件名称");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}在上面的继承关系图中可以看出:
在上面的关系图中可以看出:
转换流的特点:
何时使用转换流?
具体的对象体现:
这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。
File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录中的文件列表,创建、删除文件和目录等方法。
该对象并不是流体系中的一员,其封装了字节流,同时还封装了一个缓冲区(字符数组),通过内部的指针来操作字符数组中的数据。 该对象特点:
注意:该对象在实例化时,如果要操作的文件不存在,会自动创建;如果文件存在,写数据未指定位置,会从头开始写,即覆盖原有的内容。 可以用于多线程下载或多个线程同时写数据到文件。
============================================================================================import java.io.*;
class StreamTest
{
public static void main(String[] args) throws Exception
{
/*int data;
while((data=System.in.read())!=-1)
{
System.out.write(data);
}*/
/*FileOutputStream fos=new FileOutputStream("1.txt");
//fos.write("http://www.mybole.com.cn".getBytes());
//fos.close();
BufferedOutputStream bos=new BufferedOutputStream(fos);
//bos.write("http://www.mybole.com.cn".getBytes());
//bos.flush();
//bos.close();
DataOutputStream dos=new DataOutputStream(bos);
byte b=3;
int i=78;
char ch='a';
float f=4.5f;
dos.writeByte(b);
dos.writeInt(i);
dos.writeChar(ch);
dos.writeFloat(f);
dos.close();
FileInputStream fis=new FileInputStream("1.txt");
BufferedInputStream bis=new BufferedInputStream(fis);*/
/*byte[] buf=new byte[100];
//int len=fis.read(buf);
int len=bis.read(buf);
System.out.println(new String(buf,0,len));
//fis.close();
bis.close();*/
/*DataInputStream dis=new DataInputStream(bis);
System.out.println(dis.readByte());
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readFloat());
dis.close();*/
/*FileOutputStream fos=new FileOutputStream("1.txt");
OutputStreamWriter osw=new OutputStreamWriter(fos);
BufferedWriter bw=new BufferedWriter(osw);
bw.write("http://www.mybole.com.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)
{
System.out.println(strLine);
}
br.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,welcome you!".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();
}
}
}import java.io.*;
class ObjectSerialTest
{
public static void main(String[] args) throws Exception
{
Employee e1=new Employee("zhangsan",25,3000.50);
Employee e2=new Employee("lisi",24,3200.40);
Employee e3=new Employee("wangwu",27,3800.55);
FileOutputStream fos=new FileOutputStream("employee.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(e1);
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();
System.out.println(e.name+":"+e.age+":"+e.salary);
}
ois.close();
}
}
class Employee implements Serializable
{
String name;
int age;
double salary;
transient Thread t=new Thread();
public Employee(String name,int age,double salary)
{
this.name=name;
this.age=age;
this.salary=salary;
}
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
{
age=ois.readInt();
name=ois.readUTF();
System.out.println("Read Object");
}
}import java.io.*;
class RandomFileTest
{
public static void main(String[] args) throws Exception
{
Student s1=new Student(1,"zhangsan",98.5);
Student s2=new Student(2,"lisi",96.5);
Student s3=new Student(3,"wangwu",78.5);
RandomAccessFile raf=new RandomAccessFile("student.txt","rw");
s1.writeStudent(raf);
s2.writeStudent(raf);
s3.writeStudent(raf);
Student s=new Student();
raf.seek(0);
for(long i=0;i<raf.length();i=raf.getFilePointer())
{
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);
raf.writeDouble(score);
}
public void readStudent(RandomAccessFile raf) throws IOException
{
num=raf.readInt();
name=raf.readUTF();
score=raf.readDouble();
}
}import java.io.*;
class FileTest
{
public static void main(String[] args) throws Exception
{
//File f=new File("1.txt");
//f.createNewFile();
//f.mkdir();
//File f=new File("E:\\JavaLesson\\Lesson7\\1.txt");
//f.createNewFile();
/*File fDir=new File(File.separator);
String strFile="JavaLesson"+File.separator+"Lesson7"+
File.separator+"1.txt";
File f=new File(fDir,strFile);
f.createNewFile();
//f.delete();
f.deleteOnExit();
Thread.sleep(3000);*/
/*for(int i=0;i<5;i++)
{
File f=File.createTempFile("winsun",".tmp");
f.deleteOnExit();
}
Thread.sleep(3000);*/
File fDir=new File(File.separator);
String strFile="JavaLesson"+File.separator+"Lesson6";
File f=new File(fDir,strFile);
String[] names=f.list(new FilenameFilter()
{
public boolean accept(File dir,String name)
{
return name.indexOf(".java")!=-1;
}
});
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}import java.util.*;
import java.nio.charset.*;
class CharsetTest
{
public static void main(String[] args) throws Exception
{
/*Map m=Charset.availableCharsets();
Set names=m.keySet();
Iterator it=names.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}*/
Properties pps=System.getProperties();
//pps.list(System.out);
pps.put("file.encoding","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);
//System.out.println(str);
String strGBK=new String(str.getBytes("ISO-8859-1"),"GBK");
System.out.println(strGBK);
}
}标签:style class blog code java http
原文地址:http://blog.csdn.net/yyxhhx/article/details/30737893