Java基础-IO

来源:互联网 发布:网络英语翻译 编辑:程序博客网 时间:2024/06/06 04:43

:想象成管道,数据信息通过“管道”在进程、数据源之间交换数据

Java.io包中定义了多个流类型来实现 输入/输出 功能
分为:输入/输出流(以进程角度描述,程序获得数据是“读”,输出数据是“写”)
字节/字符流(数据单位,1字符=2字节,中文字是2字节)
节点/处理流节点:从数据源直接读写数据;处理:“连接”在已存在的流(节点流或处理流)之上,通过对数据处理为程序提供更强大读写功能)

四个“祖宗”:字节流InputStream、OutputStream;字符流:Reader、Writer

InputStream基本方法
读:int read() throws IOException
可选参数 ,byte[] buffer, byte[] buffer int offset int length 从offset位置开始,读取length长度的字节,存储到字节数组buffer
关闭流释放内存:void close() throws IOException

OutputStream基本方法
写:void write() throws IOException
可选参数 int b, byte[] buffer, byte[] buffer int offset int length 写参数b的低8位,将字节数组写入输出流
将输出流中缓冲数据全部写出到目的地:void flush() throws IOException
关闭流释放内存:void close() throws IOException

Reader基本方法:同InputStream,byte[] buffer换为 char[] buffer

Writer基本方法:同上,外加void write()参数 String s, String s int offset int length

数据读取的结果都是0和1组成的字符,可以先用int a = 读取的内容,再(char) a强行转换为字符。注意一个中文占两个字节,用字节流会乱码

节点流类型
这里写图片描述
注意File类型的输出流,构造时都可以加上参数true,表示追加
例如:

FileReader fr = new FileReader(URL); //读文件while((b = fr.read()) != -1){    System.out.print((char)b); //将16位字节强制转换为字符    num++; //记录读了多少字符}FileWriter fw =  new FileWriter(url); //写文件,文件不存在则创建Scanner sc = new Scanner(System.in);System.out.println("输入您想写的信息:");String s = sc.nextLine();fw.write(s);fw.flush();fw.close();System.out.println("信息录入结束!");

处理流类型:(装比特的小管子套上大管子,转换为其他类型传输,构造方法参数必须是输入/输出流)
这里写图片描述

缓冲流Buffering:提供缓冲功能(数据暂存到缓冲区,再一次性读/写,能保护硬盘)
Reader有mark()标记、reset()重置标记函数,指定读取位置
例如:

FileInputStream fis = new FileInputStream(url);BufferedInputStream bis = new BufferedInputStream(fis);  //可以包含任何InputStream

转换流:字节数据 <=> 字符数据
类:InputStreamReader(和InputStream套接,可用System.in作为参数)、OutputStreamWriter(和OutputStream套接)
作用:直接读写字符串
例如:

FileOutputStream fos =  new FileOutputStream(URL); //输出到文件OutputStreamWriter osw = new OutputStreamWriter(fos); //套接输出流osw.write("It's doesn't occupy student's too much time.\r\n");osw.write("In fact, it's unhealthy for them to spend all of time on their study.\r\n"); //\r\n是JAVA中的回车换行System.out.println(osw.getEncoding()); //获取编码格式osw.flush();osw.close();
/*读取系统输入,每读取一行后输出*/InputStreamReader isr = new InputStreamReader(System.in); //套接输入流,由系统输入构造输入流,该参数本身为InputStreamBufferedReader br = new BufferedReader(isr); //缓冲流System.out.println("请输入:");String str = null;try{    str = br.readLine(); //读取一行    while(!str.equalsIgnoreCase("exit")){ //输入exit后退出        System.out.println(str);        str = br.readLine();    }    br.close();}catch (IOException e){        e.printStackTrace();}

数据输入输出流:DataInputStream、DataOutputStream
作用:直接读写基本数据类型
例如:

public class TestDateStream {    public void TestDateStream() throws IOException {        ByteArrayOutputStream bos = new ByteArrayOutputStream(); //内存上申请一个byte数组,输入的管子怼在上面        DataOutputStream dos = new DataOutputStream(bos); //套上一个数据输出流        dos.writeBoolean(true); //1字节        dos.writeDouble(Math.random()); //8字节        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); //从内存输出数据的管子怼内存的byte数组上        System.out.println(bis.available());                                    //注意要转化为byte数组        DataInputStream dis = new DataInputStream(bis); //套上一个数据输入流        //套上数据流,可以直接输入输出基本数据类型,而不用一位一位输出        dos.close();        dis.close();        System.out.println(dis.readBoolean()); //这是队列结构,先进先出        System.out.println(dis.readDouble());    }}

打印流Print:只有输出流,PrintStream(字节)、PrintWriter(字符)
作用:用于多种数据类型的输出
综合例子:

public class TestPrint2 {//  实现写日记的功能    //从系统标准输入读数据    //一句一句写到文件中    public void TestLog() {        InputStreamReader isr = new InputStreamReader(System.in);        BufferedReader bis = new BufferedReader(isr); //数据从键盘到bis管子        String str = null;        try{            FileWriter fw = new FileWriter("D:\\Java\\workspace\\SXTIO\\src\\io\\Test5.txt", true); //追加            PrintWriter pw = new PrintWriter(fw); //输出到Test5.txt文件的管子            System.out.println("请输入日志:");            while((str = bis.readLine()) != null){                if(str.equalsIgnoreCase("exit")) break;                System.out.println(str.toUpperCase());                pw.println("--------------");                pw.print(str);                pw.flush();            }            pw.println("=======" + new Date() + "========");            pw.close();        }catch (IOException e){            e.printStackTrace();        }    }}

Object流:ObjectInputStream、ObjectOutputStream,直接读写Object对象
transient关键字:临时的,修饰类的成员变量。不被序列化
Serializable接口(标记型接口,无方法):序列化,将Object转换为字节流,写到硬盘/网络上
Externalizable接口: Serializable子接口,内有两种方法,用于控制序列化过程
例如:

 class T implements Serializable{    Int i=10;    transient double j = 2.6;}//随后运用方法和前面各种处理流类似
原创粉丝点击