IO
- 用于处理设备之间的数据传输
- 输入input:读取外部数据到程序中
- 输出output:将程序数据输出到磁盘,光盘等存储设备中
区分
-
按照数据单位不同分为,字节流(8bit = 1 byte),字符流(16bit = 2byte = 1char)
-
按照数据流向不同分为 输入流 和 输出流
-
按流的角色不同分为,节点流,处理流
抽象基类 字节流 字符流 输入流 InputStream Reader 输出流 OutputStream Writer
节点流(文件流)
- 使用FileInputStream和FileOutputStream复制非文本文件
public class Test1 {
public static void main(String[] args) throws IOException {
long start1 = System.currentTimeMillis();
copyFile("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/图片.jpg","/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/图片_Copy.jpg");
long end1 = System.currentTimeMillis();
System.out.println("复制图片花费的时间为" + (end1 - start1));
}
public static void copyFile(String path1,String path2){
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
// 读入和写出的File对象
File file1 = new File(path1);
File file2 = new File(path2);
// 创建读入流和写入流对象
fileInputStream = new FileInputStream(file1);
fileOutputStream = new FileOutputStream(file2,true);
// 读出数据的操作和写入的操作
int data;
while ((data = fileInputStream.read()) != -1){
fileOutputStream.write(((char)data));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
// 流对象的关闭
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- FileReader
public class Test {
public static void main(String[] args){
FileReader fileReader = null;
try {
// 实例化Fiel类的对象,指明要操作的文件
File file = new File("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello.txt");
// 使用输入字符流
fileReader = new FileReader(file);
// read()返回读入的字符,如到达文件结尾,返回-1
int data;
while ((data = fileReader.read()) != -1){
System.out.print((char) data);
}
}catch (Exception e){
e.printStackTrace();
} finally {
try {
// 关闭流
if ( fileReader != null)
fileReader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
改进版
public class Test1 {
public static void main(String[] args) throws IOException {
// 实例化File类
File file = new File("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello.txt");
// 实例化流
FileReader fr = new FileReader(file);
// 读取文件的具体操作
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
// 注意char在每次去装字符的是否都是使用同一个对象,数据反复覆盖,在取数组中的数据时要注意取0到几索引
System.out.print(new String(cbuf,0,len));
}
// 关闭流对象
fr.close();
}
}
- FileWriter
1.File可以不存在,在写入时会自动创建
2.new FileWriter(file,true);,中参数true表示追加文本,默认为false会删除原文本中的数据
public class Test2 {
public static void main(String[] args) throws IOException {
// 实例化File类(文件可以不存在,在写入操作的时候会创建文件)
File file = new File("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello.txt");
// 实例化FileWriter,参数中append : true表示追加文本
FileWriter fileWriter = new FileWriter(file,true);
// 写出的操作(覆盖了文件原本的内容)
fileWriter.write("hello you me her he\n");
fileWriter.write("hello java python c++\n");
// 关闭资源流
fileWriter.close();
}
}
文件复制
public class Test {
public static void main(String[] args) throws IOException {
// 读入和写出的File对象
File file1 = new File("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello.txt");
File file2 = new File("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello_copy.txt");
// 创建读入流和写入流对象
FileReader fileReader = new FileReader(file1);
FileWriter fileWriter = new FileWriter(file2,true);
// 读出数据的操作和写入的操作
int data;
while ((data = fileReader.read()) != -1){
fileWriter.write(((char)data));
}
// 流对象的关闭
fileWriter.close();
fileReader.close();
}
}
缓冲流(处理流的一种,通常在开发中使用)
- 作用,提高流的读取和写入速度
- BufferedInputStream和BufferedOutputStream
public class Test1 {
public static void main(String[] args) {
long start2 = System.currentTimeMillis();
copyFile("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/视频.avi","/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/视频_Copy_2.avi");
long end2 = System.currentTimeMillis();
System.out.println("复制视频花费的时间为" + (end2 - start2));
}
public static void copyFile(String path1,String path2){
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(new File(path1)));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(path2)));
byte[] data = new byte[1024];
int len;
while ((len = bufferedInputStream.read(data)) != -1){
bufferedOutputStream.write(data,0,len);
bufferedOutputStream.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if (bufferedInputStream != null){
bufferedInputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
try{
if (bufferedOutputStream != null){
bufferedOutputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
- BufferedReader和BufferedsWriter
public class Test2 {
public static void main(String[] args) {
long start2 = System.currentTimeMillis();
copyFile("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello.txt","/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello_cpoy_2.txt");
long end2 = System.currentTimeMillis();
System.out.println("复制文本花费的时间为" + (end2 - start2));
}
public static void copyFile(String path1,String path2){
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader(path1));
bufferedWriter = new BufferedWriter(new FileWriter(path2));
char[] data = new char[1024];
int len;
while ((len = bufferedReader.read(data)) != -1){
bufferedWriter.write(data,0,len);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
if (bufferedReader != null){
bufferedReader.close();
}
}catch (Exception e){
e.printStackTrace();
}
try{
if (bufferedWriter != null){
bufferedWriter.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}
转换流
- InputStreamReader : 将字节输入流转换为字符输入流
public class Test {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
char[] data = new char[1024];
int len;
while ((len = inputStreamReader.read(data)) != -1){
System.out.print(new java.lang.String(data,0,len));
}
inputStreamReader.close();
}
}
- OutputStramWriter : 将字节输出流转换为字符输出流
public class Test1 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day09/hello_change.txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.write("输出转换流");
outputStreamWriter.close();
}
}
标准输入输出流
- System.out 标准的输出流 : 表示从控制台输出
- System.in 标准的输入流 : 表示从键盘输入
public class Test {
public static void main(String[] args) throws IOException {
// System.in 输入字节流 --> (使用转换流)输入字符流 ---> 转换为BuferedReader(需要使用其.readLine()特有方法)
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String data;
while (true){
data = bufferedReader.readLine();
if (data.equalsIgnoreCase("exit") || data.equalsIgnoreCase("e")){
System.out.println("程序结束");
break;
}
System.out.println(data.toUpperCase());
}
}
}
打印流
- PrintStream,其下的重载的方法prinln()和printf()等等
System.out.println("你好");
- PrintWriter
数据流
- 是为了方便操作基本数据类型和String的数据,
- DatainputStream和DataoutputStream