IO流的基础与小型项目
对于流的描述,流是一种抽象的描述。
流的分类:
1、输入流(Input)
2、输出流(Output)
按类型分:
1、字节流(InputStream/OutputStream)
1 public class ReadFileByLoop { 2 3 public static void main(String[] args) throws IOException { 4 // TODO Auto-generated method stub 5 6 // 根据给定的文件路径构建一个字节输入流对象 7 File f = new File("C:\\Users\\lenovo\\Desktop\\培训\\试卷及答案\\1.txt"); 8 InputStream is = new FileInputStream(f); 9 // 准备一个自己缓冲区 10 byte[] b = new byte[1024]; 11 // 申明一个临时变量记录本次读取的真实长度 12 int len = 0; 13 while ((len = is.read(b)) != -1) { 14 String s = new String(b, 0, len); 15 System.out.println(s); 16 } 17 18 // is.close(); 19 } 20 21 }
2、字符流(Reader/Writer)
1 public class ReadFileByChar { 2 3 public static void main(String[] args) throws IOException { 4 //创建一个File对象 5 File f = new File("C:\\Users\\lenovo\\Desktop\\培训\\试卷及答案\\1.txt"); 6 //构建一个字符输入流 7 FileReader fr = new FileReader(f); 8 char[] a=new char[1024]; 9 int len; 10 while((len=fr.read(a))!=-1){ 11 String s=new String(a,0,len); 12 System.out.println(s); 13 } 14 fr.close(); 15 } 16 } 17
按照功能分:
1、节点流(低级流)
2、处理流(高级流)
字节流到字符流之间的流动用InputStreamReader
BufferedReader x=new BufferedReader(new InputStreamReader(System.in))
字符流到字符流之间的流动用OutputStreamWriter
对一个文件夹拷贝
1 public class Test { 2 3 // 递归方法 4 public static void copyFile(File file, File file2) { 5 // 当找到目录时,创建目录 6 if (file.isDirectory()) { 7 file2.mkdir(); // 创建目录 8 File[] files = file.listFiles(); 9 for (File f : files) { 10 // 递归 11 copyFile(f, new File(file2, f.getName())); 12 } 13 // 当找到文件时 14 } else if (file.isFile()) { 15 File f = new File(file2.getAbsolutePath()); 16 try { 17 f.createNewFile(); 18 copyDatas(file.getAbsolutePath(), f.getAbsolutePath()); 19 } catch (IOException e) { 20 e.printStackTrace(); 21 } 22 } 23 } 24 25 // 复制文件数据的方法 26 public static void copyDatas(String filePath, String filePath1) { 27 FileInputStream fis = null; 28 FileOutputStream fos = null; 29 try { 30 // 字节流 31 fis = new FileInputStream(filePath); 32 fos = new FileOutputStream(filePath1); 33 byte[] buffer = new byte[1024]; 34 while (true) { 35 int temp = fis.read(buffer, 0, buffer.length); 36 if (temp == -1) { 37 break; 38 } else { 39 fos.write(buffer, 0, temp); 40 } 41 } 42 } catch (IOException e) { 43 System.out.println(e); 44 } finally { 45 try { 46 fis.close(); 47 fos.close(); 48 } catch (IOException e) { 49 System.out.println(e); 50 } 51 } 52 } 53 54 public static void main(String args[]) { 55 File file = new File("D:\\大学文件管理"); 56 File file2 = new File("D:\\1"); 57 copyFile(file, file2); 58 } 59 }
对象序列化
对象序列化是一种用于在文件或者各种其他输入输出设备中存储java对象的机制,通过将实现过序列化结构的对象存储到指定的输出源,可以完整的保存对象数据;对象序列化机制一般常用于大型项目中的缓存技术,以缓存经常需要使用到的对象数据;java中实现对象序列化通常包含两种方式:
-
实现Serializable接口
-
实现Externalizable接口
完成对象序列化的过程必须包含以下步骤:
-
需要完成序列化的对象对应的类必须实现Serializable接口
-
通过对象输出流将对象存储到指定的输出源(文件,网络)中(通过ObjectOutputStream)
例子:
输入三个成绩,语数外三个科目的成绩:按照总成绩存到文件夹,在控制台查看
1 2 public class Student implements Serializable{ 3 4 /** 5 * 6 */ 7 private static final long serialVersionUID = 1L; 8 private int chinese; 9 private int math; 10 private int english; 11 private int sorce; 12 13 public Student() { 14 // TODO Auto-generated constructor stub 15 } 16 public Student(int chinese, int math, int english) { 17 super(); 18 this.chinese = chinese; 19 this.math = math; 20 this.english = english; 21 this.sorce=chinese+english+math; 22 } 23 public int getSorce() { 24 return sorce; 25 } 26 public int getChinese() { 27 return chinese; 28 } 29 public void setChinese(int chinese) { 30 this.chinese = chinese; 31 } 32 public int getMath() { 33 return math; 34 } 35 public void setMath(int math) { 36 this.math = math; 37 } 38 public int getEnglish() { 39 return english; 40 } 41 public void setEnglish(int english) { 42 this.english = english; 43 } 44 @Override 45 public String toString() { 46 return "Student 语文=" + chinese + ", 数学=" + math + ", 英语=" + english + ", 总分=" + sorce + "]"; 47 } 48 49 } 50 51 52 53 54 55 56 package com.softeem.Objects; 57 58 import java.io.FileInputStream; 59 import java.io.FileOutputStream; 60 import java.io.IOException; 61 import java.io.InputStream; 62 import java.io.ObjectInputStream; 63 import java.io.ObjectOutputStream; 64 import java.io.OutputStream; 65 import java.util.ArrayList; 66 import java.util.Comparator; 67 import java.util.List; 68 import java.util.Scanner; 69 70 public class StudentTest { 71 ArrayList<Student> list = new ArrayList<>(); 72 Student stu = new Student(); 73 74 public void scanner() { 75 Scanner sc = new Scanner(System.in); 76 System.out.println("语文成绩:"); 77 int a = sc.nextInt(); 78 System.out.println("数学成绩:"); 79 int b = sc.nextInt(); 80 System.out.println("英语成绩:"); 81 int c = sc.nextInt(); 82 Student stu = new Student(a, b, c); 83 list.add(stu); 84 System.out.println("添加成功!"); 85 } 86 87 public void select() { 88 for (Student s : list) { 89 System.out.println(s); 90 } 91 92 } 93 94 public void copy() throws IOException, ClassNotFoundException { 95 list.sort(new Comparator<Student>() { 96 @Override 97 public int compare(Student x, Student y) { 98 // TODO Auto-generated method stub 99 return x.getSorce() - y.getSorce(); 100 } 101 }); 102 OutputStream os = new FileOutputStream("ok.txt"); 103 ObjectOutputStream oos = new ObjectOutputStream(os); 104 oos.writeObject(list); 105 oos.close(); 106 } 107 108 private void selectCopy() throws IOException, ClassNotFoundException { 109 InputStream is = new FileInputStream("ok.txt"); 110 ObjectInputStream ois = new ObjectInputStream(is); 111 Object obj = ois.readObject(); 112 //ois.readLine() 113 System.out.println(obj); 114 } 115 public void pint() throws ClassNotFoundException, IOException { 116 System.out.println("----------【1】输入学生成绩-----------"); 117 System.out.println("----------【2】学生成绩查询-----------"); 118 System.out.println("----------【3】将学生信息添加到文件中----"); 119 System.out.println("----------【4】显示文件中的信息--------"); 120 System.out.println("----------【5】退出--------"); 121 Scanner sc = new Scanner(System.in); 122 System.out.println("请选择:"); 123 int i = sc.nextInt(); 124 switch (i) { 125 case 1: 126 scanner(); 127 pint(); 128 break; 129 case 2: 130 System.out.println("查询结果如下:"); 131 select(); 132 System.out.println("查询成功"); 133 pint(); 134 case 3: 135 copy(); 136 pint(); 137 System.out.println("导入成功!"); 138 break; 139 case 4: 140 selectCopy(); 141 pint(); 142 break; 143 case 5: 144 System.out.println("退出成功!"); 145 break; 146 default: 147 System.err.println("输入错误"); 148 } 149 } 150 151 152 153 public static void main(String[] args) throws ClassNotFoundException, IOException { 154 new StudentTest().pint(); 155 } 156 }