Java初学几天的总结——一个菜鸡的自我学习之路(9)

流是输入输出的方式
流是一维单向的

流的基础类:

InputStream和OutputStream

这两个只能做字节层面上的读和写

InputStream

read()                                   int available() //数据流中还有多少字节可以读
int read()                              mark() //可以在数据流中做一个标记,但是mark只能存在一个,如果前后都有mark的话,前一个就消失
read(byte b[])                       reset() //在数据流中返回至前一个mark标记处
read(byte[],int off,int len)    boolean markSupported() //判断是否可以进行mark的运算
skip(long n)                          close()

OutputStream

write()
write(int b)
write(byte b[])
write(byte b[],int off, int len)
flush() //保证数据能够写到硬件上
close()

所有的I/O操作都存在着风险

文件流

FileInputStream
FileOutputStream

对文件做读写操作
实际工程中已经较少使用
更常用的是以在内存数据或通信数据上建立的流,如数据库的二进制数据读写或网络端口通信
具体的文件读写往往有更专业的类,比如配置文件和日志文件

import java.util.Scanner;
import java .io.IOException;

public class Main {
    public static void main(String[] args) {
        System.out.pringtln("hello world");
        byte[] buf = new byte[10];
        for( int i = 0; i<buf.length; i++ ) {
            buf[i] = (char)i;
        }
        try {
            FileOutputStream out = new FileOutputStream("a.dat");
            out.write(buf);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

流过滤器

以一个介质对象为基础层层构建过滤器流,最终形成的流对象能在数据的输入输出过程中,逐层使用过滤器流的方法来读写数据
import java.util.Scanner;
import java .io.IOException;

public class Main {
    public static void main(String[] args) {
        System.out.pringtln("hello world");
        byte[] buf = new byte[10];
        for( int i = 0; i<buf.length; i++ ) {
            buf[i] = (char)i;
        }
        try {
            DateOutputStream out = new DateOutputStream(new BufferedOutputStream(new FileOutputStream("a.dat")));
            int i = 123456;
            out.writeInt(i);
            out.close();
            DateInputStream in = new DateInputStream(new BufferedInputStream(new FileInputStream("a.dat")));
            int j = in.readInt();
            System.out.println(j);
            out.write(buf);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

上面的buffer过滤器起到了缓冲的作用,Data起到了读写数据内容到文件的作用

Data

DataInputStream
DataOutputStream
用以读写二进制方式表达的基本数据类型的数据

Reader/Writer

二进制数据采用InputStream/OutputStream
文本数据采用Reader/Writer

在流上建立文本处理

 PrintWriter pw = new PrintWriter(
        new BufferedWriter(
            new OutputStreamWriter(
                new FileOutputStream("abc.txt")));

按这个写出来的文件码是ASCⅡ码或者是国标码

当文本本身不是用Unicode码来写的时候需要借助Stream来打开文件,在Stream的基础上,以过滤流/过滤器的方式来建立Reader/Writer,来做文本的输出

import java.util.Scanner;
import java .io.IOException;

public class Main {
    public static void main(String[] args) {
        System.out.pringtln("hello world");
        byte[] buf = new byte[10];
        for( int i = 0; i<buf.length; i++ ) {
            buf[i] = (char)i;
        }
        try {
            PrintWriter out = new PrintWriter(
                new BufferedWriter(
                    new OutputStreamWriter(
                        new FileOutputStream("a.dat"))));
            int i = 123456;
            out.println(i);
            out.close();
            BufferedReader in = new BufferedReader(
                new InputStreamReader(//InputStreamReader("file","编码")仅仅只是用来处理文本的函数,如果没有给出
                    new FileInputStream("src/hello/Main.java")));//FileInputStream(file)函数是处理二进制字节的函数
            String line;//读取文件中所有的数据,原封不动的输出
            while ( (line=in.readLine) ) != null ) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Reader

常用的是BufferedReader
readLine()//用于将文本中的一整行读进来

LineNumberReader

可以得到行号
getLineNumber()

FileReader

InputStreamReader类的子类,所有方法都从父类中继承而来
FileReade (File file)//在给定从中读取数据的File的情况下创建一个新FileReader。
FileReader (String fileName)//在给定从中读取数据的文件名的情况下创建一个新FileReader
FileReader不能指定编码转换方式

汉字编码

InputStreamReader (InputStream in)//创建一个使用默认字符集的InputStreamReader
InputStreamReader (InputStream in, Charset cs)//创建使用给定字符集的InputStreamReader
InputStreamReader (InputStream in, CharsetDecoder dec)//创建使用给定字符集解码器的InputStreamReader
InputStreamReader (InputStream in, String charsetName)//创建使用给定字符集的InputStreamReader

PrintWrite //使用在System.out.后面的

format("格式",...);
printf("格式:",...);
print(各种基本类型);
println(各种基本类型);

Scanner

在InputStream或Reader上建立一个Scanner对象可以从流中的文本中解析出以文本表达的各种基本类型
next...()

在这里插入图片描述

首先判断的是说,这个数据是不是二进制的,如果数据本身是二进制的,那么就用Stream来读,如果你想要读出Java里面的数据,就可以用到InputStream函数;
如果这个文件本身不是二进制的,是文本文件类型的,那么就要判断这个文本文件表达,就是说这个文本文件我们是要像文章一样直接读取它的内容还是读取它的数据;
如果我们是把它当作一个文章来读,那么我们在它的上方构建一个Reader,然后用这个Reader的readerLine来把它都进来;
如果我们把它当作数据,那么这时候我们就要用到Scanner,用Scanner从文本里面解析出各种基本数据类型,然后将这些放在基本数据类型的变量里面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值