一:File类:
1.File类的理解:
1.File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
2.File类声明在java.io包下
3.File类中涉及到关于文件或文件目录的创建,删除,重命名,修改时间,大小等方法
并涉及到写入或读取文件 内容的操作。如果需要读取或写入文件内容,必须使用IO流来完成
4.后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的“终点”。
2.File类的实例化
2.1 常用构造器
File(String filePath)
File(String parentPath,String childPath)
File(File parentFile, String chilPath)
2.2 路径的分类
相对路径:相较于某个路径下的,指明的路径
绝对路径: 包含盘符在内的文件或文件目录的路径
说明:如果大家开发中使用JUnit中的单元测试方法测试,相对路径即为当前Module下。
如果大家使用main()测试,相对路径为当前project 下
2.3 路径分隔符
windows:\\
unix:/
public void test1(){
    //构造器1:相对路径
    File file = new File("hello.txt");
    System.out.println(file);
    //构造器1:绝对路径
    File file1 =new File("D:\\java\\TestDemo1\\h1.txt");
    System.out.println(file1);
    //构造器2
    File file2 =new File("D:\\java\\TestDemo1","filetest");
    System.out.println(file2);
    //构造器3
    File file3 =new File(file2,"h3.txt");
    System.out.println(file3);
} 
3.File类的常用方法
file类的获取功能
public String getAbsolutePath():获取绝对路径
public String getPath():获取路径
public String getName():获取名称
public String getParent():获取上层文件目录的路径。若无返回null
public long length():获取文件长度(即:字节数)。不能获取目录的长度
public long lastModifiled():获取最后一次的修改时间,毫秒值
public String [] list(): 获取指定目录下所有文件或者文件目录的名称数组
public File() listFiles():获取指定目录下的所有文件或文件目录的File数组
file类的重命名功能
public boolean renameTo(File dest): 把文件重命名为指定的文件路径
file类的判断功能
public boolean isDirectory(): 判断是否是文件目录
public boolean isFile(): 判断是否是文件
public boolean exists():判断是否存在
public boolean canRead(): 判断是否可读
public boolean canWrite(): 判断是否可写
public boolean isHidden(): 判断是否可读
file类的创建功能
public boolean createNewFile(): 创建文件。若文件存在,则不创建,返回flase
public boolean mkdir():创建文件目录。如果此文件目录存在,就不创建了。如果
此文件目录的上次目录不存在,也不创建
public boolean mkdirs():创建文件目录。如果此文件目录的上次目录不存在,一并创建
注意事项: 如果你创建文件或文件目录没有写盘符路径,那么默认在项目路径下
file类的删除功能
public boolean delete():删除文件或者文件夹:
注意事项:
java 中删除不走回收站。 要删除一个文件目录,请注意该文件目录内不能包含文件或文件目录
代码示例:
@Test
public void test5(){
    File file2 =new File("D:\\java\\TestDemo1\\IOtest","filetest");
    if(!file2.exists()){
        file2.mkdirs();
        System.out.println("创建成功!");
    }else{
        file2.delete();
        System.out.println("删除成功!");
    }
}
@Test
public void test6() {
    //判断指定目录是否由以.jpg 结尾的文件,如果有输出文件名称
    File file = new File("D:\\java");
    String[] list= file.list();
     for(String str: list){
         if(str.endsWith(".jpg")){
             System.out.println("以jpg结尾的文件是:"+str);
         }
     }
} 
二:IO流概述:
1.流的概述
》操作数据单位:字节流,字符流
》数据的流向:输入流,输出流
》流的角色;j节点流,处理流
2.流的体系结构

