java I/O

 文件字符流

字符流不同于字节,字符流是以一个具体的字符进行读取,因此它只适合读纯文本的文件,如果是其他类型的文件不适用。

字节流;英文1个字节,中文3个字节。 字符流:中英文都是2个字节

    public static void main(String[] args){
        try (FileReader reader = new FileReader("test.txt")){
            System.out.println((char) reader.read());
        }catch (IOException e){
            e.printStackTrace();
        }

        try (FileWriter writer = new FileWriter("test.txt")){
            writer.write("eeee");
        }catch (IOException e){
            e.printStackTrace();
        }

        //拷贝
        try (FileReader reader1 = new FileReader("test.txt");
             FileWriter writer1 = new FileWriter("test1.txt")){
            char[] chars = new char[3];
            int len;
            while ((len=reader1.read(chars))!= -1){
                writer1.write(chars,0,len);
            }
         }catch (IOException e){
            e.printStackTrace();
        }

    }
}

文件对象

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

        //文件对象
        File file = new File("test.txt");
        System.out.println(file.exists()); //判断文件是否存在
        System.out.println(file.getAbsoluteFile());  //获取绝对路径
        file.createNewFile();  //创建文件
        file.length();  //获取字节长度

        File file1=new File("hello/test2");
        System.out.println(file1.mkdirs()); //创建文件夹

    }
}

    public static void main(String[] args){
        File file=new File("Honor(1).mp3");
        try (FileInputStream in = new FileInputStream("Honor(1).mp3");
             FileOutputStream out =new FileOutputStream("xxx.mp3")){
            byte[]bytes=new byte[1024*1024];
            int len;
            long total =file.length(),sum=0;
            while ((len=in.read(bytes))!=-1){
                out.write(bytes,0,len);
                sum+=len;
                System.out.println("文件已拷贝"+(sum*100/total)+"%");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

/*输出
文件已拷贝10%
文件已拷贝21%
文件已拷贝31%
文件已拷贝42%
文件已拷贝52%
文件已拷贝63%
文件已拷贝73%
文件已拷贝84%
文件已拷贝95%
文件已拷贝100%
*/

缓冲流

当调用mark()之后,输入流会以某种方式保留之后读取的readlimit数量的内容,当读取的数量超过readlimit则之后的内容不会被保留,当调用reset()之后,会使得当前的读取位置回到mark()调用时的位置。

        try (BufferedInputStream stream = new BufferedInputStream(new FileInputStream("test.txt"))){
            stream.mark(0);
            System.out.print((char) stream.read());
            System.out.print((char) stream.read());
            System.out.println((char) stream.read());
            stream.reset();
            System.out.print((char) stream.read());
            System.out.print((char) stream.read());
            System.out.print((char) stream.read());
            System.out.print((char) stream.read());
        }catch (IOException e){
            e.printStackTrace();
        }

转换流

读取的是一个字符串或一个个字符,但是只能往一个OutputStream里输出,但是OutputStream只支持byte类型,如果要往里面写入内容,进行数据转换就会很麻烦,可以使用转换流使之简洁:

        try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("test.txt"))){
            writer.write("aaabbbbccc");
        }catch (IOException e){
            e.printStackTrace();
        }

同样的,现在只拿到一个InputStream,希望能按照字符否方式读取:

        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("test.txt")))){
            System.out.println(reader.readLine());
        }catch (IOException e){
            e.printStackTrace();
        }

你可能感兴趣的:(java,apache,intellij-idea)