IO流
什么是IO
IO流就是对文件进行的读写操作
从硬盘向内存中传输数据叫读(read),称为输入(Input),输入流(InputStream)
从内存向硬盘传输数据叫写(write),称为输出(Output),输出流(OutpurStream)
IO:I(input),O(Output)
IO的分类
按照流的方向进行分类:以内存作为参考点
往内存中去叫做读(read),也就是(InputStream)输入流
从内存中出来叫做写(write),也就是(OutoutStream)输出流
按照读取数据的方式
字节流:一次性读取一个字节,也就是8位,字节流可以读取任意类型的文件
字符流:一次读取一个字符,但字符流只能读取普通文本文件(能用记事本编辑的文件都是普通文本文件)
IO流的四个大类
java.io.InputStream 字节输入流
java.io.OutputStream 字节输出流
java.io.Reader 字符输入流
java.io.Writer 字符输出流
在Java中凡是以Stream结尾的都是字节流,以Reader和Writer结尾的都是字符流
由于使用流会占用资源,所以在用完后,一定要关闭( close() ),在流最终输出后一定要刷新( flush() )
IO中的流
文件专属的流
Java.io.FileInputStream
java.io.FileOutputStream
java.io.FileReader
java.io.FileWriter
转换流(将字节流转换位字符流)
java.io.InputStreamWriter
java.io.OutpurStreamReader
缓冲流
java.io.BufferedInputStream
java.io.BufferedOutputStream
java.io.BufferedReader
java.io.BufferedWriter
文件专属流
FIleInputStream
import java.io.FileInputStream;
import java.io.IOException;
/*
一次读取一个字符,效率低下
文件的路径:idea中的文件路径是以项目所在路径开始的
*/
public class FileInputStreamTest01 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("文件的路径");
int a = 0;
while ((a = fis.read()) != -1){
System.out.println((char)a);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/************************************************************************/
/*
一次读取多个字节
效率比较高
*/
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamTest01 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("文件的路径");
byte[] a = new byte[10];
int b = 0;
while ((b = fis.read(a)) != -1){
System.out.println(new String(a,0,b));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class FileOutputStreamTest1 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
//如果文件不存在则自己创建
fos = new FileOutputStream("文件",true);//true表示在源文件的末尾进行追加
byte[] xie = {12,23,35,4,56,67,78};
fos.write(xie);
String str = "哈哈哈哈哈";
byte[] stra = str.getBytes(StandardCharsets.UTF_8);//将字符串转化为字节,并设置字符集为utf-8
fos.write(stra);
fos.flush();//写完以后要刷新一下
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos == null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件的复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("xxx.txt");
fos = new FileOutputStream("XXXXXX.txt",true);
//一边读,一边写
byte[] aa = new byte[1024 * 1024];
int bb = 0;
while ((bb = fis.read(aa)) != -1){
fos.write(aa,0,bb);
}
fos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos == null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis == null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader
import java.io.FileReader;
import java.io.IOException;
public class FileReaderTest {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("文件的路径");
char[] chars = new char[10];//读取时按照字符进行读取
int char1 = 0;
while ((char1 = fr.read(chars)) != -1){
System.out.println(new String(chars,0,char1));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fr == null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("xxx.txt",true);
char[] chars = {'q','d','d','d','d','f'};
fw.write(chars);
String str = "嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻嘻";
fw.write(str);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fw == null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
缓冲流
BufferedReader
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader("xxx.txt");//fr称为节点流
br = new BufferedReader(fr);//br称为处理流/包装流
String str = null;
while ((str = br.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br == null) {
try {
br.close();//关闭时只需要将最外面的处理流关闭就行
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**********************************************************************/
//通过字节流实现上面的功能
import java.io.*;
public class BufferedReaderTest {
public static void main(String[] args) {
FileInputStream fis = null;
InputStreamReader isr = null;//InputStreamReader转换流,将字节转换为字符
BufferedReader br = null;
try {
fis = new FileInputStream("xxx.txt");
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
String str = null;
while ((str = br.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (br == null) {
try {
br.close();//关闭时,也只需要关闭最外层
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BuffrerdWriterTest {
public static void main(String[] args) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter("xxx.txt");
bw = new BufferedWriter(fw);
bw.write("123456787346787dhdfsjdshfhfhdsf吃的那家吃多少机会的说法挥洒");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**********************************************************************/
//通过字节流实现上面的功能
import java.io.*;
public class BuffrerdWriterTest {
public static void main(String[] args) {
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter bw = null;
try {
fos = new FileOutputStream("xxx.txt");
osw = new OutputStreamWriter(fos);//OutputStreamWriter转换流
bw = new BufferedWriter(osw);
bw.write("成本价大宋北斗司接口方便独守空房把会计师的看法不都是空发布的时刻");
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}