1.IO流分类
a:按照数据流向
输入流 读入数据
输出流 写出数据
b:按照数据类型
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件
字节输入流 InputStream 读
字节输出流 OutputStream 写
字符流
字符输入流 Reader 读
字符输出流 Writer 写
2.IO流基类概述
a:字节流的抽象基类:
InputStream ,OutputStream。
b:字符流的抽象基类:
Reader , Writer。
3. 构造方法
FileOutputStream(File file)
FileOutputStream(String name)
A:FileOutputStream的三个write()方法
public void write(int b):写一个字节 超过一个字节 砍掉前面的字节
public void write(byte[] b):写一个字节数组
public void write(byte[] b,int off,int len):写一个字节数组的一部分
案例演示:复制一个mp3文件
FileInputStream in1 = new FileInputStream("D:\\测试音乐.mp3");
FileOutputStream out1 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\测试音乐.mp3");
byte[] bytes1 = new byte[1024 * 10];
int l = 0;
while ((l = in1.read(bytes1)) != -1) {
out1.write(bytes1, 0, l);
}
in1.close();
out1.close();
案列演示:编写程序,获取指定目录下 所有的.java结尾的文件, 打印出文件的绝对路径(包含 子文件夹中的.java文件
public class Test {
public static void main(String[] args) {
//获取指定目录下 所有的.java结尾的文件, 打印出文件的绝对路径(包含 子文件夹中的.java文
File file = new File("D:\\westos");
chack(file);
}
private static void chack(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile()){
JianCha(f);
}else {
File file1 = new File(file + "\\" + f.getName());
chack(file1);
}
}
}
private static void JianCha(File f) {
if(f.getName().endsWith(".java")){
String abf = f.getAbsolutePath();
System.out.println(abf);
}
}
}
案例演示:编写程序,删除 指定目录下的 所有文件与文件夹 (包含子文件夹)
public class MyTest2 {
public static void main(String[] args) {
//删除多级目录
File file = new File("E:\\demo");
deleteFolder(file);
}
private static void deleteFolder(File file) {
//获取此目录下所有的文件或者目录
File[] files = file.listFiles();
for (File f : files) {
if (f.isFile()) {
f.delete();
} else {
deleteFolder(f);
}
}
file.delete();//删除自身这个空文件夹
}
}
案例演示:编写程序,采用多种方式,把d:\video01.avi的内容复制到d:\video02.avi中
方式1:基本字节流一次读写一个字节
方式2:基本字节流一次读写一个字节数组
方式3:高效字节流一次读写一个字节
方式4:高效字节流一次读写一个字节数组
public class Test1 {
public static void main(String[] args) throws IOException {
// 方式1:基本字节流一次读写一个字节
// 方式2:基本字节流一次读写一个字节数组
// 方式3:高效字节流一次读写一个字节
// 方式4:高效字节流一次读写一个字节数组
InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\\\video01.avi"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\\\video02.avi"));
measure1(in,out);
measure2(in,out);
BufferedInputStream input = new BufferedInputStream(new FileInputStream("d:\\\\video01.avi"));
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream("d:\\\\video02.avi"));
measure3(input,output);
measure4(input,output);
}
//方式一:
private static void measure1(InputStreamReader in, OutputStreamWriter out) throws IOException {
int len=0;
while ((len=in.read())!=-1){
out.write(len);
//字符流记得刷一下
out.flush();
}
System.out.println("复制完成");
in.close();
out.close();
}
//方式二:
private static void measure2(InputStreamReader in, OutputStreamWriter out) throws IOException {
int len = 0;
char[] chars = new char[1024];
while ((len = in.read(chars)) != -1) {
out.write(chars, 0, len);
out.flush();
}
//释放资源
in.close();
out.close();
}
//方式三
private static void measure3(BufferedInputStream input, BufferedOutputStream output) throws IOException {
int len=0;
while ((len=input.read())!=-1){
output.write(len);
output.flush();
}
input.close();
output.close();
}
//方式四
private static void measure4(BufferedInputStream input, BufferedOutputStream output) throws IOException {
int len = 0;
byte[] bytes = new byte[1024 * 8];
while ((len = input.read(bytes)) != -1) {
output.write(bytes, 0, len);
output.flush();
}
//释放资源
input.close();
output.close();
}
}
学到流这一块,真的需要多敲代码,多练手,只有熟悉了调用,了解有哪些方法是可以直接调用的,可以节省很多时间,包括一些文件复制格式是固定的,得多熟悉代码,将流程可以很熟练的敲下来·,还是得多用心,一入程序深似海啊!