JAVA2核心技术卷I:基础知识(原书第7版) -- 第12章. 流与文件

来源:互联网 发布:servlet接收数据原理 编辑:程序博客网 时间:2024/05/22 07:59

1.  读写操作

    InputStream/OutputStream: 输入输出流,以字节为单位

    Reader/Writer: 读写以双字节为单位

2. InputStream和OutputStream方法

    2.1 int read(): 读取一个字节的数据并返回,读到流的结尾,返回-1

    2.2 int read(byte[] b): 将数据读到一个字节数组中并返回读取的字节数目,到达末尾返回-1

    2.3 int read(byte[] b, int off, int len): 读取的数据放在b的第off开始,len代表长度

    2.4 int available(): 返回可用的未阻塞的字节数

        int byteAvailable = in.available();

        if (byteAvailable > 0) {

            byte[] data = new byte[byteAvailable];

            in.read(data);

        }

    2.5 void close(): 关闭输入流

    2.6 void write(int n): 写入一个字节的数据

    2.7 void write(byte[] b): 写入数组b的所有字节

    2.8 void write(byte[] b, int off, int len): 写入数组b某一范围的数据

    2.9 void close(): 刷新并关闭输出流

    2.10 void flush(): 刷新输出流

3. JDK5.0加入4种新接口: Closeable、Flushable、Readable、Appendable

4. 介绍基本流的使用

    4.1 FileInputStream和FileOutputStream能够和磁盘文件联系起来

        FileInputStream fin = new FileInputStream("Employee.dat"); // 在当前目录下寻找文件

        在windows格式的路径,应该使用“//”作为路径分隔符

    4.2 同样可以使用以下方法读取

        File f = new File("employee.dat");

        FileInputStream fin = new FileInputStream(f);

        byte[] b = (byte) fin.read();

        System.getProperty("user.dir"): 可以获取当前目录的属性

    4.3 缓冲读取数据

        DataInputStream din = new DataInputStream(new BufferedInputStream(newFileInputStream("employee.dat")));

        一般使用DataInputStream来帮助读取ZipInputStream和FileInputStream中的数据

5. DataInput和DataOutput

    5.1 readBoolean()、readByte()、String readLine(): 读取一行由/n /r /r/n或者EOF结尾的字符、String readUTF()

6. RandomAccessFile: 随机存取文件流能够在文件的任何位置查找或者写入数据

    RandomAccessFile("employee.dat", "r/rw/rws"): 后面是读取模式

    getFilePointer(): 获取指针位置

7. 文本流

    使用编码读取文件 InputStreamReader in = new InputStreamReader(new FileInputStream("abc.dat"), "ISO8859_5");

    String abc = new String(byte[] b); 方便把byte转换成String

8. 字符集Charset

    Charset cset = Charset.forName("ISO-6659-1");

    8.1 编码

        String str = "abc";

        ByteBuffer buffer = cset.encode(str);

        byte[] bytes = buffer.array();

    8.2 解码

        byte[] bytes = "...";

        ByteBuffer buffer = ByteBuffer.wrap(bytes);    //返回给定的字节数组

        CharBuffer cbuf = cset.decode(buffer);

        String str = cbuf.toString();

9. 文本输出 - PrintWriter

    PrintWriter: 文本格式写数据    DataOutputStream: 二进制格式写数据

    PrintWriter = out new PrintWriter(new FileWriter("employee.dat")); 或者

    PrintWriter = out new PrintWriter(new FileOutputStream("employee.dat"));

    PrintWriter = out new PrintWriter(new FileWriter("employee.dat"), true);  //自动刷新模式

10. 文本输入 - BufferedReader

    BufferedReader in = new BufferedReader(new FileReader("Employee.txt")); 

11. ZIP文件流

    ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));

    ZipEntry entry;

    while ((entry = zin.getNextEntry()) != null) {

        analyze entry;

        read the contents of zin;

        zin.closeEntry();

    }

    zin.close();

12. 分隔符 - StringTokenizer

    用于读取字符串中的被分割后的字符

    StringTokenizer tokenizer = new StringTokenizer(line, "|"); //字符串line用“|”被分割

    while (tokenizer.hasMoreTokens()) {

        String token = tokenizer.nextToken();

        process token;

    }

13. StringBuilder和StringBuffer

    StringBuffer允许多线程进行操作,StringBuilder一个线程操作

    append(String str/ char c): 追加字符串或者代码单元

    insert(int offset, String str): 在offset位置插入一个字符串

    delete(int startIndex, int endIndex): 删除从startIndex到endIndex-1的内容

    toString(): 返回一个String

14. 对象序列化读写

    读取的对象必须实现Serializable接口 class Employee implements Serializable

    14.1 写入对象

    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat"));

    Employee harry = new Employee("Harry", 50000, 1989, 10, 1);

    out.writeObject(harry);

    14.2 读取对象

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat"));

    Employee e = (Employee) in.readObject();    

15. ByteArrayOutputStream 存储数据到一个字节数组

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");

    ... //osw.writeObject(this);

    byte[] reqBuffer = baos.toByteArray();

16. 文件管理

    File f = new File("text.txt"); // 创建一个文件,使用test.txt

    boolean delete(): 删除文件

    boolean exists(): 文件是否存在

    String getAbsolutePath(): 得到绝对路径

    String getName(): 得到文件名

    boolean isDirectory(): 是否为目录

    boolean isFile(): 是否为文件

    String[] list(): 列出目录下所有文件名

原创粉丝点击