3. 重点说明的几个流结构
抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[]buffer))
OutputStream FileOutputStream(write(byte[] buffer,0,len)) BufferedOutputStream(write(byte[] buffer,0,len))
Reader FileReader(read(char[] cbuf)) BufferedReader(read(char[] cbuf) /readline()
Writer FileWriter(write(char[] cbuf,0,len)) BufferedWriter(write(char[] cbuf,0,len))
4.输入,输出的标准过程
4.1输入过程
①创建FIle的对象,指明读取的数据来源。(要求文件一定存在)
②创建相应的输入流,将File类的对象作为参数,传入流的构造器中
③具体的读入过程
创建相应的byte[]或char[]
④关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理
4.2输出过程
①创建FIle的对象,指明写出的数据来源。(不要求文件一定存在)
②创建相应的输出流,将File类的对象作为参数,传入流的构造器中
③具体的写出过程
write(char[]/byte[],0,len)
④关闭流资源
说明:程序中出现的异常需要使用try-catch-finally处理
三:节点流:
1.FileReader/FileWriter的使用
1.1 FileReader的使用
说明点:
1.read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1;
2.异常的处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
3.读入的文件一定要存在,否则就会报FileNotFoundException
@Test
public void test1() {
    //创建文件
    File file = null;
    FileReader fr = null;
    try {
        file = new File("hello.txt");
        /* 创建字符输入流 */
        
        fr = new FileReader(file);
        int data;
        //每次读取一个字符并返回,当读到末尾,返回-1
        while ((data = fr.read()) != -1) {
            System.out.print((char) data);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    //关闭字符流
    catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            fr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
@Test
public void test2() throws IOException {
    //创建文件
    File file = new File("hello.txt");
    //创建输入流
    FileReader fr = new FileReader(file);
    char[] cbuff = new char[5];
    int len;
    //读入文件,返回读取个数,读取完成返回-1
    while ((len = fr.read(cbuff)) != -1) {
        String str = new String(cbuff, 0, len);
        System.out.print(str);
    }
    fr.close();
}
 
1.2 FileWriter的使用
说明点:
1.输出操作,对应的File可以不存在的。并不会报异常
2.
File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件
File对应的硬盘中的文件如果存在,
如果流使用的构造器是:FileWriter(file,false):对原文件的覆盖
如果流使用的构造器是:FileWriter(file,true): 不会对原文件覆盖,而是在 原文件基础上,追加内容
@Test
public void test3() {
    FileWriter fw = null;
    try {
        //创建文件
        File file = new File("hello1.txt");
        //创建输出流
        fw = new FileWriter(file);
        //写出内容
        fw.write("hello");
        fw.write("world");
        fw.write("123");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //关闭流
        try {
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 
1.3 文本文件的复制:
@Test
public void test4() {
    //创建文件
    FileReader fr = null;
    FileWriter fw = null;
    try {
        File file = new File("hello.txt");
        File file2 = new File("hello2.txt");
        //创建输入,输出流
        fr = new FileReader(file);
        fw = new FileWriter(file2);
        //拷贝文件
        char[] cbuff = new char[5];
        int len;
        while ((len = fr.read(cbuff)) != -1) {
            fw.write(new String(cbuff, 0, len));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
    //关闭流
    try {
        if( fw!=null)
        { fw.close();}
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if( fr!=null)
            {  fr.close();}
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}InputStream
 
2.FileInputStream/FileOutputStream的使用
1.对文本文件(.txt,.java , .c, .cpp)使用字符流处理
2.对于非文本文件(.jpg, .mp3,.mp4,.avi, .doc,.ppt....)使用字节流处理
@Test
public void test5() {
    /*
    字节流实现图片复制
     */
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //创建文件
        File file = new File("cat.jpg");
        File file2 = new File("cat2.jpg");
        //创建字节流
        fis = new FileInputStream(file);
        fos = new FileOutputStream(file2);
        //读写文件
        int len;
        byte[] bytes = new byte[10];
        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
    //关闭流
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (fos != null)
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}
 
//封装复制方法
public void InputOutputCopyFile(String srcpath, String destpath) throws IOException {
    //创建文件
    File file = new File(srcpath);
    File file2 = new File(destpath);
    //创建字节流
    FileInputStream fis = new FileInputStream(file);
    FileOutputStream fos = new FileOutputStream(file2);
    //读写文件
    int len;
    byte[] bytes = new byte[1024];
    while ((len = fis.read(bytes)) != -1) {
        fos.write(bytes, 0, len);
    }
} 
 
相对路径在idea和eclipse的区别:
idea:
如果使用单元测试方法,相对路径基于当前的Module
如果使用main()测试,相对路径基于当前project
eclispe:
单元测试方法还是main()测试,相对路径基于当前project
四:处理流:
4.1 缓冲流
4.1.1 缓冲流涉及到的类:
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
4.1.2作用:

4.1.3典型代码:
》使用BufferedInputStream 和BufferedOutputStream
@Test
public void test7() {
    /*
    缓冲流实现图片复制
     */
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        //创建文件
        File file = new File("cat.jpg");
        File file2 = new File("cat2.jpg");
        //创建字节流
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file2);
        //创建处理流
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);
        //读写文件
        int len;
        byte[] bytes = new byte[10];
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
    //关闭流
    if (bis != null) {
        try {
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (bos != null)
        try {
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
}
 
》使用BufferedReader 和BufferedWriter
  @Test
    public void test8() throws IOException {
          /*
        缓冲字符流实现文本复制
         */
        //创建文件对象
        File  file = new File("hello.txt");
        File  file2 = new File("hellocp.txt");
        //创建字符流对象
        FileReader  fr = new FileReader(file);
        FileWriter  fw  = new FileWriter(file2);
        //创建处理流
        BufferedReader  bur  = new BufferedReader(fr);
        BufferedWriter  buw   =new BufferedWriter(fw);
        //读写文件 方式一:
//
//        char[] buff =new char[1024];
//         int len;
//         while((len=bur.read(buff))!=-1 ){
//             buw.write(buff,0,len);
//
//
//        }
        //读写文件 方式二:
        char[] buff =new char[1024];
         String str;
        while((str=bur.readLine())!=null){
           //buw.write(str+"\n");
            buw.write(str);
            buw.newLine();
        }
        //关闭流
        bur.close();
         buw.close();
    }
 
4.2转换流
4.2.1 转换流涉及到的类
InputSteamReader:将一个字节的输入流转换为字符的输入流
解码:字节,字节数组----》字符数组,字符串
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
编码:字符数组,字符串----》字节,字节数组
4.2.2 作用
提供字节流与字符流之间的转换
4.2.3 图示

4.2.4 典型实现
@Test
public void test(){
    //创建文件
    InputStreamReader isr = null;
    OutputStreamWriter osw = null;
    try {
        File file = new File("hello.txt");
        File file2 = new File("hellogbk.txt");
        //创建字节流
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file2);
        //创建转换流
        isr = new InputStreamReader(fis,"utf-8");
        osw = new OutputStreamWriter(fos,"gbk");
        char[] ch= new char[10];
        int len;
        while((len=isr.read(ch))!=-1){
            osw.write(ch,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    //关闭流
        try {
            if(isr!=null)
            isr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if(osw!=null)
            osw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
 
五:其他流:
1.标准的输入输出流
System.in:标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
修改默认的输入和输出行为:
System类的SetIn(InputStrem is)/setOut(PrintStream ps)方式重新指定输入和输出流
2.打印流
PrintStream 和PrintWriter
说明:
》提供了一系列重载的print()和println()方法,用于多种数据类型的输出
》System.out返回的是PrintStream的实例
3.代码
1.实现键盘标准输入字符串,并转换成大写输出! 方法一:Scanner
public class ScannerTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(true){
            System.out.println("请输入内容,输入e或者exit 结束输入!");
            String next = scan.next();
            if("e".equalsIgnoreCase(next)||"exit".equalsIgnoreCase(next)){
                System.out.println("系统输入结束!");
                break;
            }
            System.out.println(next.toUpperCase());
        }
    }
}
 
方法二:缓冲流BufferedReader
public class StandardTest {
    public static void main(String[] args) {
        BufferedReader  bur = null;
        try {
            InputStreamReader fir =new  InputStreamReader(System.in);
            bur = new BufferedReader(fir);
            while(true){
                System.out.println("请输入内容,输入e或者exi退出:");
                String str = bur.readLine();
                if("e".equalsIgnoreCase(str)||"exit".equalsIgnoreCase(str)){
                    System.out.println("退出程序!");
                    break;
                }
                String toUpperCase = str.toUpperCase();
                System.out.println(toUpperCase);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bur!=null){
                try {
                    bur.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
2.读取文档,并统计字符出现的次数
public class WordCount {
    @Test
    public void test() throws IOException {
      
        //创建流和map集合
        FileReader  fr = new FileReader(new File("hello.txt"));
        BufferedWriter buw = new BufferedWriter(new FileWriter(new File("wordcount.txt")));
        Map<Character,Integer> map =new HashMap<Character,Integer>();
        //按字符读取文件,每一个字符出现的次数放到map中
        int c;
        while(( c=fr.read())!=-1){
            char ch = (char)c;
            if(map.get(ch)==null)
            { map.put(ch,1);}
            else{
                map.put(ch,map.get(ch)+1);
            }
        }
     //把集合数据写到文件中
        Set<Map.Entry<Character, Integer>> entries = map.entrySet();
        for(Map.Entry<Character, Integer> entry: entries){
            switch(entry.getKey()){
                case ' ':
                    buw.write("空格="+entry.getValue());
                    break;
                case '\t':
                    buw.write("制表符="+entry.getValue());
                    break;
                case '\r':
                    buw.write("回车="+entry.getValue());
                    break;
                case '\n':
                    buw.write("换行="+entry.getValue());
                    break;
                default:
                    buw.write(entry.getKey()+"="+entry.getValue());
                    break;
            }
            buw.newLine();
        }
            //关闭流:
            fr.close();
            buw.close();
    }
}
 
                


















