栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java基础IO流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java基础IO流

IO流IO流File输入字节流FileInputStream 和 输出字节流FileOutputStreamBufferedInputStream BufferedOutputStream字符BufferedReader BufferedWriter字符,从控制台输入,一行一行复制到文档总结:

IO流 IO

输入(input)与输出(output)

Java的IO主要包含三个部分:

流式部分――IO的主体部分

非流式部分――主要包含一些辅助流式部分的类

文件读取部分的与安全相关的类以及与本地操作系统相关 的文件系统的类

Java中的流操作分为两种

基于字节流(InputStream读取,OutputStream写入)

字符流(Reader读取,Writer写入)

Java IO流可以概括为:两个对应、一个桥梁(字节流向字符流的转换)。两个 对应指字节流(Byte Stream)和字符流(Char Stream)的对应,输入流和输出流的对应。一个桥 梁指从字节流到字符流的桥梁(字节流向字符流的转换)

使用的时候都是它们的子类

 

File

对文件和文件夹做操作

​
import java.io.File;
import java.io.IOException;
​

public class FileDemo {
    public static void main(String[] args) {
​
        File file1=new File("love.md");
        File file2=new File("d:\love.md");
        File file3=new File("d:\love");
        File file4=new File("d:\love\ai");
​
        try {
           boolean f1= file1.createNewFile();
           boolean f2= file2.createNewFile();
           boolean f3= file3.mkdir();
           boolean f4= file4.mkdirs();
​
           if(f1 && f2 && f3 && f4){
               System.out.println("ok");
           }
​
​
        } catch (IOException e) {
            e.printStackTrace();
        }
​
​
    }
​
}
​
输入字节流FileInputStream 和 输出字节流FileOutputStream

FileInputStream提供了测试文件大小的方法 available(),提供了关闭流的方法close(),使用 read()方法从数据源中读取数据。可以使用缓冲区, 通过指定byte[]b的大小来提高效率——如果不设计 缓冲区,那么只能一个字节一个字节的读取,效率太 低

FileOutputStream类中提供了三种写入数据的write() 方

​
import java.io.*;
​

public class FileStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis=null;
        FileOutputStream fos=null;
        try {
            fis=new FileInputStream("d:\知鬼而上.png");
            fos=new FileOutputStream("d:\zgrs.png");
​
            byte []b= new byte[1024];
            int lenght=0;
            while ((lenght=fis.read(b))!=-1){
               fos.write(b);
​
            }
            fos.flush();
​
​
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
​
​
    }
​
}
BufferedInputStream BufferedOutputStream

​
import java.io.*;
​

public class BufferedReaderDemo {
    public static void main(String[] args) {
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
​
        try {
            bis=new BufferedInputStream(new FileInputStream(new File("d:\知鬼而上.png")));
            bos=new BufferedOutputStream(new FileOutputStream(new File("d:\zgrs.png")));
            byte b[]=new byte[1024];
            int lenght=0;
            while ((lenght=bis.read(b))!=-1){
                bos.write(b);
​
            }
            bos.flush();
​
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
​
​
    }
}
​
字符BufferedReader BufferedWriter
package com.exercises.lesson17;
import java.io.*;
​

public class BufferedDemo {
    public static void main(String[] args) {
        BufferedReader br=null;
        BufferedWriter bw=null;
        try {
            br=new BufferedReader(new FileReader("d:\love.txt"));
            bw=new BufferedWriter(new FileWriter("d:\no.txt"));
​
            String temp=null;
            while ((temp=br.readLine())!=null){
                bw.write(temp);
                bw.newline();
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
​
        }
​
​
    }
}
​

字符,从控制台输入,一行一行复制到文档
package com.exercises.lesson17;
​
import java.io.*;
​

public class Zjzzw {
    public static void main(String[] args) {
        BufferedReader br=null;
        BufferedWriter bw=null;
​
        br=new BufferedReader(new InputStreamReader(System.in));
        try {
            bw=new BufferedWriter(new FileWriter("d:\niubi.txt"));
            String temp=null;
            while ((temp=br.readLine())!=null){
                    if (temp.equals("end")){
                        break;
                    }
                bw.write(temp);
                bw.newline();
            }
            bw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bw.close();
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

总结:

File类的用来创建文件、文件夹,并实现对它们属性 的读取及修改

字节流InputStream、OutputStream可以用来读取 二进制文件

字符流Reader、Writer能够高效的读取文本文件

Java提供了字节流向字符流的转换: InputStreamReader、OutputStreamWriter

PrintWriter能够对通往输出流的数据进行格式化

<-个人笔记->

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358508.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号