2019-01-22
内容:IO流
一、java.io.File类
@凡是与输入、输出相关的类、接口都定义在java.io包下面
@File是一个类,可以使用构造器构造其实例对象,对象对应着一个文件或者文件夹
相对路径:在当前工程下的文件的路径
@File类对象与平台无关
@File类中的方法仅涉及对文件的创建、删除、重命名等表面的操作,无法操作文件内容
@File的对象作为io流的具体类的构造器的形参
注意:mkDir:创建文件夹,只能在上层文件夹存在才能创建成功
mkDirs创建文件夹,当上层文件夹不存在时也能创建
list()返回一个字符串数组的文件名
listFiles()返回一个文件数组
二、io流
@用来处理设备之间的数据传输
@JAVA程序中,对数据的输入/输出操作以流的方式进行
@java.io包下面提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或者输出数据
@流的分类:*按流向分类:输入流和输出流
*按数据单位分:字节流和字符流(纯文本文件.txt文件是字符流,其他都是字节流)
*按流的角色分:节点流(4个InputStrem,OutputStream,FileReader,FileWriter,都是处理文件的)和处理流
@JAVA的io流共涉及40多个类,都是从四个抽象基类派生出来的(InputStrem,OutputStream,Reader,Writer),并且由这4个基类派生出来的子类的名称都是以其父类的名称作为后缀的
三、FileInputStream
七、字符流和字节流复制区别
字符流只能复制文本文件,一般用它处理文本文件
字节流可以复制文本文件、音频文件等
八、处理流之缓冲流
缓冲流比节点流速度快
功能:*输入时实现字节流到字符流的转换(文件是文本文件),使用InputStreamReader
*输出时实现字符流到字节流的转换,使用OutputStreamWriter
十一、数据流DateInputStream和DateOutputStream,目的是更方便地操作基本数据类型
十二、对象流ObjectInputStream和ObjectOutputStream,目的是存储和读取对象
序列化:用ObjectOutputStream类将一个JAVA对象写入io流
反序列化:用ObjectInputStream类从io流中恢复该对象
序列化机制:把JAVA对象转换成和平台无关的二进制流,这个二进制流可以放在硬盘中,也可以通过网络传输到另一个网络节点。
序列化好处:可以把JAVA对象转换成字节数据来保存和传输
可以实现序列化地类要求:*这个类必须实现可序列化接口
*这个类中属性地类型必须实现可序列化接口
*这个类要提供一个序列号private static final serialVersionUID
*使用static和transient修饰地属性不能被序列化
十三、随机存取流RandomAccessFile
@凡是与输入、输出相关的类、接口都定义在java.io包下面
@File是一个类,可以使用构造器构造其实例对象,对象对应着一个文件或者文件夹
1 @Test 2 public void Test1() { 3 //创建一个File类对象,代表一个文件 4 File file1 = new File("d:/io/io.txt"); 5 //创建一个File类对象,代表一个文件夹 6 File file2 = new File("d:/io"); 7 }
@相对路径和绝对路径的区别:绝对路径:包括盘符在内的完整的文件路径
相对路径:在当前工程下的文件的路径
@File类对象与平台无关
@File类中的方法仅涉及对文件的创建、删除、重命名等表面的操作,无法操作文件内容
@File的对象作为io流的具体类的构造器的形参
注意:mkDir:创建文件夹,只能在上层文件夹存在才能创建成功
mkDirs创建文件夹,当上层文件夹不存在时也能创建
list()返回一个字符串数组的文件名
listFiles()返回一个文件数组
二、io流
@用来处理设备之间的数据传输
@JAVA程序中,对数据的输入/输出操作以流的方式进行
@java.io包下面提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或者输出数据
@流的分类:*按流向分类:输入流和输出流
*按数据单位分:字节流和字符流(纯文本文件.txt文件是字符流,其他都是字节流)
*按流的角色分:节点流(4个InputStrem,OutputStream,FileReader,FileWriter,都是处理文件的)和处理流
@JAVA的io流共涉及40多个类,都是从四个抽象基类派生出来的(InputStrem,OutputStream,Reader,Writer),并且由这4个基类派生出来的子类的名称都是以其父类的名称作为后缀的
三、FileInputStream
1 实现读取文件中的内容到程序中并打印出来 2 @Test 3 public void Test2() { 4 //使用FileInputStream流向程序中读文件内容,并打印出来,文件必须存在 5 //第二步:创建一个输入流的对象 6 FileInputStream fis = null;//写在外面时为了关闭流 7 try { 8 //第一步:新建一个文件对象 9 //File file1 = new File("C:\\Users\\贾博蓉\\code\\Demo1\\src\\hello.txt");//两种写法都可以 10 File file1 = new File("C:/Users/贾博蓉/code/Demo1/src/hello.txt"); 11 fis = new FileInputStream(file1); 12 //第三步:利用流的对象调用方法,实现文件的读取 13 int b; 14 while ((b = fis.read())!=-1) { 15 System.out.print((char)b); 16 } 17 }catch (Exception e) { 18 e.getMessage(); 19 }finally { 20 if (fis!=null) { 21 try { 22 fis.close(); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 } 29 @Test 30 public void Test2() { 31 //使用FileInputStream流向程序中读文件内容 32 //第二步:创建一个输入流的对象 33 FileInputStream fis = null;//写在外面时为了关闭流 34 try { 35 //第一步:新建一个文件对象,将文件传进来 36 //File file1 = new File("C:\\Users\\贾博蓉\\code\\Demo1\\src\\hello.txt");//两种写法都可以 37 File file1 = new File("C:/Users/贾博蓉/code/Demo1/src/hello.txt"); 38 fis = new FileInputStream(file1); 39 //第三步:利用流的对象调用方法,实现文件的读取 40 byte[] b = new byte[3]; 41 int len; 42 while ((len = fis.read(b))!=-1) { 43 for (int i = 0; i < len; i++) { 44 System.out.print((char)b[i]); 45 } 46 } 47 }catch (Exception e) { 48 e.getMessage(); 49 }finally { 50 if (fis!=null) { 51 try { 52 fis.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } 56 } 57 } 58 }
四、FileOutputStream
1 实现将内容写入一个文件中 2 @Test 3 public void Test3() { 4 //实现将某些内容写入一个文件中,文件可以不存在 5 //第二步:创建一个输出流对象 6 FileOutputStream fos = null; 7 try { 8 //第一步:创建一个文件对象 9 File file1 = new File("hello1.txt"); 10 fos = new FileOutputStream(file1); 11 fos.write(new String("hello1").getBytes()); 12 }catch (Exception e) { 13 System.out.println(e.getMessage()); 14 }finally { 15 if (fos != null) { 16 try { 17 fos.close(); 18 } catch (IOException e) { 19 e.printStackTrace(); 20 } 21 } 22 } 23 }
五、使用FileInputStream和FileOutputStream实现将一个文件内容复制到另一个文件中
1 @Test 2 public void Test4() { 3 //实现将一个文件的内容复制到另一个文件中 4 //第二步:创建相应的流 5 FileInputStream fis = null; 6 FileOutputStream fos = null; 7 try { 8 //第一步:创建文件对象 9 File file1 = new File("C:/Users/贾博蓉/code/Demo1/src/hello.txt"); 10 File file2 = new File("C:/Users/贾博蓉/code/Demo1/src/hello1.txt"); 11 fis = new FileInputStream(file1); 12 fos = new FileOutputStream(file2); 13 //第三步:调用方法完成复制 14 byte[] b = new byte[3]; 15 int len; 16 while((len=fis.read(b))!=-1) { 17 fos.write(b, 0, len); 18 } 19 }catch (Exception e) { 20 System.out.println(e.getMessage()); 21 }finally { 22 if(fis != null) { 23 try { 24 fis.close(); 25 }catch(Exception e) { 26 System.out.println(e.getMessage()); 27 } 28 } 29 if(fos != null) { 30 try { 31 fos.close(); 32 }catch(Exception e) { 33 System.out.println(e.getMessage()); 34 } 35 } 36 } 37 }
六、使用FileReader和FileWriter实现将一个文件内容复制到另一个文件中
1 @Test 2 public void Test5() { 3 //实现将一个文件的内容复制到另一个文件中 4 //第二步:创建相应的流 5 FileReader read = null; 6 FileWriter write = null; 7 try { 8 //第一步:创建文件的对象 9 File file1 = new File("C:\\Users\\贾博蓉\\code\\JAVA学习笔记\\基础\\第一章\\数组.txt"); 10 File file2 = new File("C:\\Users\\贾博蓉\\code\\JAVA学习笔记\\基础\\第一章\\数组副本.txt"); 11 read = new FileReader(file1); 12 write = new FileWriter(file2); 13 //第三步:调用方法实现复制 14 char[] b = new char[10]; 15 int len; 16 while ((len = read.read(b))!=-1) { 17 write.write(b, 0, len); 18 } 19 }catch (Exception e) { 20 System.out.println(e.getMessage()); 21 }finally { 22 if (write!=null) { 23 try { 24 write.close(); 25 }catch (Exception e) { 26 e.getMessage(); 27 } 28 } 29 if (read!=null) { 30 try { 31 read.close(); 32 }catch (Exception e) { 33 e.getMessage(); 34 } 35 } 36 } 37 }
七、字符流和字节流复制区别
字符流只能复制文本文件,一般用它处理文本文件
字节流可以复制文本文件、音频文件等
八、处理流之缓冲流
缓冲流比节点流速度快
1 @Test 2 public void Test6() { 3 //使用缓冲流BufferedInputStream和BufferedOutputStream完成文件的复制 4 //第二步:创建相应的缓冲流流对象 5 BufferedInputStream bis = null; 6 BufferedOutputStream bos = null; 7 try { 8 //第一步:创建相应的文件对象 9 File file1 = new File("C:\\Users\\贾博蓉\\code\\Demo1\\src\\hello.txt"); 10 File file2 = new File("C:\\Users\\贾博蓉\\code\\Demo1\\src\\hello2.txt"); 11 FileInputStream fis = new FileInputStream(file1); 12 FileOutputStream fos = new FileOutputStream(file2); 13 //第三步:将节点流对象作为形参构造缓存流对象 14 bis = new BufferedInputStream(fis); 15 bos = new BufferedOutputStream(fos); 16 //第四步:调用方法完成复制 17 byte[] b = new byte[3]; 18 int len; 19 while ((len = bis.read(b))!=-1) { 20 bos.write(b, 0, len); 21 bos.flush();//相比于节点流新加的 22 } 23 }catch (Exception e) { 24 System.out.println(e.getMessage()); 25 }finally { 26 if (bos!=null) { 27 try { 28 bos.close(); 29 }catch (Exception e) { 30 e.getMessage(); 31 } 32 } 33 } 34 if (bis!=null) { 35 try { 36 bis.close(); 37 }catch (Exception e) { 38 e.getMessage(); 39 } 40 } 41 } 42 @Test 43 public void Test7() { 44 //使用缓冲流BufferedReader和BufferedWriter完成文件的复制 45 //第二步:创建相应的缓冲流对象 46 BufferedReader bread = null; 47 BufferedWriter bwrite = null; 48 try { 49 //第一步:创建文件对象 50 File file1 = new File("C:\\Users\\贾博蓉\\code\\Demo1\\src\\hello.txt"); 51 File file2 = new File("C:\\Users\\贾博蓉\\code\\Demo1\\src\\hello3.txt"); 52 FileReader fis = new FileReader(file1); 53 FileWriter fos = new FileWriter(file2); 54 //第三步:将节点流对象作为缓冲流构造器的形参创建相应的缓冲流 55 bread = new BufferedReader(fis); 56 bwrite = new BufferedWriter(fos); 57 //第四步:调用方法,完成功能 58 String str; 59 while((str = bread.readLine())!=null) { 60 bwrite.write(str); 61 bwrite.newLine(); 62 } 63 }catch (Exception e) { 64 System.out.println(e.getMessage()); 65 }finally { 66 if (bwrite!=null) { 67 try { 68 bwrite.close(); 69 }catch (Exception e) { 70 e.getMessage(); 71 } 72 } 73 } 74 if (bread!=null) { 75 try { 76 bread.close(); 77 }catch (Exception e) { 78 e.getMessage(); 79 } 80 } 81 }
九、转换流InputStreamReader和OutputStreamWriter
功能:*输入时实现字节流到字符流的转换(文件是文本文件),使用InputStreamReader
*输出时实现字符流到字节流的转换,使用OutputStreamWriter
1 例子:从键盘输入字符串,要求将读取到的整行字符串转成大写输出,然后继续进行输入操作,直到当输入为“e”或者“exit”时退出程序 2 @Test 3 public void Test8() { 4 //从键盘输入字符串,要求将读取到的整行字符串转成大写输出,然后继续进行输入操作,直到当输入为“e”或者“exit”时退出程序 5 BufferedReader br = null; 6 try { 7 InputStream is = System.in;//字节流 8 InputStreamReader isr = new InputStreamReader(is);//转换为字符流 9 br = new BufferedReader(isr); 10 while(true) { 11 System.out.print("请输入字符串:"); 12 String str = br.readLine(); 13 if(str.equals("e")||str.equals("exit")) { 14 System.out.println("退出程序"); 15 break; 16 }else { 17 System.out.println(str.toUpperCase()); 18 } 19 } 20 } catch (Exception e) { 21 e.printStackTrace(); 22 }finally { 23 if(br!=null) { 24 try { 25 br.close(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 } 31 }
十、打印流PrintStream(字节流)和PrintWriter(字符流)
十一、数据流DateInputStream和DateOutputStream,目的是更方便地操作基本数据类型
十二、对象流ObjectInputStream和ObjectOutputStream,目的是存储和读取对象
序列化:用ObjectOutputStream类将一个JAVA对象写入io流
反序列化:用ObjectInputStream类从io流中恢复该对象
序列化机制:把JAVA对象转换成和平台无关的二进制流,这个二进制流可以放在硬盘中,也可以通过网络传输到另一个网络节点。
序列化好处:可以把JAVA对象转换成字节数据来保存和传输
可以实现序列化地类要求:*这个类必须实现可序列化接口
*这个类中属性地类型必须实现可序列化接口
*这个类要提供一个序列号private static final serialVersionUID
*使用static和transient修饰地属性不能被序列化
十三、随机存取流RandomAccessFile
1 @Test 2 public void Test9() { 3 //用RandomAccessFile实现在一个文件指定位置插入 4 RandomAccessFile raf = null; 5 try { 6 File file = new File("C:\\Users\\贾博蓉\\code\\Demo1\\hello1.txt"); 7 raf = new RandomAccessFile(file,"rw"); 8 raf.seek(2); 9 byte[] b = new byte[2]; 10 int len ; 11 StringBuffer sb = new StringBuffer(); 12 while((len = raf.read(b))!=-1) { 13 sb.append(new String(b, 0, len)); 14 } 15 raf.seek(2); 16 raf.write(new String("xy").getBytes()); 17 raf.write(sb.toString().getBytes()); 18 } catch (FileNotFoundException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 }finally { 25 if (raf!=null) { 26 try { 27 raf.close(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 } 34 }