资源描述
*************************
*******第19天 IO流*******
*************************
**********************
*******基本内容*******
**********************
字符流: Reader
常用子类:InputStreamReader->(继承)FileReader,BufferedReader Writer
常用子类:OutputStreamWriter->(继承)FileWriter,BufferedWriter
字节流: OutputStream
常用子类:FileOutputStream,DataOutputStream,ObjectOutputStream,
BufferedOutputStream,ByteArrayOutputStream
InputStream
常用子类:FileInputStream,DataInputStream,ObjectOutputStream,
BufferedInputStream,ByteArrayInputStream
打印流: PrintStream
常用子对象:System.out,System.in,System.err
==============================================================================================
********************************
字节流:OutputStream,InputStream
********************************
一、字节流: FileInputStream,FileInputStream
//文件读写
import java.io.*;
public class Text1 {
public static void main(String[] args) {
FileInputStream fis=null;
FileInputStream fos=null;
try {
fis=new FileInputStream("e:\\temp\\from.zip");
fos=new FileOutputStream("e:\\temp\\to.zip");
byte [] buffer=new byte[1024];
while (true) {
int temp = fis.read(buffer,0,buffer.length);
if(temp==-1){
break;
}
fos.write(buffer,0,temp);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {//捕获io流关闭异常
fis.close();
fos.close();
} catch (Exception e2) {
}
}
}
}
二、字节缓冲流:BufferedInputStream,BufferedOutputStream--------------------------------------
//文件读写
public class Text2 {
public static void main(String[] args) {
String fileName="e:\\老罗视频Android01-09.zip";
String toName="e:\\temp\\老罗视频Android01-09.zip";
FileInputStream fis=null;
FileOutputStream fos=null;
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try {
fis=new FileInputStream(fileName);
fos=new FileOutputStream(toName);
bis=new BufferedInputStream(fis);
bos=new BufferedOutputStream(fos);
long time1=System.currentTimeMillis();//打印时间
while (true) {
byte[] buffer=new byte[1024];
int temp=bis.read(buffer,0,buffer.length);
if (temp==-1) {
break;
}
bos.write(buffer,0,temp);
}
long time2=System.currentTimeMillis();
System.out.println((time2-time1)/60000);//求时间差
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、数据流: DataInputStream,DataOutputStream
public class Text3 {
public static void main(String[] args) throws IOException {
int x=8;
char ch='a';
double f=5.6;
DataOutputStream dos=new DataOutputStream(new FileOutputStream("e:\\temp\\dis.txt"));
DataInputStream dis=new DataInputStream(new FileInputStream("e:\\temp\\dis.txt"));
dos.writeInt(x);
dos.writeChar(ch);
dos.writeDouble(f);
dos.close();
int getx=dis.readInt();//8
char getch=dis.readChar();//'a'
double getf=dis.readDouble();//5.6
}
}
四、对象数据流:ObjectInputStream,ObjectDataOutputStream--------------------------------------
//写入对象数据
public class Task4 {
public static void main(String[] args) throws Exception {
ObjectOutputStream obs=new ObjectOutputStream(new FileOutputStream("e:\\temp\\obs.txt"));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("e:\\temp\\obs.txt"));
obs.writeObject(new Student("杨过",20,99));
obs.writeObject(new Student("小龙女",16,77));
obs.writeObject(new Student("小小龙女",11,77));
obs.writeObject(new Student("小小小龙女",7,77));
while(true){
try {
Student stu=(Student)ois.readObject();
System.out.println(stu);
} catch (Exception e) {
break;
}
}
}
}
class Student implements Serializable{//实现序列化接口/*Externalizable*/
String name;
int age;
double score;
transient String id;//不希望属性被序列化,不会在写入文件时保存
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
}
/*class Student1 implements Externalizable{//自定义序列化
String name;
int age;
double score;
transient String id;//不希望属性被序列化,不会在写入文件时保存
public Student1() {
}
public Student1(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score + "]";
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("反序列化");
this.name=(String)in.readObject();
this.age=in.readInt();
this.score=in.readDouble();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
System.out.println("序列化");
out.writeObject(name);
out.writeInt(age);
out.writeDouble(score);
}
}*/
五、内存流 ByteArrayOutputStream,ByteArrayInputStream-----------------------------------------
public class Text5 {
public static void main(String[] args) throws IOException {
ByteArrayInputStream bis=new ByteArrayInputStream("6217001820008037164".getBytes());
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int m=0;
while(true){
int ch=bis.read();//bis.read(byte[],0,len)
if(ch==-1){
break;
}
/*int retch;
if (ch<='z'&&ch>='a') {
retch=Character.toUpperCase((char)ch);
}else{
retch=ch;
}*/
bos.write(ch);//bis.write(byte[],0,len)
m++;
if (m==4) {
bos.write(" ".getBytes());
m=0;
}
}
System.out.println(new String(bos.toByteArray()));
}
}
==============================================================================================
********************************
字符流:Reader,Writer
********************************
一、字符流: FileReader FileWriter:--------------------------------------------------------
//文本文件复制
public class CopyFile {
public static void main(String[] args) {
File from=new File("e:\\temp\\from2.txt");
File to=new File("e:\\temp\\to3.txt");
FileReader fr=null;
FileWriter fw=null;
try {
fr=new FileReader(from);
fw=new FileWriter(to);
char[] buffer=new char[100];
while (true) {
int ret=fr.read(buffer,0,buffer.length);
if (ret==-1) {
break;
}
fw.write(buffer,0,buffer.length);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fr.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
二、字符缓冲流:BufferedReader,BufferedWriter-------------------------------------------------
//文本文件读写
public class Text2 {
public static void main(String[] args)
String fileName="e:\\temp\\from.txt";
String toName="e:\\temp\\to11.txt";
FileReader fr=null;
FileWriter fw=null;
BufferedReader br=null;
BufferedWriter bw=null;
try {
fr=new FileReader(fileName);
br=new BufferedReader(fr);
fw=new FileWriter(toName);
bw=new BufferedWriter(fw);
while(true){
String line=br.readLine();
if (line==null) {
break;
}
System.out.println(line);
bw.write(line,0,line.length());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
br.close();
bw.flush();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、字符转换流:InputStreamReader OutputStreamWriter------------------------------------------
//文件读写
public class Text3 {
public static void main(String[] args) throws IOException {
String srcfilename = "D:\\徐莎_备课\\java_day18\\安排.txt";
/* readLine();---BufferedReader对象的方法
* 构建BufferedReader对象需要Readerd对象参数
* 由于FileReader使用的默认字符集进行解码,不能用FileReader
* 可以用FileInputStream
* 考虑如何把FileInputStream转换成Reader的对象
* 可以使用转换流:InputstreamReader
* InputStreamReader(InputStream in, String charsetName)
*/
FileInputStream fis = new FileInputStream(srcfilename);//原始流
InputStreamReader isr = new InputStreamReader(fis, "gb2312");//把字节流转换成字符流
BufferedReader br = new BufferedReader(isr);//创建装饰流对象
String line = br.readLine();
System.out.println(line);
line = br.readLine();
System.out.println(line);
br.close();
}
}
==============================================================================================
********************************
打印流: PrintStream
********************************
public class PrintstreamDemo {
public static void main(String[] args) throws IOException {
int x = 5;
char ch = 'a';
double f = 2.2;
/*PrintStream pout = new PrintStream("print.txt");
pout.print(x);
pout.print(ch);
pout.print(f);*/
OutputStream fos = new FileOutputStream("print2.txt");
PrintWriter pout = new PrintWriter(fos);
// PrintWriter pout = new PrintWriter("print.txt");
pout.print(x);
pout.print(ch);
pout.print(f);
pout.flush();
System.out.println("hello!");//输出到文件:
System.out.println(x);
System.out.println(f);
}
}
//标准输出重定向
PrintStream out = new PrintStream("setout.txt");
System.setOut(out);
//标准键盘输入
static String input() throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
return br.readLine();
}
*************************
********The end**********
*************************
展开阅读全文