注意:java中只要类名“以Stream结尾的都是字节流,以Reader/Writer结尾的都是字符流,着重对Stream进行理解学习”
输出流:OutputStream以及Writer还拥有一个共同特性,继承了flushable接口,所有的输出流,都是可刷新的,都具有flush()方法,在用完输出流时,一定要进行刷新,表示将流管道中剩余未输出的数据强行输出完,刷新的所用就是清空管道。
注意:如果没有flush()可能会造成数据丢失。
(4)java.io包下需要掌握的16个流(其实有很多,这16个常用)。分别是
文件专属流(4个):
java.io.FileInputStream
java.io.FileOutputStream
java.io.FileReader
java.io.FileWriter
华丽分割线---------------------------------------------------------------------
转换流(将字节流转换为字符流)(2个):
java.io.InputStreamReader
java.io.OutputStreamWriter
华丽分割线---------------------------------------------------------------------
缓冲流专属(4个):
java.io.BufferedReader
java.io…BufferedWriter
java.io…BufferedInputStream
java.io…BufferedOutputStream
华丽分割线---------------------------------------------------------------------
数据流专属(2个):
java.io.DataInputStream
java.io.DataOutputStream
华丽分割线---------------------------------------------------------------------
标准输出流(2个):
java.io.PrintWriter
java.io.PrintStream
华丽分割线---------------------------------------------------------------------
对象专属流(2个):
java.io.ObjectInputStream
java.io.ObjectOutputStream
华丽分割线---------------------------------------------------------------------
(1)FileInputStream
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamTest {
public static void main(String[] args) {
FileInputStream fis=null;
try {
fis=new FileInputStream("ArmyTest/src/Test.java");//读取的文件名
byte[] bytes=new byte[4];
int readCount=0;
while((readCount=fis.read(bytes))!=-1){
System.out.print(new String(bytes,0,readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(2)FileOutputStream
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamTest {
public static void main(String[] args) {
FileOutputStream fos=null;
try {
/*fos=new FileOutputStream("myFile");*/
fos=new FileOutputStream("myFile",true);
//开始写
//myFile文件不存在时会先创建,再写入
//已存在该文件,会先清空再写入,慎用
//以追加的方式再文件末尾写入
byte[] bytes={97,98,99,100};
fos.write(bytes);
String str="我是中国人";
byte[] bs=str.getBytes();
fos.write(bs);
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(3)FileReader
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader=null;
try {
reader=new FileReader("myFile");
char[] chars=new char[4];
int readCount=0;
while((readCount=reader.read(chars))!=-1){
System.out.println(new String(chars,0,readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(4)FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
FileWriter fileWriter=null;
try {
//创建一个文件字符输出流
fileWriter=new FileWriter("myFile",true);
char[] chars={'我','是','中','国','人'};
fileWriter.write(chars);
fileWriter.write("\n");
fileWriter.write("我很骄傲");
//刷新
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileWriter!=null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(5)BufferedReader
import java.io.*;
public class BufferedReaderTest {
public static void main(String[] args) {
BufferedReader bf=null;
try {
bf=new BufferedReader(new InputStreamReader(new FileInputStream("File")));//运用java.io.InputStreamReader将字节流转换成字符流
String s=null;
while((s=bf.readLine())!=null){
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bf!=null){
try {
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(6)BufferedWriter
import java.io.*;
public class BufferedWriterTest {
public static void main(String[] args) {
BufferedWriter out=null;
try {
/* out=new BufferedWriter(new FileWriter("copy01"));
out.write("你好,世界");
out.write("\n");
out.write("Hello World");
out.flush();*/
out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("copy01",true)));
out.write("哈哈");
out.write("\n");
out.write("呵呵");
out.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(7)实现Copy功能
第一种方式:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyTest01 {
public static void main(String[] args) {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
//创建一个输入流对象
fis=new FileInputStream("C:\\Users\\Lenovo\\Music\\录音\\最好的那年.m4a");
//创建一个输出流对象
fos=new FileOutputStream("E:\\SogouDownload\\最好的那年.m4a",true);
//实现一边读一边写
byte[] bytes=new byte[1024*1024];
int readCount=0;
while((readCount=fis.read(bytes))!=-1){
fos.write(bytes,0,readCount);
}
//最后输出流要刷新
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//输入流关闭
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//输出流关闭
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
第二种方式:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
使用FileReader和FileWriter进行Copy
*/
public class CopyTest01 {
public static void main(String[] args){
FileReader in=null;
FileWriter out=null;
try {
in=new FileReader("IO/src/CopyTest01.java");
out=new FileWriter("Copy01.java");
char[] chars=new char[1024*1024];
int readCount=0;
while((readCount=in.read(chars))!=-1){
out.write(chars,0,readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
(8)实现目录拷贝
import java.io.*;
public class CopyAll {
public static void main(String[] args) {
File srcFile=new File("D:\\MySQL\\mysql-8.0.21-winx64\\bin");
File destFile=new File("E:\\");
copyDir(srcFile,destFile);
}
private static void copyDir(File srcFile, File destFile) {
if(srcFile.isFile()){
//如果srcFile是一个文件的话,递归结束
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream(srcFile);
String path=(destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\\")+srcFile.getAbsolutePath().substring(3);
out=new FileOutputStream(path);
byte[] bytes=new byte[1024*1024];
int readCount=0;
while((readCount=in.read(bytes))!=-1){
out.write(bytes,0,readCount);
}
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
//获取源目录下的子目录
File[] files=srcFile.listFiles();
for(File file:files){
//System.out.println(file.getAbsolutePath());
if(file.isDirectory()){
//System.out.println(file.getAbsolutePath());
String srcDir=file.getAbsolutePath();
//System.out.println(srcDir.substring(3));
String destDir=(destFile.getAbsolutePath().endsWith("\\")?destFile.getAbsolutePath():destFile.getAbsolutePath()+"\\")+srcDir.substring(3);
//System.out.println(destDir);
File newFile=new File(destDir);
if(!newFile.exists()){
newFile.mkdirs();
}
}
copyDir(file,destFile);
}
}
}
(9)PrintStream
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class PrintStreamTest01 {
public static void main(String[] args) {
System.out.println("Hello World");
PrintStream ps=System.out;
ps.println("Hello zhangsan");
ps.println("Hello Lisi");
try {
//标准输出流不再指向控制台,指向outData文件
PrintStream pm=new PrintStream(new FileOutputStream("outData"));
//修改输出方向,将输出方向修改到“outData”文件中
System.setOut(pm);
pm.println("你好,世界");
pm.println("加油,奥利给");
pm.println("加油");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
转载 : Java怎么理解IO流?详解_古藤老人的博客-CSDN博客_如何理解io