自己总结的,不全面,不够系统,参考即好!
IO流
1. File类
构造方法:
(1)File file = new File("d:\\test\\abc.txt");
(2)File file2 = new File("D:\\test", "abc.txt");
(3)File parent = new File("D:\\test");
File file3 = new File(parent, "abc.txt");
2. 字节流
字节输入流:FileInputStream
字节输出流:FlieOutputStream
复制文件练习:
①一次一个字节的复制(慢)
public class CopyFile {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:\\YesDir\\test.JPG");
FileOutputStream out = new FileOutputStream("CopyTest.JPG");
//3.从数据源中读数据
int ch = -1; //临时变量ch,用来记录每次读到的字节
while( (ch = in.read()) != -1 ){
//4.写数据到目的地
out.write(ch);
}
//5.关闭流
in.close();
out.close();
}
}
②一次一个数组的复制(快)
public class CopyFile02 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("C:\\YesDir\\test.JPG");
FileOutputStream out = new FileOutputStream("CopyTest2.JPG");
//3.从数据源中读数据
byte[] buffer = new byte[1024]; //创建一个临时数组存储每次从文件中读到的数据
int len = -1; // 用来记录每次从文件中读到多少个新字节数量
while ( (len = in.read(buffer)) != -1 ) {
//4.写数据到目的地
out.write(buffer, 0, len);
// out.write(buffer);
}
//5.关闭流
in.close();
out.close();
}
}
3.字符流
字符输入流:FileReadr
字符输出流:FileWriter
复制文档练习:
public class CopyFile {
public static void main(String[] args) throws IOException {
method1();//一次一个字符方式
method2();//一次一个字符数组方式
}
//一次一个字符数组方式
private static void method2() throws IOException {
FileReader in = new FileReader("copy.java");
FileWriter out = new FileWriter("copy3.java");
char[] buffer = new char[1024];
int len = -1;
while ( (len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();
}
//一次一个字符方式
private static void method1() throws IOException {
FileReader in = new FileReader("copy.java");
FileWriter out = new FileWriter("copy2.java");
int ch = -1;
while ( (ch = in.read()) != -1 ) {
out.write(ch);
}
in.close();
out.close();
}
}
4. 转换流
将字节流转换成字符流
输入流:
InputStream is = new FileInputStream("file2.txt");
目的一: 将 字节输入流InputStream对象 ,通过构造方法,把字节流 包装成了 字符流
InputStreamReader isr = new InputStreamReader( is ); //第一种构造
目的二:从文件中读取到的字节数据,可以通过字符编码表,获取到对应的字符数据
InputStreamReader isr = new InputStreamReader( is, "GBK" ); //第二种构造
输出流:
OutputStream os = new FileOutputStream("file2.txt");
目的一: 把字节流转换成字符流,通过字符流中的方法写入字符串,好处是方便写入字符串数据
OutputStreamWriter osw = new OutputStreamWriter( os ); //第一种构造
目的二:可以指定写入文件中的数据,按照指定的字符编码表进行写入
OutputStreamWriter osw = new OutputStreamWriter(os, "GBK"); //第二种构造
5. 缓冲流 (速度快)
字节缓冲流:
BufferedInputStream bis = new BufferedInputStream( new FileInputStream("file3.txt"));
BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream("file3.txt") );
字符缓冲流:
BufferedReader br = new BufferedReader( new FileReader("file4.txt"));
//增加了一行一行读数据方法(readline)
String line = null; //用来存储每次从文件中读到的一行字符串内容
while ( (line = br.readLine()) != null ) {
//4.写数据
bw.write(line);
bw.newLine();//换行
bw.flush();//刷新
}
BufferedWriter bw = new BufferedWriter(new FileWriter("file4.txt"));
6. Properties类
Properties类是Map集合的子类,是唯一一个可以和IO流交互的集合
(一)创建集合 并添加元素
《《《该集合没有泛型,创建时不用加泛型》》》
Properties prop = new Properties();
//prop.put(key, value); //可以用put
prop.setProperty("周迅", "张学友"); //元素的键和值均为String类型
特有的一种遍历集合的方法(类似键找值方法):
Set<String> keys = prop.stringPropertyNames(); //stringPropertyNames()方法返回 键的集合
for (String key : keys) {
//键找值
String value = prop.getProperty(key);
System.out.println(key +"----"+value);
}
(二)
把集合内容存储到IO流所对应文件中:
FileWriter out = new FileWriter("prop.txt");
prop.store(out, "HelloWorld"); //其中HelloWorld为注释内容,可以什么都不写
out.close();
}
}
把文件中内容存储到集合中:
FileReader in = new FileReader("prop.txt");
Properties prop = new Properties(); //创建集合
prop.load(in);
in.close();
System.out.println(prop);
}
}
7. 序列化流和反序列化流
用于从流中读取对象的操作流 ObjectInputStream 称为 反序列化流
用于向流中写入对象的操作流 ObjectOutputStream 称为 序列化流
特点:用于操作对象。可以将对象写入到文件中,也可以从文件中读取对象。
序列化:
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Object.txt"));
//Person类必须实现Serializable接口,(public class Person implements Serializable )
out.writeObject(new Person("小明", 28)); // Person对象,
out.close();
反序列化:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("Object.txt"));
// Object.txt 文件必须是由序列化产生的文件
Object obj = in.readObject();
System.out.println(obj);
in.close();
person类添加序列化版本号,点下面的黄线
8. 打印流
BufferedReader in = new BufferedReader(new FileReader("test.java"));
PrintWriter out = new PrintWriter(new FileWriter("copy.java") , true); //true可以自动换行、刷新
String line = null;
while ( (line = in.readLine()) != null ) {
out.println(line);
}
out.close();
in.close();
9. 递归
递归,指在当前方法内调用自己的这种现象
编写一个方法用来打印指定目录中的.java文件路径,并进行方法的调用
若指定的目录有子目录,那么把子目录中的.java文件路径也打印出来
public class DiGuiTest04 {
public static void main(String[] args) {
//1. 指定要打印的目录File对象
File filePath = new File("d:\\YesDir");
//2. 调用getFileAll()方法,传入要打印的目录File对象
getFileAll( filePath );
}
//调用getFileAll()方法,传入要打印的目录File对象
public static void getFileAll(File filePath) {
//2.1 获取当前目标下所有的File对象
File[] files = filePath.listFiles();
//2.2遍历,得打每一个File对象
for (File file : files) {
//2.3判断当前File对象是否为文件夹
if (file.isDirectory()) {
//true, 代表是文件夹, 递归回到 步骤2, 调用getFileAll()方法
getFileAll(file); //file --- D:\YesDir\小视频
} else {
//false,代表是文件
//2.4 如果是文件,继续判断该文件是否为.java后缀名的文件
if (file.getName().endsWith(".java")) {
//true, 打印文件路径
System.out.println(file.getAbsolutePath());
}
}
}
}
} |
|