1、IO流
1.1、流的概念
流对于计算机来说是一个抽象的概念,是对输入输出设备的抽象
在Java程序中,对于数据的输入和输出操作,都是以流的形式
数据以二进制的形式在程序和设备之间流动,这种传输方式称之为输入流、输出流。
流是具有方向的性------输入流和输出流
以Java程序为参照,数据从其他地方(设备)流向程序,这就是输入流,反之就是输出流。
从文件中写入数据----------输出流
从文件中读出数据----------输入流
1.2、流的分类
我们一般按照数据的流向和数据的类型进行划分
数据流向划分
- 输入流:把数据从其他设备上读取到程序中
- 输出流:把数据充程序中写出到其他设备上
数据类型分类
- 字节流:以字节为单位,读写数据的流
- 字符流:以字符为单位,读写数据的流
将两种分类结合:
- 字节输入流,在程序中,以字节的方式,将数据从其他设备读入程序中
- 字节输出流,在程序中,以字节的方式,将输入从程序中写到其他设备
- 字符输入流,在程序中,以字节的方式,将数据从其他设备读入程序中
- 字符输出流,在程序中,以字符的方法,将数据从其他设备写入程序中
字节是byte,字符是char
1.3、流的结构
在Java中几乎所有的流,都是派生自四个抽象类的父类型:
- InputStream--------字节输入流
- OutputStream------字节输出流
- Reader-----------------字符输入流
- Writer-------------------字符输出流
IO流相关的类,主要是在java.io
包下的定义
一个流具备的特点:
- 是输入还是输出
- 是字节还是字符
- 流的目的地
1.4、字节流
概述
对于计算机而言,所有的数据都是二进制的形式存储的,最终都是一个一个的字节,所以使用流,进行数据的传输的时候,也是按照一个一个的字节进行的
java.io.InputStream
是所有字节输入流的抽象父类型
java.io.OutputStream
是所有字节输出流的抽象父类型
一般情况,使用字节流来操作数据的时候,往往是使用一对,一个字节输入流,赋值读取数据,一个字节输出流,负责将数据写出去
使用流操作数据的基本步骤:
- 声明流
- 创建流
- 使用流
- 关闭流
相应练习
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Arrays;
import java.util.Scanner;
import org.junit.Test;
public class DataFlow {
@Test
public void test1() {
//这里是一次读取用一个字节,然后把字节值输出
System.out.println("请输入单个字符");
//声明并创建流
InputStream in = System.in;
OutputStream out = System.out;
//使用流
int data=-1;
try {
data = in.read();
out.write(data);
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(in!=null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test2() {
System.out.println("请输入多个数据");
//声明流
InputStream in = null;
OutputStream out = null;
//创建流
in = System.in;
out = System.out;
//使用流
int leng = -1;
byte[] buf = new byte[2048];
try {
while((leng = in.read(buf))!=97) {
out.write(buf,0,leng);
out.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭流
if(in!=null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void test3() {
//声明流
InputStream in = null;
OutputStream out = null;
//创建流
Scanner scanner =new Scanner(System.in);
String str = scanner.nextLine();
byte[] arr = str.getBytes();
in = new ByteArrayInputStream(arr);
out = new ByteArrayOutputStream();
//使用流
int len = -1;
byte[] buf = new byte[1024];
try {
len = in.read(buf);
out.write(buf);
out.flush();
byte[] toByteArray = ((ByteArrayOutputStream) out).toByteArray();
System.out.println(Arrays.toString(toByteArray));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//4.关闭流
if(in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test4() {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
try {
in.connect(out);
WriteThread t1 = new WriteThread(out);
ReadThread t2 = new ReadThread(in);
t1.start();
t2.start();
t1.join();
t2.join();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println();
System.out.println("程序运行结束!");
}
@Test
public void test5() {
String pathname = "D:\\briup-java\\corejava-day19\\note\\a\\b\\c";
File file = new File(pathname);
if(!file.exists()){
file.mkdirs();
}
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
System.out.println(file.getName());
System.out.println(file.length());
String[] arr = new File("D:\\briup-java\\corejava-day19").list();
File[] arrs = new File("D:\\briup-java\\corejava-day19").listFiles();
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arrs));
}
}
class WriteThread extends Thread{
private OutputStream out;
public WriteThread(OutputStream out){
this.out = out;
}
@Override
public void run() {
byte[] arr = "石原里美".getBytes();
try {
for(int i=0;i<arr.length;i++){
out.write(arr[i]);
out.flush();
Thread.sleep(100);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
if(out!=null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class ReadThread extends Thread{
private InputStream in;
public ReadThread(InputStream in){
this.in = in;
}
@Override
public void run() {
int data = -1;
try {
while((data=in.read())!=-1){
// System.out.println((char)data);
System.out.write(data);
// System.out.println();
System.out.flush();
}
System.out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
字节流
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Scanner;
import org.junit.Test;
public class CharactersFlow {
@SuppressWarnings({ "resource", "unused" })
@Test
public void test1() throws IOException {
String str = new Scanner(System.in).nextLine();
char[] arr = str.toCharArray();
Reader in = new CharArrayReader(arr);
Writer out = new CharArrayWriter();
int len =-1;
char[] buf = new char[3];
try {
len = in.read(buf);
out.write(buf);
out.flush();
System.out.println(Arrays.toString(((CharArrayWriter)out).toCharArray()));
} catch (Exception e) {
}finally {
if(in!=null) {
in.close();
}
if(out!=null) {
out.close();
}
}
}
@Test
public void test2() {
PipedReader in =new PipedReader();
PipedWriter out = new PipedWriter();
try {
in.connect(out);
ReadtThread readThread = new ReadtThread(in);
WritersThread writerThread = new WritersThread(out);
readThread.start();
writerThread.start();
readThread.join();
writerThread.join();
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("\n程序运行结束");
}
@Test
public void test3() throws IOException {
File file1 = new File("D:\\briup-java\\corejava-day19\\note\\day20\\背影.txt");
File file2 = new File("D:\\briup-java\\corejava-day19\\note\\day20\\拷贝.txt");
Reader in = new FileReader(file1);
Writer out = new FileWriter(file2);
Writer o = new CharArrayWriter();
try {
int len = -1;
char[] buf = new char[1024];
while((len = in.read(buf))!=-1) {
o.write(buf);
o.flush();
char[] toCharArray = ((CharArrayWriter) o).toCharArray();
System.out.println(Arrays.toString(toCharArray));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(in != null) {
in.close();
}
if(o != null) {
in.close();
}
}
}
}
class WritersThread extends Thread{
private PipedWriter out;
public WritersThread(PipedWriter out) {
this.out = out;
}
@Override
public void run() {
char[] arrse = "石原里美".toCharArray();
try {
for(int i = 0;i<arrse.length;i++) {
out.write(arrse[i]);
out.flush();
Thread.sleep(100);
}
} catch (Exception e) {
// TODO: handle exception
}finally {
if(out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
class ReadtThread extends Thread{
private PipedReader in;
public ReadtThread(PipedReader in) {
this.in = in;
}
@Override
public void run() {
int data = -1;
try {
while((data = in.read())!=-1) {
System.out.println((char)data);
// System.out.write(data);
System.out.flush();
}
System.out.flush();
} catch (Exception e) {
// TODO: handle exception
}finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
字符流