编码表、字节流、字节缓冲流

概念: 

       字节流不需要刷新(byte)

    字符流需要刷新(char)

    IO流:input(通过Java程序往内存里读取) output(通过Java程序往硬盘上外写出) 输入输出流

 * IO流的分类

 *          按照操作的数据不同

 *                 字节流:操作字节,由于计算机上一切皆是字节,字节流可以操作一切数据

 *                 字符流:操作字符,只能操作文本文件

 *                 文本文件:能使用文本工具 打开 看懂的叫做文本文件 txt java html xml css。(  world excel是文本文件吗? 不是文本)

 *          按照流向分类

 *                 字节流

 *                        字节输入流  InputStream read  读一个字节 读一个字节数组

 *                                      FileInputStream

 *                                      BufferedInputStream

                            BufferedInputStream infile=new BufferedInputStrean(new FileInputStream("文件路径") );

 *                        字节输出流 OutputStream write 写一个字节 写一个字节数组 写字节数组的一部分

 *                                    FileOutputStream

 *                                    BufferedOutputStream

 *                 字符流

 *                        字符输入流 Reader read 读一个字符  读一个字符数组

 *                                    FileReader

 *                                     BufferedReader

 *                        字符输出流 Writer write 写一个字符 写一个字符数组 写字符数组的一部分 写字符串

 *                                    FileWriter

 *                                    BufferedWriter

 * java.io.OutputStream 所有字节输出流的抽象超类

 *           方法

 *                        write(int b) 写一个字节

 *                        write(byte[] byte) 写一个字节数组

 *                        write(byte[] bytes,int offset, int length) 写字节数组的一部分

 *                        close() 关闭资源

 *

 *          常用子类

 *                         FileOutputStream

 *                 构造方法绑定数据目的

 *                        FileOutputStream(File file)

 *                        FileOutputStream(String name)

 *                 参数

 *                        File对象

 *                        String路径

 *                        IO流对象本身没有读写功能,需要调用操作系统的读写功能 完成读写

 *                         在调用后,需要关闭资源

       文件的续写与换行

 *   续写

 *          FileOutputStream类的构造方法

 *                 FileOutputStream(File file, boolean append)

 *                 FileOutputStream(String name, boolean append)

 *                 当参数 append为true时 则续写  为false则覆盖 不加append默认false

 java.io.InputStream 所有字节输入流的超类

 *          方法

 *                        int read() 读一个字节

 *                        int read(byte[] bytes) 读一个字节数组

 *          常用子类

 *                              FileInputStream

 *                       构造方法绑定数据源

 *                              FileInputStream(File file)

 *                              FileInputStream(String name)

 *                       参数

 *                              File对象

 *                              String路径

 * int read(byte[] bytes) 读一个字节数组

 *

 *                 返回值

 *                        int 读取的有效字节数(返回的是有效个数)

 java.io.BufferedOutputStream extends OutputStream 字节输出缓冲流

 *                 可以使用所有OutputStream的方法  write 写一个字节 一个字节数组 一个字节数组的一部分

 *   构造方法

 *                        BufferedOutputStream(OutputStream out)

 *                 参数

 *                        OutputStream out:所有字节输出流的超类 ,抽象类不能创建对象,需要传入其子类对象

 *                                                     FileOutputStream

 java.io.BufferedInputStream extends InputStream 字节输入缓冲流

 *                 可以使用所有InputStream的方法 read 读一个字节 读一个字节数组

 *   构造方法

 *                 BufferedInputStream(InputStream in)

 *          参数

 *                 InputStream in:所有字节输入流的超类,抽象类不能创建对象 ,需要传入其子类对象

 *                                         FileInputStream

java.util.Properties extends Hashtable  implements Map

 *          键和值都是字符串类型 唯一一个可以和IO流直接结合使用的集合

 *

 *   方法

 *                 setProperty(String key,String value) 添加元素  put(K key ,V value)

 *                 String getProperty(String key) 根据键找值

 *                 Set<String> stringPropertyNames() 获取所有键的set集合

             <读取文件>和IO流结合使用

 *                      load(InputStream in) 将文件中的键值对加载进集合

 *                 load(Reader in)

 *         参数  任意输入流对象

              <写入文件>

 *                       store(OutputStream out ,String com) 将集合中的数据 写入到配置文件中

 *                       store(Writer out ,String com)

 *         参数  任意输出流对象       

