Java IO流,
分享于 点击 28227 次 点评:123
Java IO流,
说明
- IO流在java中很重要,文件的上传下载,网络的数据传输都需要利用到IO流
- 分别从IO流的类别以及IO流的用法进行说明,后面的例子带有zhus注释,可以更好的理解IO流
一、IO流思维导图
二、例子目录结构
1.字节流
JDK提供的源码
- InputStreamAndOutputStream.java
/**
* @Title: InputStream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月25日
* @version V1.0
*/
package com.lut.wy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @ClassName: InputStream
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
public class InputStreamAndOutputStream {
public static void main(String[] args) {
InputStream();
//OutputStream();
}
public static void InputStream() {
InputStream in =null;
//从控制台获取用户输入的数据
in = System.in;
try {
//调用read()方法,来获取接收到的数据的int类型值
//此方法会造成线程阻塞
int read = 0;
while(read != -1) {
read=in.read();
System.out.println(read);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(in != null) {
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void OutputStream() {
OutputStream out =null;
try {
out = System.out;
byte [] byt = new byte[10];
byt[0]=97;//a
byt[1]=98;//b
byt[2]=99;//c
byt[3]=100;//d
byt[4]=101;//e
byt[5]=102;//f
out.write(byt,0,3);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- ByteArrayInputAndOutputStream.java
/**
* @Title: ByteArrayInputAndOutputStream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月25日
* @version V1.0
*/
package com.lut.wy;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @ClassName: ByteArrayInputAndOutputStream
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
public class ByteArrayInputAndOutputStream {
public static void main(String[] args) {
InputStream in =null;
OutputStream out = null;
String str = "hello wy";
try {
in = new ByteArrayInputStream(str.getBytes());//将字串变成byte数组,供ByteArrayInputStream输入
int len =-1;
//将in读取的字节数组进行输出。
/*while((len = in.read()) != -1) {
System.out.write(len);//标准输出流输出
System.out.flush();//刷新流中的缓存
}*/
//使用字节数组输出流输出字节数组输入流的内容
out = new ByteArrayOutputStream();
byte [] byt = new byte[10];
while((len = in.read(byt)) != -1) {
out.write(byt, 0, len);
out.flush();
}
System.out.println("使用字节数组输出流得到的:"+out.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
if(in != null) {
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- FileInputAndFileOutputSream.java
/**
* @Title: FileInputAndFileOutputSream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月25日
* @version V1.0
*/
package com.lut.wy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @ClassName: FileInputAndFileOutputSream
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
public class FileInputAndFileOutputSream {
public static void main(String[] args) {
//多态声明,使用父类接受子类
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(new File("src/com/lut/wy/a.txt"));//创建文件输入流
//指定一次从文件读取多少个字节存入该数组
byte[] byt = new byte[1024];
int len = -1;
/*while((len = is.read(byt)) != -1) {
System.out.println(new String(byt,0,len));//将字节数组的有效有效长度转换为字串输出
}*/
os = new FileOutputStream(new File("src/com/lut/wy/b.txt"));//向b文件中写入字节
//将文件输入流中的内容通过文件字节输出流输出到另一个文件中
while((len = is.read(byt)) != -1) {
os.write(byt, 0, len);
os.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- PipedInputAndPipedOutputStream.java
/**
* @Title: PipedInputAndPipedOutputStream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月25日
* @version V1.0
*/
package com.lut.wy;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
* @ClassName: PipedInputAndPipedOutputStream
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
public class PipedInputAndPipedOutputStream {
public static void main(String[] args) {
//这是两个管道流,使用两个线程,一个读一个写,产生10位随机密码
PipedInputStream pis =null;
PipedOutputStream pos =null;
ReadThread read =null;
WriteThread write =null;
try {
pis = new PipedInputStream();
pos = new PipedOutputStream(pis);
read= new ReadThread(pis, "read");
write=new WriteThread(pos, "write");
read.start();
write.start();
} catch (Exception e) {
// TODO: handle exception
}
}
}
/**
* 读取线程
* @ClassName: ReadThread
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
class ReadThread extends Thread{
InputStream in =null;
/**
* Instance ReadThread.
*
*/
public ReadThread(PipedInputStream pis,String name) {
super(name);
this.in=pis;
}
/* (!Javadoc)
*
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
try {
int len =-1;
while((len=in.read()) != -1) {
System.out.write(len);;
System.out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(in != null) {
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* 写线程
* @ClassName: WriteThread
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
class WriteThread extends Thread{
OutputStream out =null;
/**
* Instance WriteThread.
*
*/
public WriteThread(PipedOutputStream pos,String name) {
super(name);
this.out=pos;
}
/* (!Javadoc)
*
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890.!@#$%^&*()";
int len=-1;
try {
for(int i=0;i<10;i++){
int num =(int) (Math.random()*str.length()+1);
len=str.charAt(num);
out.write(len);
out.flush();
sleep(1000);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(out != null) {
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- BufferedInputAndBufferedOutputStream.java
/**
* @Title: BufferedInputAndOutputStream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月25日
* @version V1.0
*/
package com.lut.wy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @ClassName: BufferedInputAndOutputStream
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
public class BufferedInputAndBufferedOutputStream {
public static void main(String[] args) {
//buffed包装流,提供多种方法
InputStream is =null;
OutputStream os =null;
BufferedInputStream bis =null;
BufferedOutputStream bos =null;
try {
is = new FileInputStream("src/com/lut/wy/a.txt");
os = new FileOutputStream("src/com/lut/wy/b.txt");
bis = new BufferedInputStream(is);
bos = new BufferedOutputStream(os);
int len = -1;
/*while((len = bis.read()) != -1) {
System.out.write(len);
System.out.flush();
}*/
while((len = bis.read()) != -1) {
bos.write(len);
bos.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- DataInputAndDataOutputStream.java
/**
* @Title: DataInputAndDataOutputStream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月25日
* @version V1.0
*/
package com.lut.wy;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @ClassName: DataInputAndDataOutputStream
* @Description: TODO
* @author Licon
* @date 2018年8月25日
*
*/
public class DataInputAndDataOutputStream {
//可以读和写各种数据类型,常用的为UTF
public static void main(String[] args) {
InputStream is =null;
OutputStream os =null;
DataInputStream dis =null;
DataOutputStream dos =null;
try {
is = new FileInputStream("src/com/lut/wy/a.txt");
os = new FileOutputStream("src/com/lut/wy/b.txt");
dis = new DataInputStream(is);
dis.read();
dos = new DataOutputStream(os);
dos.writeUTF("666");
dos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(dis != null) {
dis.close();
}
if(is != null) {
is.close();
}
if(dos != null) {
dos.close();
}
if(os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- CharArrayReaderAndCharArrayWriter.java
/**
* @Title: ReaderAndWriter.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* @ClassName: ReaderAndWriter
* @Description: TODO
* @author Licon
* @date 2018年8月29日
*
*/
public class CharArrayReaderAndCharArrayWriter {
public static void main(String[] args) {
Reader r = null;
Writer w = null;
char [] ch = new char[10];
ch[0]=97;
ch[1]=98;
ch[2]=99;
ch[3]=100;
ch[4]=101;
r = new CharArrayReader(ch);
w = new CharArrayWriter();
try {
//使用标准输出流打印读取的内容
/*int len = -1;
while((len = r.read()) != -1) {
System.out.write(len);
System.out.flush();
}*/
//使用字符数组写入流写出读取的字符数组
int len = -1;
while((len = r.read()) != -1) {
w.write(len);
w.flush();
}
System.out.println(w.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(r != null) {
r.close();
}
if(w != null) {
w.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- FileReaderAndFileWriter.java
/**
* @Title: FileReaderAndFileWriter.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* @ClassName: FileReaderAndFileWriter
* @Description: TODO
* @author Licon
* @date 2018年8月29日
*
*/
public class FileReaderAndFileWriter {
public static void main(String[] args) {
Reader r = null;
Writer w = null;
try {
r = new FileReader(new File("src/com/lut/wy/a.txt"));
w = new FileWriter(new File("src/com/lut/wy/c.txt"));
char []ch = new char[20];
int len = -1;
//一个字符一个字符读取并存入ch字符数组,使用字符写入流写入c.txt文件
while((len = r.read(ch)) != -1) {
w.write(ch, 0, len);
w.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(r != null) {
r.close();
}
if(w != null) {
w.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- BufferedReaderAndBufferedWriter.java
/**
* @Title: BufferedReaderAndBufferedWriter.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* @ClassName: BufferedReaderAndBufferedWriter
* @Description: TODO
* @author Licon
* @date 2018年8月29日
*
*/
public class BufferedReaderAndBufferedWriter {
public static void main(String[] args) {
Reader r = null;
Writer w = null;
BufferedReader br = null;
BufferedWriter bw = null;
try {
r = new FileReader(new File("src/com/lut/wy/a.txt"));
w = new FileWriter(new File("src/com/lut/wy/d.txt"));
br = new BufferedReader(r);
bw = new BufferedWriter(w);
//包装后的字符流可以读取一行
/*String str = null;
while((str = br.readLine()) != null) {
System.out.println(str);
}*/
//包装后的字符流可以写入一行
//将读取的字符流一行一行写入d.txt
String str = null;
while((str = br.readLine()) != null) {
bw.write(str);
bw.flush();
bw.newLine();//不加这句,会在d.txt中将读取的字串连接在一起
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(bw != null) {
bw.close();
}
if(br != null) {
br.close();
}
if(w != null) {
w.close();
}
if(r != null) {
r.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- InputStreamReaderAndOutputStreamWriter.java
/**
* @Title: InputStreamReaderAndOutputStreamWriter.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
/**
* @ClassName: InputStreamReaderAndOutputStreamWriter
* @Description: TODO
* @author Licon
* @date 2018年8月29日
*
*/
public class InputStreamReaderAndOutputStreamWriter {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
Reader r = null;
Writer w = null;
try {
is = new FileInputStream(new File("src/com/lut/wy/a.txt"));
os = new FileOutputStream(new File("src/com/lut/wy/e.txt"));
r = new InputStreamReader(is, "utf-8");//利用字节流转换字符流将其转换,并设置字符编码
w = new OutputStreamWriter(os);
/*
* 后面就可以根据操作字符流一样操作字节流,还可以对字符流进行包装
*/
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(r != null) {
r.close();
}
if(w != null) {
w.close();
}
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- Student.java ObjectInputStreamAndObjectOutputStream.java
/**
* @Title: Student.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.Serializable;
/**
* @ClassName: Student 一个存取数据的bean
* @Description: TODO
* @author Licon
* @date 2018年8月29日
*
*/
public class Student implements Serializable{
/**
* @Fields field:field:{todo} 序列化id
*/
private static final long serialVersionUID = -1501551742453125001L;
private int id;
private String name;
private char gender;
private int age;
public Student(int id, String name, char gender, int age) {
super();
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", gender=" + gender + ", age=" + age + "]";
}
}
/**
* @Title: ObjectInputStreamAndObjectOutputStream.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/**
* @ClassName: ObjectInputStreamAndObjectOutputStream
* @Description: TODO 用于序列户,可将一个对象写入文件,也可也反序列化,将一个对象从文件中读出
* @author Licon
* @date 2018年8月29日
*
*/
public class ObjectInputStreamAndObjectOutputStream {
public static void main(String[] args) {
InputStream is =null;
OutputStream os =null;
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
try {
is = new FileInputStream(new File("src/com/lut/wy/f.txt"));
//写入与读取不能同时开启
//os = new FileOutputStream(new File("src/com/lut/wy/f.txt"));
ois = new ObjectInputStream(is);
//oos = new ObjectOutputStream(os);
//Student stu = new Student(1,"wyy",'f',22);
//oos.writeObject(stu);
//oos.flush();
System.out.println( ois.readObject());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(oos != null) {
oos.close();
}
if(ois != null) {
ois.close();
}
if(is != null) {
is.close();
}
if(os != null) {
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
- PrintWriterTest.java
/**
* @Title: PrintWriter.java
* @Package com.lut.wy
* @Description: TODO
* @author Licon
* @date 2018年8月29日
* @version V1.0
*/
package com.lut.wy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* @ClassName: PrintWriter
* @Description: TODO
* @author Licon
* @date 2018年8月29日
*
*/
public class PrintWriterTest {
public static void main(String[] args) {
InputStream is =null;
PrintWriter pw = null;
InputStreamReader isr =null;
BufferedReader br = null;
try {
is = new FileInputStream(new File("src/com/lut/wy/a.txt"));
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
pw = new PrintWriter(new File("src/com/lut/wy/g.txt"));
String str=null;
while((str = br.readLine()) != null){
pw.println(str);
}
pw.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(pw != null) {
pw.close();
}
if(br != null) {
br.close();
}
if(isr != null) {
isr.close();
}
if(is != null) {
is.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
相关文章
- 暂无相关文章
用户点评