您的位置:首页 > 编程语言 > Java开发

java34: 流

2016-05-30 20:34 411 查看
输入流 读
输出流 写

按照是否直接与特定的地方(磁盘,内存,设备)相连
节点流 低级流
可以从或向一个特定的覅放读写数据

处理流 高级流
处理另外一个流
不能独立存在(构造方法中需要传入另外一个流),
处理另外一个流

简化读写

按照处理的单位的不同分为

字节流
一次读写一个字节

字符流
一次读写一个字符

InputStream 和OutputStream 本身是不能实例化的,他们是抽象类 是所有字节输入和输出流的父类

InputStream

OutputStream

FileOutputStream

低级流 是文件的字节输出流 使用这个流 可以以字节为单位将数据写入文件
构造方法 FileOutputStream(File file)
创建一个向指定File 对象表示的文件中写出数据的文件输出流

FileOutputStream(String filename);

创建一个向具有指定名称的文件中写出数据的文件输入流

指定的文件中已经包含内容, 会将这个文件中原有的数据全部清除,只能写

package se.day06;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* FileOutputStream
* 节点流(低级流)
* 用于向文件中写出字节的流
* @author zongx
*
*/
public class FOSDemo {
public static void main(String[] args) throws IOException{
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write(97);
String str = "我是卖报的小行家";
byte[] buf = str.getBytes("UTF-8");
fos.write(buf);

int i = Integer.MAX_VALUE;
fos.write(i>>>24);
fos.write(i>>>16);
fos.write(i>>>8);
fos.write(i);
fos.close();
}
}
package se.day06;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* FileInputStream
* 低级流
* 用于从文件中读取字节流
* @author zongx
*
*/
public class FISDemo {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("fos.txt");
int i = fis.read();
System.out.println(i);
byte[] buf = new byte[24];
int len = fis.read(buf);
String s = new String(buf,"utf-8");
System.out.println(s);
fis.close();
}
}
package se.day06;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 使用FOS对已经存在的文件的操作
* 会清空文件以前的内容
* @author zongx
*
*/
public class FOSDemo2 {
public static void main(String[] args) throws IOException{
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write(100);
fos.close();
}
}

package se.day06;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 使用FOS对已经存在的文件的操作
* 会清空文件以前的内容
* 使用一个重载的方法可以追加写
* true
* @author zongx
*
*/
public class FOSDemo2 {
public static void main(String[] args) throws IOException{
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write(100);
fos.close();
fos = new FileOutputStream("fos.txt",true);
fos.write(100);
fos.close();
fos = new FileOutputStream("fos.txt",true);
fos.write(100);
fos.close();
fos = new FileOutputStream("fos.txt",true);
fos.write(100);
fos.close();
}
}


缓冲流
(加快读写效率)

高级流
处理流
BufferedOutputStream

BufferedInputStream

bos.flush()

将应给基本类型数据转换为相应的字节
将这些字节有序的写入文件保存

将一个特定的数据结构转换为一组字节的过程 称之为:序列化
基本类型序列化
DataInputStreamDataOutputStream
引用类型序列化(对象序列化)
ObjectInputStreamObjectOutputStream 需要序列化的类 必须实现 Serializable这个接口 把这个类标记成可序列化的

反之就是: 反序列化
将数据写入硬盘做长久保存的过程称之为:数据持久化
用途:传输,保存
DataInputStream
DataOutputStream

ObjectInputStream
ObjectOutputStream
package se.day06;

import java.io.Serializable;
import java.util.List;

public class Person implements Serializable{
/**
* 序列化版本号,要注意
*/
private static final long serialVersionUID = 1L;
private String name;
private int age;
private char sex;
//当某个属性被transient 修饰,那么该属性在序列化过程中被忽略
private transient double salary;
private List<String> list;
public Person(String name,int age,char sex, double salary,List list){
this.name = name;
this.age  = age;
this.sex = sex;
this.salary = salary;
this.list = list;

}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex
+ ", salary=" + salary + ", list=" + list + "]";
}
}
package se.day06;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* 序列化一个对象
* @author
*
*/
public class OOSDemo {
public static void main(String[] args) throws IOException{
List<String> list = new ArrayList<String>();
list.add("家在西安");
list.add("其他信息1");
list.add("信息2");
Person person = new Person("zs",19,'男',2000,list);
FileOutputStream  fos = new FileOutputStream("person.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(person);
System.out.println("OK");
oos.close();

}
}
package se.day06;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
* 高级流
* 可以将一组字节转换为对应的对象
* 用于对象的反序列化
* @author
*
*/
public class OISDemo {
public static void main(String[] args) throws IOException, Exception{
FileInputStream fis = new FileInputStream("person.txt");
ObjectInputStream ois  = new ObjectInputStream(fis);
Person p =(Person)ois.readObject();
System.out.println(p);
ois.close();
}
}


字符流
只用于读写文本数据
Reader 是字符输入流 的父类
Writer 是字符输出流的父类
以字符(char)为单位读写数据,一次处理一个unicode
字符流底层还是基本的字节流

Reader 方法
int read() 读取应给字符,返回的int 值 低16 有效

int read() 读取一个字符数组的length 个字符并存入该数组,返回值为实际读取到的字符量

Writer 方法

void write(int c)

void write(char[] chs)

void write(String str)

void write(char[] chs,int offset,int len)

package se.day06;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

/**
* OutputStreamWriter
* 字符输出流,以字符为为单位写出数据
* 字符流只用于读写字符数据
*
* @author
*
*/
public class OSWDemo {
public static void main(String[] args) throws IOException{
/*
* 向文件中写出文本数据 写字符串
* 	向文件中写数据FileOutputStream
* 	写的是文本数据
*/
FileOutputStream fos = new FileOutputStream("osw.txt");
/*
* OutputStreamWriter 的特点是可以将给定的字符串按照特定的
* 字符集转换为字节后写出
* 使用
* OutputStreamWriter osw  = new OutputStreamWriter(fos,"UTF-8");
* OutputStreamWriter osw  = new OutputStreamWriter(fos);
*/
OutputStreamWriter osw  = new OutputStreamWriter(fos,"UTF-8");
osw.write("大家都去卖报子");
osw.write("dajiadouqumaibaozi");

osw.close();

}
}
package se.day06;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* InputStreamReader
* 读取
* @author
*
*/
public class ISRDemo {
public static void main(String[] args) throws IOException{
FileInputStream fis = new FileInputStream("osw.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
int d = -1;
while((d = isr.read())!= -1){
char c = (char)d;
System.out.print(c);
}
isr.close();
}
}
package se.day06;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* 复制文本文件
* @author
*
*/
public class OSWISRDemo {
public static void main(String[] args) throws IOException{
FileOutputStream fos = new FileOutputStream("osw2.txt");
FileInputStream fis = new FileInputStream("osw.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
InputStreamReader isr = new InputStreamReader(fis);
int d = -1;
while((d =isr.read() )!= -1){
osw.write(d);
}
osw.close();
isr.close();

}
}


本文出自 “浪漫的偷笑” 博客,请务必保留此出处http://lmdtx.blog.51cto.com/6942028/1784595
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: