IO流:数据在内存跟硬盘中传输的过程就是IO流。
输入流 | 输出流 | |
字节流 | 字节输入流InputStream | 字节输出流OutputStream |
字符流 | 字符输入流Reader | 字符输出流Writer |
字节流:能处理任何资源,一次读取一个文字
(下面是通过具体的案例来学习)
/**
* 1.将字符串Hello Io写到c:/a.txt
* O流 程序(内存)——>文件
* 字节流
* outputStream
* FileOutputStream
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//1、创建文件字节输出流对象
FileOutputStream fileOutputStream = new FileOutputStream("D:\\io\\a.txt");
//2、写数据
fileOutputStream.write("Hello Io".getBytes()); //字符串转byte数组
}
/**
* 2.将c:/a.txt复制到c:/aaa/a.txt(文件复制)
* IO流 文件——>文件
* 字节流
* FileInputStream
* FileOutputStream
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建File对象,作为中间结点的作用
File file = new File("D:\\io\\a.txt");
//输入流管道
FileInputStream fileInputStream = new FileInputStream(file);
//输出流管道
FileOutputStream fileOutputStream = new FileOutputStream("D:\\io\\456\\a.txt");
//获取输入流中剩余的字节数
int size = fileInputStream.available();
for (int i = 0; i < size; i++) {
//读取数据
int data = fileInputStream.read(); //读取一个字节的数据
//写出数据
fileOutputStream.write(data); //写一个字节
}
//关闭io流
fileOutputStream.close();
fileInputStream.close();
}
当文件多大时,复制效率会非常慢,所以推荐用下面的最终版复制方法:
/**
* 优化版、改进版、最终版()
* 2.将c:/a.txt复制到c:/aaa/a.txt(文件复制)
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//输入流管道
FileInputStream fileInputStream = new FileInputStream("D:\\io\\a.txt");
//输出流管道
FileOutputStream fileOutputStream = new FileOutputStream("D:\\io\\456\\a.txt");
//构建一个长度为1024的byte[]
byte[] data = new byte[1024];
while (true){
//一次读取一个数组长度的数据到数组中
int len = fileInputStream.read(data);
if (len == -1){
break;
}
fileOutputStream.write(data,0,len);
}
//关闭流
fileOutputStream.close();
fileInputStream.close();
}
/**
* 3.将c:/a.txt中的内容读取到String s中(两种方式)
* I流 文件——>程序(内存)
* IO流
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//创建I流对象
FileInputStream fileInputStream = new FileInputStream("D:\\io\\a.txt");
//定义一个数组
byte[] data = new byte[fileInputStream.available()];
//读取数据到数组中
fileInputStream.read(data);
//byte[]转String
String s = new String(data);
System.out.println(s);
}
字符流:只能处理文本资源,底层是字节流,一次读取一个字符
(下面也是通过具体的案例来学习)
//1.将字符串Hello Io写到c:/a.txt
public static void main(String[] args) throws IOException {
FileWriter fileWriter = new FileWriter("D:\\io\\a.txt");
fileWriter.write("Hello io"); //并没有直接写到硬盘去,而是写在了缓冲区
fileWriter.close(); //调用关闭方法时,会强制将缓冲区的数据写出来
}
/**
* 2.将c:/a.txt复制到c:/aaa/a.txt(文件复制)
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("D:\\io\\a.txt");
FileWriter fileWriter = new FileWriter("D:\\io\\456\\a.txt");
char[] data = new char[1024];
while (true){
int len = fileReader.read(data);
if (len == -1){
break;
}
fileWriter.write(data,0,len);
}
fileWriter.close();
fileReader.close();
}
/**
* 3.将c:/a.txt中的内容读取到String s中
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File("D:\\io\\a.txt");
FileReader reader = new FileReader(file);
char[] data = new char[(int) file.length()];
int len = reader.read(data);
String s = new String(data,0,len);
System.out.println(s);
}
其实字符流的使用和字节流的使用差不多,只是有一些些微的不同,参照学习即可。
缓冲流
/**
* 缓冲流
* 字节缓冲流:没有提供额外API,且内部提供的缓冲数组,可以在普通流中使用自定义数组实现。
* 字符缓冲流:有提供额外API加成,可以使用。
* Reader:readLine 读一行
* Writer:newLine 换一行
* @author admin
*
*/
public class Demo1 {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
// //1.构建字符普通流对象
// FileReader fileReader = new FileReader("D:\\io\\a.txt");
// //2.根据字符普通流对象构建字符缓冲流对象
// BufferedReader bufferedReader = new BufferedReader(fileReader);
//
// while(true){
// //3.一次读取一行
// String string = bufferedReader.readLine();
// if(string==null){
// break;
// }
// System.out.println(string);
// }
//将静夜思写到c:/b.txt
FileWriter fileWriter = new FileWriter("D:\\io\\a.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("静夜思");
bufferedWriter.newLine();
bufferedWriter.write("床前明月光");
bufferedWriter.newLine();
bufferedWriter.write("疑是地上霜");
bufferedWriter.newLine();
bufferedWriter.write("举头望明月");
bufferedWriter.newLine();
bufferedWriter.write("低头思故乡");
bufferedWriter.newLine();
//关闭流
bufferedWriter.close();
fileWriter.close();
}
}