### IO流Java IO 流主要是指通过 Java 程序完成输入、输出的功能。所谓的输入是指将文件以数据流的形式读取到 Java 程序中,所谓的输出是指通过 Java 程序将数据流写入到文件中。### File类IO 流可以实现 Java 程序对文件的读写操作,首先需要掌握的是 Java 如何来操作文件,通过 java.io.File 类来创建文件对象,从而完成相关操作。File 类常用方法:public File(String pathname) 根据路径创建文件对象public String getName() 获取文件名public String getParent() 获取文件所在的目录public File getParentFile() 获取文件所在目录对应的 File 对象public String getPath() 获取文件路径public boolean exists() 判断文件是否存在,true 表示存在,false 表示不存在public boolean isDirectory() 判断对象是否为目录public boolean isFile() 判断对象是否为文件public long length() 获取文件大小public boolean createNewFile() 根据当前对象创建新文件public boolean delete() 删除当前对象public boolean mkdir() 根据当前对象创建新目录public boolean renameTo(File dest) 为已存在的对象重命名```javaimport java.io.File;import java.io.IOException;public class Test { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); boolean flag = file.exists(); if(flag) { System.out.println("文件存在"); String fileName = file.getName(); System.out.println("文件名:"+fileName); long length = file.length(); System.out.println("文件大小:"+length); String path = file.getPath(); System.out.println("文件路径:"+path); String parent = file.getParent(); System.out.println("文件所在路径:"+parent); File parentFile = file.getParentFile(); boolean flag2 = parentFile.isDirectory(); if(flag2) { System.out.println("parentFile是路径"); }else { System.out.println("parentFile是文件"); } boolean flag3 = parentFile.isFile(); if(flag3) { System.out.println("parentFile是文件"); }else { System.out.println("parentFile不是文件"); } }else { System.out.println("文件不存在"); } File file2 = new File("/Users/southwind/Desktop/test2.txt"); try { System.out.println(file2.createNewFile()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } File file3 = new File("/Users/southwind/Desktop/test3.txt"); System.out.println(file2.renameTo(file3)); System.out.println(file2.delete()); }}```### 字节流IO 流的 I 指 input,表示输入,O 指 output,表示输出。流就是一组有序的数据序列,以先进先出的方式发送数据的通道,Java 中的流有很多种不同的分类。- 按照方向分,可以分为输入流和输出流。- 按照单位分,可以分为字节流和字符流,字节流指每次处理数据是以字节为单位,字符流是指每次处理数据是以字符为单位。- 按照功能分,可以分为节点流和处理流。字节流按照方向又可以分为输入字节流(InputStream)和输出字节流(OutputStream),InputSteam 是 java.io 包中的顶层父类,并且是一个抽象类。```javapublic abstract class InputStream implements Closeable```InputStream 常用方法public abstract int read() 以字节为单位读取数据public int read(byte b[]) 将数据存入 byte 类型的数组中,并且返回数据长度public byte[] readAllBytes() 将数据存入 byte 类型的数组中返回public int read(byte b[],int off,int len) 将数据存入 byte 类型的数组的指定区间中,并返回数据长度public int available() 返回当前数据流中未读取的数据个数public void close() 关闭当前输入流对象```javaimport java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class Test2 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); try { InputStream inputStream = new FileInputStream(file); int temp = 0; try { System.out.println("当前未读取的数据个数"+inputStream.available()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("开始读取"); while((temp = inputStream.read())!=-1) { System.out.println(temp); System.out.println("当前未读取的数据个数"+inputStream.available()); } inputStream.close(); System.out.println("**********************************"); inputStream = new FileInputStream(file); byte[] bytes = new byte[6]; int length = inputStream.read(bytes); System.out.println("数据长度"+length); System.out.println("遍历byte数组"); for(byte b:bytes) { System.out.println(b); } inputStream.close(); System.out.println("*********************************"); inputStream = new FileInputStream(file); bytes = new byte[10]; length = inputStream.read(bytes, 2, 3); System.out.println("数据长度"+length); System.out.println("遍历byte数组"); for(byte b:bytes) { System.out.println(b); } inputStream.close(); System.out.println("**********************************"); inputStream = new FileInputStream(file); bytes = inputStream.readAllBytes(); System.out.println("遍历 byte 数组"); for(byte b:bytes) { System.out.println(b); } inputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}```OutputStream 和 InputStream 类似,也是一个抽象父类,常用方法如下:public abstract write(int b) 以字节为单位写数据public void write(byte b[]) 将 byte 类型数组中的数据写出public void write(byte b[],int off,int len) 将 byte 类型数组中指定区间的数据写出public void flush() 可以强制将缓冲区的数据同步到输出流中public void close() 关闭数据流```javaimport java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class Test3 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test2.txt"); try { OutputStream outputStream = new FileOutputStream(file); try { byte[] bytes = {105,111,99}; outputStream.write(bytes,1,2); outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}```### 字符流字符流按照流向可以分为输出字符流(Writer)和输入字符流(Reader),Reader 是一个抽象类,常用的方法如下:public int read() 以字符为单位读取数据public int read(char ch[]) 将数据读入 char 类型数组,并返回数据长度public abstract int read(char ch[],int off,int len) 将数据读入 char 类型数组的指定区间,并返回数据长度public abstract void close() 关闭数据流public long transferTo(Writer out) 将数据直接读入字符输出流```javaimport java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class Test4 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); try { Reader reader = new FileReader(file); char[] chars = new char[8]; int length = reader.read(chars); System.out.println("数据长度"+length); System.out.println("遍历数组"); for(char c:chars) { System.out.println(c); } reader.close(); System.out.println("************************"); reader = new FileReader(file); chars = new char[8]; length = reader.read(chars, 2, 6); System.out.println("数据长度"+length); System.out.println("遍历数组"); for(char c:chars) { System.out.println(c); } reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}```输出字符流 Writer,常用方法如下:public void write(int c) 以字符为单位向外写数据public void write(char cha[]) 将 char 数组中的数据写出public abstract void write(char cha[],int off,int len) 将 char 数组中指定区间的数据写出public void write(String str) 将 String 数据写出public void write(String str,int off,int len) 将 String 类型指定区间的数据写出public abstract void flush() 强制将缓冲区的数据同步到输出流中public abstract void close() 关闭数据流
Test.java
import java.io.File;import java.io.IOException;public class Test { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); boolean flag = file.exists(); if(flag) { System.out.println("文件存在"); String fileName = file.getName(); System.out.println("文件名:"+fileName); long length = file.length(); System.out.println("文件大小:"+length); String path = file.getPath(); System.out.println("文件路径:"+path); String parent = file.getParent(); System.out.println("文件所在路径:"+parent); File parentFile = file.getParentFile(); boolean flag2 = parentFile.isDirectory(); if(flag2) { System.out.println("parentFile是路径"); }else { System.out.println("parentFile是文件"); } boolean flag3 = parentFile.isFile(); if(flag3) { System.out.println("parentFile是文件"); }else { System.out.println("parentFile不是文件"); } }else { System.out.println("文件不存在"); } File file2 = new File("/Users/southwind/Desktop/test2.txt"); try { System.out.println(file2.createNewFile()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } File file3 = new File("/Users/southwind/Desktop/test3.txt"); System.out.println(file2.renameTo(file3)); System.out.println(file2.delete()); }}
Test2.java
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class Test2 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); try { InputStream inputStream = new FileInputStream(file); int temp = 0; try { System.out.println("当前未读取的数据个数"+inputStream.available()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("开始读取"); while((temp = inputStream.read())!=-1) { System.out.println(temp); System.out.println("当前未读取的数据个数"+inputStream.available()); } inputStream.close(); System.out.println("**********************************"); inputStream = new FileInputStream(file); byte[] bytes = new byte[6]; int length = inputStream.read(bytes); System.out.println("数据长度"+length); System.out.println("遍历byte数组"); for(byte b:bytes) { System.out.println(b); } inputStream.close(); System.out.println("*********************************"); inputStream = new FileInputStream(file); bytes = new byte[10]; length = inputStream.read(bytes, 2, 3); System.out.println("数据长度"+length); System.out.println("遍历byte数组"); for(byte b:bytes) { System.out.println(b); } inputStream.close(); System.out.println("**********************************"); inputStream = new FileInputStream(file); bytes = inputStream.readAllBytes(); System.out.println("遍历 byte 数组"); for(byte b:bytes) { System.out.println(b); } inputStream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Test3.java
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;public class Test3 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test2.txt"); try { OutputStream outputStream = new FileOutputStream(file); try { byte[] bytes = {105,111,99}; outputStream.write(bytes,1,2); outputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
Test4.java
import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class Test4 { public static void main(String[] args) { File file = new File("/Users/southwind/Desktop/test.txt"); try { Reader reader = new FileReader(file); char[] chars = new char[8]; int length = reader.read(chars); System.out.println("数据长度"+length); System.out.println("遍历数组"); for(char c:chars) { System.out.println(c); } reader.close(); System.out.println("************************"); reader = new FileReader(file); chars = new char[8]; length = reader.read(chars, 2, 6); System.out.println("数据长度"+length); System.out.println("遍历数组"); for(char c:chars) { System.out.println(c); } reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}