I/O流
概念:内存与存储设备之间传输数据的通道
流的分类
按方向
- 输入流:将
存储设备
中的内容读到内存
中
- 输出流:将
内存
中的内容写到存储设备
中
按单位
- 字节流:以字节为单位,可以读写所有数据
- 字符流:以字符为单位,只能读写文本数据
按功能
- 节点流:具有实际传输数据的读写功能
- 过滤流:在节点流的基础之上增强功能
字节流
字节流的两个超类:InputStream
和OutputStream
1 2 3 4 5 6 7 8 9
| public int read(){} public int read(byte[] b){} public int read(byte[] b, int off, int len){}
public void write(int n){} public void write(byte[] b){} public void write(byte[] b, int off, int len){}
|
文件字节流
文件字节输入流
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
| public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("e:\\hello.txt");
int data = 0; while((data = fis.read()) != -1){ System.out.println((char)data); }
byte[] buf = new byte[3]; int count = fis.read(buf); System.out.println(new String(buf)); System.out.println(count); int count2 = fis.read(buf); System.out.println(new String(buf)); System.out.println(count2);
int count = 0; while((count = fis.read(buf)) != -1){ System.out.print(new String(buf, 0, count)); }
fis.close(); }
|
文件字节输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("e:\\hello.txt", true);
fos.write(97); fos.write('a');
String string = "hello world"; fos.write(string.getBytes()); fos.close(); }
|
图片复制Demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("e://aaa.jpg"); FileOutputStream fos = new FileOutputStream("e://bbb.jpg"); byte[] buf = new byte[1024]; int count = 0; while((count = fis.read(buf)) != -1){ fos.write(buf, 0, count); } fis.close(); fos.close(); }
|
字节缓冲流
字节缓冲流:BufferedInputStream
和BufferedOutputStream
- 提高IO效率,减少访问磁盘次数
- 数据存储在缓冲区中,flush是将缓冲区的内容写出到文件中,也可以直接close
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("e:\\hello.txt"); BufferedInputStream bis = new BufferedInputStream(fis); int data = 0; while((data = bis.read()) != -1){ System.out.println((char)data); } byte[] buf = new byte[1024*2]; int count = 0; while((count = bis.read(buf)) != -1){ System.out.println(new String(buf, 0, count)); }
bis.close();
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("e:\\hello.txt"); BufferedOutputStream bos = new BufferedOutputStream(fos); for(int i = 0; i < 10; i ++){ bos.write("hello".getBytes()); bos.flush(); } bos.close(); }
|
对象流
对象流:ObjectOutputStream
和ObjectInputStream
- 增强了缓冲区功能
- 增强了读写8种基本数据类型和字符串的功能
- 增强了读写对象的功能
使用流传输对象的过程称为序列化、反序列化
序列化和反序列化
序列化:使用流将对象存储到硬盘上称为序列化
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
| public class Student implements Serializable { private String name; private int age;
public Student() { }
public Student(String name, int age) { this.name = name; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("e:\\student.bin"); ObjectOutputStream oos = new ObjectOutputStream(fos);
Student zhangsan = new Student("张三", 20); oos.writeObject(zhangsan);
oos.close(); System.out.println("序列化完成"); }
|
反序列化:使用流将对象从硬盘上读取出来的过程称为反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) { FileInputStream fis = new FileInputStream("e:\\student.bin"); ObjectInputStream ois = new ObjectInputStream(fis);
Student s = (Student)ois.readObject();
ois.close();
System.out.println("反序列化完成"); System.out.println(s.toString()); }
|
注意:
- 某个类要想序列化必须实现Serializable接口
- 在将要序列化的类中通常添加
private static final long serialVersionUID = 100L;
属性,作为序列化版本号ID,保证序列化的类和反序列化的类是同一个类
- 使用transient修饰属性,这个属性就不能序列化
- 静态属性不能序列化
- 序列化多个对象,可以借助集合来实现
字符流
字符流的两个超类:Reader
和Writer
1 2 3 4 5 6 7 8 9
| public int read(){} public int read(char[] c){} public int read(char[] b, int off, int len){}
public void write(int n){} public void write(String str){} public void write(char[] c){}
|
文件字符流
文件字符输入流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public static void main(String[] args) throws IOException, ClassNotFoundException { FileReader fr = new FileReader("e:\\hello.txt");
int data = 0; while((data = fr.read()) != -1){ System.out.print((char)data); }
char[] buf = new char[2]; int count = 0; while((count = fr.read(buf)) != -1){ System.out.println(new String(buf, 0, count)); }
fr.close(); }
|
文件字符输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("e:\\hello.txt",true);
for(int i = 0; i < 10; i ++){ fw.write("这里写一些内容进去"); fw.flush(); }
fw.close(); }
|
字符缓冲流
字符缓冲流:BufferedReade
r和BufferedWirter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public static void main(String[] args) throws IOException { FileReader fr = new FileReader("e:\\hello.txt"); BufferedReader br = new BufferedReader(fr);
char[] buf = new char[1024]; int count = 0; while((count = br.read(buf)) != -1){ System.out.println(new String(buf, 0, count)); }
String line = null; while((line = br.readLine()) != null){ System.out.println(line); }
br.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("e:\\hello.txt"); BufferedWriter bw = new BufferedWriter(fw); for(int i = 0; i < 10; i ++){ bw.write("写出的内容"); bw.newLine(); bw.flush(); }
bw.close(); }
|
PrintWriter
封装了print() / println() 方法 支持写出后换行
支持数据原样打印
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) throws FileNotFoundException { PrintWriter pw = new PrintWriter("e:\\hello.txt"); pw.println(12); pw.println(true); pw.println(3.14); pw.println('a'); pw.close(); }
|
桥转换流
桥转换流:InputStreamReader
和OutputStreamWriter
1 2 3 4 5 6 7 8 9 10 11 12
| public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("e:\\hello.txt"); InputStreamReader isr = new InputStreamReader(fis, "gbk"); int data = 0; while((data = isr.read()) != -1){ System.out.println((char)data); } isr.close(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("e:\\hello.txt"); OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
for(int i = 0; i < 10; i ++){ osw.write("所写内容"); osw.flush(); }
osw.close(); }
|
File类
概念:代表物理盘符中的一个文件或文件夹
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| public static void main(String[] args) throws IOException { separator(); }
public static void separator(){ System.out.println("路径分隔符" + File.pathSeparator); System.out.println("名称分隔符" + File.separator); }
public static void fileOpen() throws IOException { File file = new File("..."); if(!file.exists()){ boolean b = file.createNewFile(); }
file.delete(); file.deleteOnExit();
System.out.println("获取绝对路径" + file.getAbsolutePath()); System.out.println("获取路径" + file.getPath()); System.out.println("获取文件名称" + file.getName()); System.out.println("获取夫目录" + file.getParent()); System.out.println("获取文件长度" + file.length()); System.out.println("文件创建时间" + new Date(file.lastModified()).toLocaleString());
System.out.println("是否可写" + file.canWrite()); System.out.println("是否是文件" + file.isFile()); System.out.println("是否隐藏" + file.isHidden()); }
public static void directoryOpe() throws Exception{ File dir = new File("..."); System.out.println(dir.toString()); if(!dir.exists()){ dir.mkdirs(); }
dir.delete(); dir.deleteOnExit();
System.out.println("获取绝对路径" + dir.getAbsolutePath()); System.out.println("获取路径" + dir.getPath()); System.out.println("获取文件名称" + dir.getName()); System.out.println("获取夫目录" + dir.getParent()); System.out.println("获取文件长度" + dir.length()); System.out.println("文件夹创建时间" + new Date(dir.lastModified()).toLocaleString());
System.out.println("是否是文件夹" + dir.isFile()); System.out.println("是否隐藏" + dir.isHidden());
File dir2 = new File("..."); String[] files = dir2.list(); for(String string : files){ System.out.println(string); }
File[] files2 = dir2.listFiles(new FileFilter(){
@Override public boolean accept(File pathname){ if(pathname.getName().endsWith(".jpg")){ return true; }else { return false; } } }); for(File file : files2){ System.out.println(file.getName()); } }
|
递归遍历文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public static void main(String[] args) { listDir(new File("e:\\myfiles")); } public static void listDir(File dir){ File[] files = dir.listFiles(); System.out.println(dir.getAbsolutePath()); if(files != null && files.length > 0){ for(File file : files){ if(file.isDirectory()){ listDir(file); }else { System.out.println(file.getAbsolutePath()); } } } }
|
递归删除文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public static void deleteDir(File dir){ File[] files = dir.listFiles(); if(files != null && files.length > 0){ for(File file : files){ if(file.isDirectory()){ deleteDir(file); }else{ System.out.println(file.getAbsolutePath() + "删除" + file.delete()); } } } }
|
Properties
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
| public static void main(String[] args) throws IOException { Properties properties = new Properties();
properties.setProperty("driver","com.mysql.jdbc.Driver"); properties.setProperty("url","jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&amp;useUnicode=true&amp;characterEncoding=utf8"); properties.setProperty("username","root"); properties.setProperty("password","lishuang001219");
Set<String> proNames = properties.stringPropertyNames(); for (String pro : proNames) { System.out.println(pro + "=" + properties.getProperty(pro)); }
PrintWriter pw = new PrintWriter("e:\\db.properties"); properties.list(pw); pw.close();
FileOutputStream fos = new FileOutputStream("e:\\db.properties"); properties.store(fos,"注释"); fos.close();
Properties properties1 = new Properties(); FileInputStream fis = new FileInputStream("e:\\db.properties"); properties1.load(fis); fis.close(); System.out.println(properties1.toString());
}
|
流的关闭
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| try (FileInputStream fis = new FileInputStream(f)) { }
finally { if (null != fis) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
|