1. File
1.1 File与流


1.2 File练习
例1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
import java.io.File; import java.io.IOException;
public class FileTest01 { public static void main(String[] args) throws IOException { File file = new File("C:\\aaa\\bbb\\ccc\\1.txt"); if (file.exists()){ file.delete(); }else{ file.getParentFile().mkdirs(); file.createNewFile(); } } }
|
例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.util.stream.Stream;
public class FileTest02 { public static void main(String[] args) throws IOException { File file = new File("C:\\Program Files\\Java\\jdk1.8.0_361\\bin"); String[] ay = file.list((dir, name) -> name.endsWith(".exe")); Stream.of(ay).forEach(System.out::println); } }
|
2. 节点流
2.1 字节流

2.2 节点流练习
例3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream;
public class FileOutputStreamTest03 { public static void main(String[] args) throws IOException { OutputStream os = new FileOutputStream("1.txt",true) ; for (int i = 'A'; i <='Z'; i++) { os.write(i); }
os.close(); } }
|
例4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream;
public class FileInputStreamTest04 { public static void main(String[] args) throws IOException { InputStream is = new FileInputStream("Test1.java") ;
int data = 0 ; while((data = is.read())!=-1){ System.out.print((char)data); } is.close(); } }
|
2.3 字符流

2.4 节点流

输入流


输出流


例5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.io.*;
public class PicCopyTest05 { public static void main(String[] args) throws IOException { InputStream is = new FileInputStream("D:\\Java180_2\\doc\\day01-html基础\\res\\mm.jpg") ; OutputStream os = new FileOutputStream("meimei.jpg") ; while (true){ int data = is.read() ; if (data==-1){ break; } os.write(data); }
System.out.println("====图片拷贝结束========="); } }
|
3. 处理流
3.1 流嵌套

3.2 处理流

3.3 调包侠
https://commons.apache.org/proper/commons-io/description.html

4. 对象流

例6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
import java.io.*;
public class ObjectOutputStreamTest11 { public static void main(String[] args) throws IOException { Goods goods = new Goods("G1001", "苹果", 4.5D);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("4.txt")) ;
oos.writeObject(goods);
oos.flush(); oos.close(); } }
import java.io.*;
public class ObjectInputStreamTest12 { public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("4.txt")) ;
Object o = ois.readObject();
ois.close();
System.out.println(o);
} }
|