<复制文件例子复制(H:\\删除添加练习使用文件1)到(H:\\删除添加练习使用文件2)>

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class CopyDir {

       public static void main(String[] args) throws IOException {

              File src = new File("H:\\删除添加练习使用文件1");

              File dest = new File("H:\\删除添加练习使用文件2");

              copyDir(src,dest);

       }     

       public static void copyDir(File srcDir,File destDir) throws IOException{

              //确定文件夹的数据源和数据目的

              //获取数据源的文件夹名称

              String srcDirName = srcDir.getName();

              //确定文件夹的数据目的

              //使用数据目的+数据源的文件夹名称 拼出最终的数据目的

              File destDir2 = new File(destDir,srcDirName);

             

              System.out.println("--------复制文件--------------");

              if(srcDir.isDirectory()){

              //创建数据目的文件夹

              destDir2.mkdirs();

              //获取数据源文件夹的所有文件

              File[] srcFileArr = srcDir.listFiles();

              //遍历数组 依次获取到每个数据源文件

              for(File srcFile: srcFileArr){

                     //确定数据目的的文件

                     //获取数据源的文件名

                     if(srcFile.isDirectory()){

                            copyDir(srcFile, destDir2);

                     }else{

                     String srcFileName = srcFile.getName();

                     //使用数据目的的文件夹 + 源文件的文件名 确定数据目的的文件

                     File destFile = new File(destDir2,srcFileName);

                     //复制文件

                     copy(srcFile,destFile);

              }

              }

              }else{

                     String srcFileName = srcDir.getName();

                     //使用数据目的的文件夹 + 源文件的文件名 确定数据目的的文件

                     File destFile = new File(destDir2,srcFileName);

                     //复制文件

                     copy(srcDir,destFile);

              }

       }     

       public static void copy(File src,File dest) throws IOException{

              //创建字节缓冲输入流对象,绑定字节输入流,绑定数据源

              BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));

              //创建字节缓冲输出流对象,绑定字节输出流,绑定数据目的

              BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));

              byte[] bytes = new byte[1024];

              int len = 0;

              while((len = bis.read(bytes))!=-1){

                     bos.write(bytes, 0, len);

              }

             

             

              //关闭资源

              bos.close();

              bis.close();   

       }

}

<文件复制例子二>

package Test_03_03;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileFilter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.HashMap;

import java.util.Random;

import java.util.Scanner;

import java.util.Set;

/*

 * 从控制台获取输入的文件目录然后将该目录(包含子目录)下的.java文件复制到D:/java文件夹中,并统计java文件的个数.

提示:如果有相同的名称的文件,如果两个Test01.java,则拷贝到目标文件夹时只能有一个Test01.java,

另一个Test01.java要修改为另一个名称:该名称可随机生成,只要不重复即可.

 */

public class Test {

       static int sum=0;

       public static void main(String[] args) throws IOException {

              Scanner sc=new Scanner (System.in);

              System.out.println("请输入要访问的文件目录:");

              String str=sc.nextLine();

              //根据输入的目录创建文件

              File file1=new File(str);

              File file2=new File("H:\\删除添加练习使用文件2");

              file2.mkdirs();

              copyPath(file1,file2);

       }

       //文件路径复制

       public static void copyPath(File file1,File file2) throws IOException{

              if(file1.isDirectory()){

                     File[] file=file1.listFiles(new FileFilter(){

                            @Override

                            public boolean accept(File pathname) {

                                   //判断如果是以

                                   if(pathname.getName().endsWith(".java")||pathname.isDirectory()){

                                          return true;

                                   }

                                   return false;

                            }

                           

                     });

                     //如果是文件夹添加后遍历进行文件存储

                     for (File file3 : file) {

                            if(file3.isDirectory()){

                                   copyPath(file3,file2);

                            }else{

                                   String str2=file3.getName();

                                   File file11=new File(file2,str2);

                                   //如果同名随机一个名字递归直到不重名

                                   while(file11.exists()){

                                          file11 = new File(file2,new Random().nextInt(100000) + str2);

                                   }

                                  

                                   //file11.mkdir();

                                   System.out.println(file11);

                                   copy(file3,file11);

                            }

                     }

              }else{

                     if(file1.getName().endsWith(".java")){

                            String str2=file1.getName();

                            File file11=new File(file2,str2);

                            copy(file1,file11);

                     }else{

                            System.out.println("你输入的不是文件夹:");

                     }                   

              }

       }

       public static void copy(File file1,File file2) throws IOException{

              //创建文件高效写流

              BufferedOutputStream outfile=new BufferedOutputStream(new FileOutputStream(file2));

              //创建文件高效读取流

              BufferedInputStream infile=new BufferedInputStream(new FileInputStream(file1));

              byte[] b=new byte[1024];

              int len=0;      

              while((len=infile.read(b))!=-1){

                     outfile.write(b,0,len);

              }

              outfile.close();

              infile.close();

             

              sum++;

       }

}

按照流向分:输入流与输出流,每个IO流对象均要绑定一个IO资源

分类关系如下:

       字节输入流 InputStream 抽象类

                     FileInputStream 操作文件的字节输入流

       字节输出流 OutputStream 抽象类

                     FileOutputStream 操作文件的字节输出流

       字符输入流 Writer写入字符流的抽象类

                     FlieWriter操作文件的字符输入流

       字符输出流 Reader用于读取字符流的抽象类

                     FileReader操作文件的字符输入流

       缓冲流,根据流的分类分类字节缓冲流与字符缓冲流。

                     字节缓冲流:BufferedInputStream

                               BufferedOutputStrea

                     字符缓冲流:BufferedReader

                               BufferedWriter

       <字节流不需要刷新字符流需要>

       按照传输方式:分为字节流和字符流

                         字符流  按照字符的方式读写

                         字节流  按照字节的方式读写

       定义一个缓冲区提高效率

                     字节型 byte[]  bytes=new byte[1024];

                     字符型 char[]  ch=new char[1024];         

猜你喜欢

转载自www.cnblogs.com/JunQiang-Ma/p/9962442.html