流的分类
- 操作数据单位不同分为:字节流(8 bit) ,字符流(按字符,字符等于多少字节按不同编码来)
- 按数据流的流向不同分为: 输入流,输出流
- 按流的角色不同分为:节点流,处理流/包装流
抽象基类(都是抽象类) 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer
重点,都是抽象类,实现需要使用他们的子类
各自优缺点:
- 字符流效率高
- 字节流能处理二进制文件,视频,音频
InputStream:字节输入流
常用子类:
-
FileInputStream 文件输入流
- 构造器: File(文件类) || 文件描述符 || String(绝对地址)
- 方法:
- close() 关闭输入流并释放相关资源
- read() 从此输入流中读取一个字节
- read(byte[] b) 从输入流中将最多b.length()个字节读入一个byte数组中,提高效率
-
BufferedInputStream 缓冲字节输入流
-
ObjectInputStream 对象字节输入流
-
FileInputStream read()使用(模板)(效率不高,不推荐)
//read
public static void readFile01() throws IOException {
String filePath = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\hello.txt";
FileInputStream file01 = null;
try {
file01 = new FileInputStream(filePath);
//如果read方法返回-1,表示读取完毕
int readData=0;
while((readData = file01.read())!=-1){
System.out.print((char)readData);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
//一定要关闭流,释放资源,并且抛出异常
file01.close();
}
}
- FileInputStream read(byte[] b)的使用(模板) (效率高,推荐)
public static void readFile02() throws IOException {
String filePath = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\hello.txt";
//一次读取八个字节,如果读取正常,返回实际读取个数,结束返回-1
int dataLen=0;
byte[] buf = new byte[8];
FileInputStream file01 = null;
try {
file01 = new FileInputStream(filePath);
while((dataLen = file01.read(buf))!=-1){
System.out.print(new String(buf, 0, dataLen));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
//一定要关闭流,释放资源,并且抛出异常
file01.close();
}
}
OutputStream:字节输出流
- FileOutputStream的使用(模板)
- String.getBytes()方法能将字符串转化为Byte数组,提高效率
- write(byte[] , st, ed);
- write(byte[]);
- new (path , append) ,后面的append(追加)为true,则字节将传入末尾而不是覆盖,默认为flase
public static void writeFile() throws IOException {
String filePath = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\Write.txt";
FileOutputStream fileOut = null;
try {
//得到一个输入流
fileOut = new FileOutputStream(filePath,true);
fileOut.write('a');//写入一个字符
String str = "Hello , World!";
fileOut.write(str.getBytes());//String 的 getBytes 可以转化从byte数组
System.out.println("输入完成......");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
fileOut.close();
}
}
- 尝试下文件copy(模板)
- 注意需要边输入边输出,为了提高效率采用read(byte[] b)的方式,
- read()里面一定要加byte数组的参数
public static void copy02() throws IOException {
String filePathIn = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\01.png";
String filePathOut = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\03.png";
FileInputStream fileInput = null;
FileOutputStream fileOutput = null;
try {
fileInput = new FileInputStream(filePathIn);
fileOutput = new FileOutputStream(filePathOut,true);
int readLen=0;
byte[] buf = new byte[1024];
while((readLen=fileInput.read(buf))!=-1){
fileOutput.write(buf,0,readLen);
}
System.out.println("Copy success!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {//加入判断安全
if(fileInput != null) fileInput.close();
if(fileOutput!=null) fileOutput.close();
}
}
FileReader字符输入流
- read()
//循环读取
public static void read01() throws IOException {
String filePath = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\hello.txt";
FileReader reader = null;
try {
reader = new FileReader(filePath);
int dataRead = 0;
while((dataRead = reader.read())!=-1){
System.out.print((char) dataRead);
}
System.out.println("\nReader success!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(reader != null) reader.close();
}
}
- read(char [] b)
//数组读取
public static void read02() throws IOException {
String filePath = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\hello.txt";
FileReader reader = null;
try {
reader = new FileReader(filePath);
int readLen = 0;
char[] buf = new char[8];
while((readLen = reader.read(buf))!=-1){
System.out.print(new String(buf,0,readLen));
}
System.out.println("\nReader success!");
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if(reader != null) reader.close();
}
}
FileWriter字符输出流
没有关闭输出流无法不仅浪费资源,还无法生成文件
- write(int); 写入单个字符
- write(char[]); 写入指定数组
- write(char[],off,len); 写入指定数组指定部分
- write(string); 写入整个字符串
- write(string,off,len); 写入字符串指定部分
因为类似,就写一个例子
public static void writer01() throws IOException {
String filePath = "C:\\Users\\Laity\\Desktop\\Java全栈\\JavaIO\\Test文件\\fileWriter.txt";
FileWriter writer = null;
try {
writer = new FileWriter(filePath,true);
String str = "wjs学习了FileWriter......";
writer.write(str);
System.out.println("writer success!");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer!=null) writer.close();
}
}