JavaSE---Basic I/O

1、I/O Streams

    1.1、Byte Streams

        Programs use byte streams to perform input and output of 8-bit bytes. (程序使用 字节流 输入|输出 8位字节)

        All byte stream classes are descended from InputStream and OutputStream.(所有的字节流 用inputstream | outputstream表示)

        CopyBytes example spends most of its time in a simple loop that reads the input stream and writes the output stream, one byte at a time.(字节流案例 花费大量的时间在 循环 读取、写入 【一次一字节】)

        CopyBytes seems like a normal program, but it actually represents a kind of low-level I/O that you should avoid. Since xanadu.txt contains character data, the best approach is to use character streams.(字节流 是一种低等级的IO,文件xanadu.txt是一个字符数据文件,可以使用 character streams)

    1.2、Character Streams

        All character stream classes are descended from Reader and Writer.(所有的字符流 用Reader、Wirter表示)

        Line-Oriented I/O(行读)

          The CopyLines example invokes BufferedReader.readLine and PrintWriter.println to do input and output one line at a time.

    1.3、Buffered Streams

        Unbuffered I/O means each read or write request is handled directly by the underlying OS.(非缓存流IO 意味着 每次读写 请求 都被底层OS直接处理)

        This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.(非缓存流 使得程序非常低效,因为每个请求 都触发磁盘访问、网络活动、其他的操作)

        To reduce this kind of overhead, the Java platform implements buffered I/O streams.(为了减少 非缓存流,Java平台提供了 缓存流)

        Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Similarly, buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.(缓存输入流 从内存的缓存读取数据 直到 缓存为空;缓存输出流 写数据到缓存中 直到缓存满)

        A program can convert an unbuffered stream into a buffered stream using the wrapping idiom.(一个程序可以 将非缓存流转换成缓存流 使用包装类)

          eg:

            BufferedInputStream and BufferedOutputStream create buffered byte streams.

            BufferedReader and BufferedWriter create buffered character streams.

    1.4、Scanning and Formatting

        Programming I/O often involves translating to and from the neatly formatted data humans like to work with.(程序IO 通常涉及翻译数据 至人类可以工作的格式)

        The Java platform provides two APIs. The scanner API breaks input into individual tokens associated with bits of data. The formatting API assembles data into nicely formatted, human-readable form.(Java平台提供了Scanner:将大量数据拆分成 独立字符;Formatting:将拆分的数据组合)

        Scanning

          By default, a scanner uses white space to separate tokens.(Scanner默认使用 空白 作为分隔符)

          To use a different token separator, invoke useDelimiter(), specifying a regular expression.(可以使用userDelimiter()自定义分隔符)

        Formatting

    1.5、I/O from the Command Line

        The Java platform supports this kind of interaction in two ways: through the Standard Streams and through the Console.(Java平台支持2种类型的交互:标准流、控制台

          标准流:System.in; System.out;System.err;

          控制台:System.console().

    1.6、Data Streams

        Data streams support binary I/O of primitive data type values (boolean, char, byte, short, int, long, float, and double) as well as String values.(数据流 支持二进制类型的数据)

        All data streams implement either the DataInput interface or the DataOutput interface.(所有的数据流实现DataInput、DataOutput接口)

    1.7、Object Streams

        The object stream classes are ObjectInputStream and ObjectOutputStream. These classes implement ObjectInput and ObjectOutput, which are subinterfaces of DataInput and DataOutput.(对象流 实现ObjectInput、ObjectOutput接口)

 

2、File I/O

    2.1、What Is a Path?

        Most file systems in use today store the files in a tree (or hierarchical) structure.(大多数文件系统都 使用树结构)

        A file is identified by its path through the file system, beginning from the root node.(文件被定义 通过文件在系统的路径)

        A path is either relative or absolute.(一个路径 可以是相对、绝对)

            An absolute path always contains the root element and the complete directory list required to locate the file.(绝对路径:包含根元素和完整的目录)

            A relative path needs to be combined with another path in order to access a file.(相对路径:是和其他路径的组合

    2.2、The Path Class

        The Path class is one of the primary entrypoints of the java.nio.file package.

        The Path class is a programmatic representation of a path in the file system.(Path 类是文件系统的一个代表)

        A Path instance reflects the underlying platform. In the Solaris OS, a Path uses the Solaris syntax (/home/joe/foo) and in Microsoft Windows, a Path uses the Windows syntax (C:\home\joe\foo).(Path实例 反映了 底层平台)

        Path Operations

    2.3、File Operations

        The Files class is the other primary entrypoint of the java.nio.file package.

    2.4、Checking a File or Directory

        Checking File Accessibility(检测文件是否可访问)

          To verify that the program can access a file as needed, you can use the isReadable(Path), isWritable(Path), and isExecutable(Path) methods.  

        Deleting a File or Directory

          The Files class provides two deletion methods:delete(Path)deleteIfExists(Path)  

        Copying a File or Directory

          You can copy a file or directory by using the copy(Path, Path, CopyOption...) method.  

        Moving a File or Directory

          You can move a file or directory by using the move(Path, Path, CopyOption...) method.  

        File and File Store Attributes

          The Files class includes methods that can be used to obtain a single attribute of a file, or to set an attribute.

    2.5、Creating and Reading Directories

        Listing a File System's Root Directories

          You can list all the root directories for a file system by using the FileSystem.getRootDirectories method.

        Creating a Directory

          You can create a new directory by using the createDirectory(Path, FileAttribute<?>) method.

        Listing a Directory's Contents

          You can list all the contents of a directory by using the newDirectoryStream(Path) method.

        Filtering a Directory Listing By Using Globbing

          If you want to fetch only files and subdirectories where each name matches a particular pattern, you can do so by using the newDirectoryStream(Path, String) method.

        Writing Your Own Directory Filter

          Perhaps you want to filter the contents of a directory based on some condition other than pattern matching. You can create your own filter by implementing the DirectoryStream.Filter<T> interface.

posted on 2020-10-10 11:29  anpeiyong  阅读(114)  评论(0编辑  收藏  举报

导航