------- android培训、java培训、期待与您交流! ----------
功能流对象
打印流:也是输出流,不抛出异常
PrintStream(字节流) PrintWriter(字符流)
解决的问题:方便打印各种数据值得表现形式,他的打印方法可以保证数值的表现形式不变,即写的是什么就打印出什么
PrintStream:
可以操作的目的:File对象,字符串路径,字节输出流
PirintStream ps=new PrintStream(“tempfile\\print.txt”);//创建一个printStream对象
pw.print(97);//将数据打印到文件中pw.close();
PrintWriter:
可以操作的目的:File对象,字符串路径,字节输出流,字符输出流
例子:键盘输入,将写入的数字转换成大写
BufferedReader bufr=
new BufferedReader(newInputStreamReader(System.in));//键盘录入
PrintWriter pw=new PrintWriter(System.out);//定义目的
String line=null;//键盘录入,写一行读一行
while((line=bufr.readLine())!=null)
{if(line.equals(“over”)){break;}pw.print(line.toUpperCase())}
pw.flush();
pw.colse();
如果想改变目的为文件
pw=new PrintWriter(new BufferWriter(newFileWriter(“tempfile\\print.txt” )),true);加上true自动刷新
序列流:SequenceInputStream
特点:流对象有序的排列
解决的问题:将多个流合并成一个流,即多个源合并成一个源
功能:特殊之处在构造函数上,一初始化就合并了多个流进来
使用场景:对多个数据进行合并,多个流对应一个目的
//如何获取一个Enumeration呢,使用ArrayList
ArrayList<FileInputStream> list=newArrayList<FileInputStream>();
for(int x=1;x<=3;x++){list.add(newFileInputStream(“tempfile\\”+x+”.txt”))}
//通过ArrayList获取枚举接口,可以使用Collections工具类中的方法
Enumeration<FileInputStream>en=Collections.enumeration(list)
//创建序列流对象,需要传递Enumeration
SequenceInputStream sis=newSequenceInputStream(en);
//创建目的
FileOutputStream fos=new FileOutputStram(“tempfile\\4.txt”);
//频繁的读写数据
byte[] buf=new byte[1024];int len=0;while((len=sis.read(buf))!=-1)
{fos.write(buf,0,len)}sis.close();fos.close()
对象的序列化:
特点:用于操作对象
解决问题:可以将对象序列化和反序列化 注意:对象序列化一定要实现Serializable接口
ObjectOutputStream:对象序列化
ObjectInputStream:对象的反序列化
将一个对象存储到持久化设备上;对象序列化
FileOutputStreamfos=new FileOutPutStream(“tempfile\\obj.txt”);//明确存储文件
ObjectOutputStreamoos=new ObjectOutputStream(fos);//加入写入对象的功能
oos.writerObject(newPerson(“wangcai”,20));//调用方法
oos.close();
要被序列化的对象需要实现Serializable接口,所以person要实现此接口
读对象:对象反序列化
FileInputStreamfis=new FileInputStream(“tempfile\\obj.txt”);//定义流对象关联数据
ObjectInputstreamois=new ObjectInputStream(fis);
Personobj=(person)ois.readObject()
Syso(obj.toString);
随机访问文件:RandomAccessFile
特点:只能操作文件,既能读,又能写,维护一个byte数组,内部定义了字节流的写入和读取,通过对指针的操作,可以实现对文件任意位置的读取好写入
功能:getFilePoint seek用于操作指针的方法
//创建一个随机访问文件的对象,文件不存在,创建,存在,不创建,也不覆盖
RandomAccessFile raf=new RandomAccessFile(“tempfile\\5.txt”,”rw”);
//写入姓名和年龄
raf.write(“zhansan”.getBytes());raf.writeInt(97);
raf.write(“lisi”.getBytes());raf.writeInt(99);writeInt是为了保证整数的原样性
随机写入
raf.seek(8);//设置指针位置,8为角标
raf.write(“wangwu”.getBytes());raf.writeInt(100)
读取文件
RandomAccessFile raf=new RandomAccessFile(“tempfile\\5.txt”,”r”);
Byte[] buf=new byte[4];
raf.read(buf);String name=new String(buf);
int age=raf.readInt();
syso(name+”:”+age);raf.close();
随机读取:只要设置指针的位置即可
raf.seek(8*1);
raf.getFilePoint();//获取指针位置
RandomAccessFile随机读写的文件,最好有一定的规律。
管道流:PipedInputStream 和pipedOutputStream
输入输出可以直接进行连接,通过结合线程使用
需要使用多线程,单线程容易死锁
功能:connect()方法
//创建管道流对象
PipedInputStream pis=new PipedInputStream();
PipedOutputStream pos=new PipedOutputStream();
//将两个流链接
pis.connect(pos);
new Thread(newInput(pis)).start();
new Thread(newOutput(pos)).start();
}
}
//定义输入任务
class Output implementsRunnable{
private PipedOutputStream pos;
public Output(PipedOutputStream pos) {
super();
this.pos = pos;
}
@Override
public void run() {
try {//通过write方法完成
pos.write("hey!管道来了".getBytes());
pos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//定义输出任务
class Input implementsRunnable{
private PipedInputStream pis;
public Input(PipedInputStream pis) {
super();
this.pis = pis;
}
@Override
public void run() {
byte[] buf=new byte[1024];
int len;
try {
len= pis.read(buf);
Stringstr=newString(buf,0,len);
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
}