IO流(2):
IO流分类图:
1.Reader:
所有字符输入流的父类
(1) InputSreamReader字节字符输入流
(2) BufferedReader:读取效率更高
(1)InputSreamReader:
*1.字节字符输入流
简单应用:
package com.mage.reader.read;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
/**
*
* @author 大萌
*
*/
public class Test01 {
public static void main(String[] args) throws IOException {
//创建对象
Reader reader = new InputStreamReader(new FileInputStream("a.txt"));
//读取
int num = reader.read();
//分析结果
System.out.println(num);
//关闭
reader.close();
}
}
(2) BufferedReader:
*1.相比于InputSreamReader读取效率更高
简单应用:
package com.mage.Reader;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* bufferedreader 读取效率更高
* @author 大萌
*
*/
public abstract class Test02 {
public static void main(String[] args) throws IOException {
//创建对象
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
//读取数据
String str=null;
StringBuilder s=new StringBuilder();
while((str=reader.readLine())!=null){
s.append(str);
s.append("\r\n");
}
System.out.println(s.toString());
reader.close();
}
}
2.Writer:
所有字符输出流的父类
(1) OutputStreamWriter:字符输出流
(2) BufferedWriter:效率更高
(1)OutputStreamWriter:
*1.字符输出流
简单应用:
package com.mage.write;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
/**
* Writer: 是所有字符输出流的父类
* OutputStreamWriter:字符输出流
* @author 大萌
*
*/
public class Test01 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建流对象
OutputStream outp=new FileOutputStream("a.txt");
Writer out=new OutputStreamWriter(outp);
//声明数据
char ch='你';
//输出数据
out.write(ch);
//刷新
out.flush();
//关闭
out.close();
}
}
(2)BufferedWriter:
*1.比OutputStreamWriter效率更高
简单应用:
package com.mage.write;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
/**
* bufferedwriter 效率更高
* @author 大萌
*
*/
public class Test02 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
OutputStream outp = new FileOutputStream("a.txt");
Writer out=new OutputStreamWriter(outp);
BufferedWriter buf=new BufferedWriter(out);
//写出数据
buf.write("开心就好");
buf.newLine();
buf.write("快乐就好");
buf.flush();
buf.close();
out.close();
outp.close();
}
}
综合运用:
package com.mage.Reader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
/**
*
* @author 大萌
*
*/
public class Test01 {
static String filePath = "C:\\Users\\大萌\\Desktop\\qq.txt";
public static void main(String[] args) {
write("黑发不知勤学早,白首方悔读书迟");
read();
}
public static void read() {
//1:创建对象
Reader out = null;
try {
out = new InputStreamReader(new FileInputStream(filePath),"GBK");
char[] chs = new char[1024];
int count = 0;
StringBuilder sb = new StringBuilder();
while((count=out.read(chs))!=-1) {
sb.append(chs,0, count);
}
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(out!=null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void write(String msg) {
Writer out = null;
try {
out = new OutputStreamWriter(new FileOutputStream(filePath),"GBK");
out.write(msg);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(out!=null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.Scanner:
System 对于InputStream的支持:
System.in: 标准输入流: 标准输入设备是键盘 指定了数据源
System.out: 标准输出力: 标准输出设备是控制台 指定输出的位置
System.err: 标准错误输出流
简单应用:
package com.mage.scanner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
/**
*
* @author 大萌
*
*/
public class Test01 {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
System.setIn(new FileInputStream("a.txt"));
Scanner input=new Scanner(System.in);
System.out.println(input.next());
System.setOut(new PrintStream(new File("meng.txt")));
System.out.println("开心就好");
input.close();
input.next();
}
}
4.ObjectOutputStream:
*1.写出的是对象
*2.在读取数据时要按照写入的类型依次读取。
简单应用:
(1)写入数据
package com.mage.inputstrean.object;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
*
* @author 大萌
*
*/
public class Test01 {
public static void main(String[] args) throws FileNotFoundException, IOException {
//创建对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.txt"));
oos.writeInt(3443);
oos.writeDouble(89.9);
oos.writeUTF("你好");
//关闭
oos.close();
}
}
(2)读数据:
package com.mage.outputstrean.object;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
/**
*
* @author 大萌
*
*/
public class Test01 {
public static void main(String[] args) throws FileNotFoundException, IOException {
//创建对象
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
//读取数据
int num = ois.readInt();
System.out.println(num);
//读取数据
double d = ois.readDouble();
System.out.println(d);
//读取数据
String s = ois.readUTF();
System.out.println(s);
//关闭
ois.close();
}
}
应用1:
1.将一个对象写出到文件中去,在将这个对象读取进来
ObjectOutputStream 对象输出流写出对象是要保证当前对象可以序列化
transient 的作用就是序列化的时候,不会将当前属性对应的值序列化出去
序列化的时候记得增加序列化版本号
(1)实体类:
package com.mage.object;
import java.io.Serializable;
/**
*
* @author 大萌
*
*/
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int sex;
private transient int age;
public User() {
// TODO Auto-generated constructor stub
}
public User(String name, int sex, int age) {
super();
this.name = name;
this.sex=sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
(2)测试类:
package com.mage.object;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
*
*
* @author 大萌
*
*/
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
read();
}
public static void write() throws IOException {
//创建对象
User u = new User("dameng",1,18);
//创建对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("a.a"));
//写出
oos.writeObject(u);
//关闭
oos.close();
}
public static void read() throws IOException, ClassNotFoundException {
//读取数据
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.a"));
Object obj = ois.readObject();
System.out.println(obj);
ois.close();
}
}
应用2:
*1.单例模式
*2.当程序在序列化的时候 会默认调用类中的 readResolve方法,不重写该方法 导致每次返回的是一个当前类的对象的副本,这个副本是一个新对象
package com.mage.object;
import java.io.Serializable;
/**
*
* @author 大萌
*
*/
public class Lazy implements Serializable{
private static Lazy lazy = null;
private Lazy() {
}
public static Lazy getInstance() {
if(lazy==null) {
lazy = new Lazy();
}
return lazy;
}
public Object readResolve() {
return lazy;
}
}
package com.mage.outputstrean.object.test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
*
* @author 大萌
*
*/
public class Test01 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Lazy lazy = Lazy.getInstance();
//创建对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("b.txt"));
//写出
oos.writeObject(lazy);
//读取数据
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("b.txt"));
Object obj = ois.readObject();
System.out.println(obj);
System.out.println(lazy);
}
}