IO

作者: CHENpq | 来源:发表于2019-05-05 23:48 被阅读0次

    File

    An abstract representation of file and directory pathnames.

    public class File

    extends Object

    implements Serializable, Comparable<File>

    //写txt文件也会被创建成文件夹并不会是txt文件

    File file = new File("./src/cn/tju/scs/c02/file/test/test.txt");

    txt文件是file不是directory

    文件夹是directory不是file

    file分hidden不hidden,isFile()用于判断是不是normal file

    Stream

    public interface Stream<T>

    extends BaseStream<T,Stream<T>>

    //In this example, widgets is a Collection<Widget>.

    //We create a stream of Widget objects via Collection.stream(),

    //filter it to produce a stream containing only the red widgets,

    //then transform it into a stream of int values representing the weight of each red widget.

    //Then this stream is summed to produce a total weight.

    int sum = widgets.stream()

                          .filter(w -> w.getColor() == RED)

                          .mapToInt(w -> w.getWeight())

                          .sum();

    可以represent多种source和destination(disk file,device,programs,memory arrays等)

    支持多种格式的数据(最简单的bytes,八种基本数据类型,objects,虽然其中有一些会改变data格式)

    – A program uses an input stream to read data from a source– A program uses an output stream to write data to a destination

    Java.io包中定义流的分类

    方式一:数据流的方向:

    InputStream

    是抽象类,需要用其子类 that returns the next byte of input.

    public abstract class InputStream

    extends Object //并不是Stream的子类,也没有实现Stream接口

    implements Closeable//只有一个void方法 close()

    //主要方法

    public abstract int read()

                      throws IOException

    //父类指针指向子类对象                 

    OutputStream os = new FileOutputStream(file, true)

    OutputStream

    An output stream accepts output bytes and sends them to some sink.

    是抽象类,需要其子类来写入文件,或者写到其他地方

    public abstract class OutputStream

    extends Object

    implements Closeable, Flushable

    //没有无参的write

    public abstract void write(int b)

                        throws IOException

    //If the second argument is true, then bytes will be written to the end of the file rather than the beginning.

    FileOutputStream(File file, boolean append)

      throws FileNotFoundException

    close();

    方式二:处理数据的单位:

    Byte Stream (字节流)和 Character Stream(字符流)

    InputStream OutputStream Reader Writer是java.io包里的四个抽象类

    其中,

    InputStream和OutputStream属于按字节流处理数据

    Reader和Writer属于按字符流处理数据

    OutputStream需要close()

    Writer不需要,但可以flush()

    开发中字节数据比较多,图片、音乐、电影

    字符最大的好处,中文的有效处理,如果处理中文,优先使用字符流,除此之外,以字节流为主。

    public abstract class Reader

    extends Object

    implements Readable, Closeable

    public int read()

            throws IOException//也抛出IO异常

    //Abstract class for reading character streams.

    //The only methods that a subclass must implement are read(char[], int, int) and close().

    public abstract class Writer

    extends Object

    //Flushable只有一个方法 void flush() throws IOException

    implements Appendable, Closeable, Flushable

    PrintStream & PrintWriter

    字符打印流 & 字节打印流

    public class PrintStream

    extends FilterOutputStream

    implements Appendable, Closeable

    //outputStream只能输出以字节类型为主的数据

    //int(Integer)、double(Double)、Date类型 的输出由PrintStream输出

    PrintStream(OutputStream out);

    public void print(数据类型 参数名称);

    整数(%d)、字符串(%s)、小数(%m.nf)、字符(%c)。

    System.in

    try{

    Integer.parseInt("abc");

    } catch(Exception e){

      //结果相同,err为红色

    System.err.println(e);

    System.out.println(e);

    }

    printf()返回类型是PrintStream

    print() println() 返回类型是void

    相关文章

      网友评论

          本文标题:IO

          本文链接:https://www.haomeiwen.com/subject/qbfloqtx.html