java基础笔记11 - IO流
十一 IO流
11.1 File类
java.io.File类 :一个对象代表一个文件或一个文件目录
构造器:
这里只是构造了一个对象,还没有涉及到硬盘读写操作,所以文件不存在也
//方法1
File file1 = new File("hello.txt");//相对路径
File file1 = new File("C:\\file\\t.o");//绝对路径,\\转义字符
//方法2
File file2 = new File("C:\\file","t.o");//new File(String parent,String child),一个指定目录,一个指定文件
File file2 = new File("C:\\file");//new File(String pathname),只创建了目录的File对象
//方法3
File file3 = new File(file2,"x.o");//File(File file,String child),使用第二种构造器对象的目录,然后再自己指定文件
路径分隔符取决于系统:windows和DOS使用 \ , unix和URL使用 /
方法:
读文档去。。。
11.2 IO流原理及流分类
 java中对于数据的IO,都是以stream 的方式进行
 java.io包下提供了各种stream类和接口,但是提供了统一的标准方法
分类:
按照数据单位的不同:字节流8 bit,字符流16 bit
流的角色不同:节点流(基础的流),处理流(在节点流的基础上包一层处理)(下面四个基类看作用在这两种流的哪一种上,就称为啥流)(不是一个考虑维度的)
| 抽象基类 | 字节流 (操作的基本单位Byte) | 字符流( 操作的基本单位char) | 
|---|---|---|
| 输入流 | InputStream | Reader | 
| 输出流 | OutputStream | Writer | 
IO流涉及到了40多个类,但是都是从以上4个抽象基类派生的,命名规则都是 Xxxx基类名
例如访问文件的FileInputStream(可以看成节点流)
访问数组的,访问管道的,访问字符串的,缓冲流,转换流,对象流,打印流,回推输入流...........(处理流)
基本读操作
@Test
public void testFileReader()  {//能用try catch就别throws,原因:可能中途的异常导致最后的close方法未执行
    FileReader fr = null;
    try {
        //1 创建文件对象
        File file = new File("hello");//相较于当前Module
        //如果是main方法则是相对于当前工程
        System.out.println(file.getAbsoluteFile());
        //2 提供流
        fr = new FileReader(file);
        //3 数据读入
        //read(),返回读入的一个字符,达到文件尾返回-1
        int data = fr.read();
        while (data != -1){
            System.out.print((char)data);
            data=fr.read();//i++
        }
        //方式2
        //while((data=fr.read())!=-1){....}
        
         //方法3
        char[] cbuf=new char[5];
        int len;
        while((len = fr.read(cbuf))!=-1){
            for(int i=0;i<len;i++){
                System.out.print(cbuf[i]);
            }
        }
        //方法4
        String str =new String(cbuf,0,len);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4 流的关闭!!!!
        try {
            if(fr!=null){//防止File就没创建成功
                fr.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
基本写操作(到文件)
文件不存在会自动创建,存在则覆盖
/*
从内存中写出文件到文件
*/
@Test
public void testFileWriter() throws IOException {
    File file =new File("hello.out");
    FileWriter fw = new FileWriter(file,true);//在原有文件上追加写,w+
    fw.write("i have a dream\n");
    fw.write("llalalalala");
    fw.close();
}
11.3 缓冲流
BufferedReader BufferedWriter BufferedOutputStream BufferedInputStream
11.4 转换流
输入字符流的范畴内
字节流和字符流之间的转换,
字节输入流 ----> InputStreamReader(字符输入流) --> 字符输入流
字符输出流 -----> OutputStreamWriter(字符输出流)---->字节输出流
try{
    InputStreamReader isr = new InputStreamReader(new FileInputStream("hello.txt"),"UTF-8");
    char[] cbuf = new char[20];
    int len;
    while((len=isr.read())!=-1){
        String str =new String(cbuf,offset:0,len);
        sout(str);
    }
}catch(IOException e){
    e.print;
}final{
    if(isr!=null)
    	isr.close();
}
/*
    将utf-8文件字节流读入,转化为字符流,在转换为字节流gbk保存
*/
public void test2(){
    InputStreamReader isr = null;
    OutputStreamWriter osw = null;
    try {
        isr = new InputStreamReader(new FileInputStream(new File("hello.out")),"utf-8");
        osw = new OutputStreamWriter(new FileOutputStream(new File("hello_gbk.out")),"gbk");
        char[] cbuf =new char[20];
        int len;
        while ((len=isr.read(cbuf))!=-1){
            osw.write(cbuf,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (osw != null) {
            try {
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
11.5 标准输入输出流
11.6 打印流
11.8 对象流
麻蛋看到IO流这章就一点动力都没了。。。
全部跳过
本文来自博客园,作者:荧惑微光,转载请注明原文链接:https://www.cnblogs.com/yinghuoweiguang/p/15767287.html
 
                    
                
 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号