《Thinking in java》阅读笔记(6)-I/O

myhalfsea posted @ 2011年2月28日 04:46 in JAVA , 2016 阅读

之前对I/O的理解总是处于混沌状态,所以用的时候总是要查资料,但是总是记不住。下面根据我的思路,一步一步的深入理解:

I/O系统可以分为两大类:InputStream/OutputStream、Reader/Writer:

Reader/Writer存在的原因:

1、老的I/O系统(即InputStream/OutputStream)仅支持8位字节流,不能很好的处理16位Unicode,而Unicode用于国际化。

2、新的速度比旧类更快。 所以应当尽量使用Reader/Writer。

基本输入:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class TestSort{
    public static String read( String filename) throws IOException{
        BufferedReader in = new BufferedReader(new FileReader(filename));
        List<String> list = new ArrayList<String>();
        String s = "aa";
        StringBuilder sb = new StringBuilder();
        while( ( s = in.readLine()) != null ){
            list.add(s);
        }
        ListIterator<String> it = list.listIterator(list.size()-1);
        while( it.hasPrevious() ){
            sb.append("\n"+it.previous());
        }
        in.close();
        return sb.toString();
    }
    public static void main(String[] args) throws IOException {
        System.out.println(TestSort.read("f://java/TestSort.java"));
    }
}

输出即为将整个程序倒着输出。

比较ByteArrayInputStream(允许将内存的缓冲区作为InputStream使用):将main()方法改为如下(输出不变):

    public static void main(String[] args) throws IOException {
        TestSort t = new TestSort();
        try{
            DataInputStream in = new DataInputStream( new ByteArrayInputStream(
                            TestSort.read("f://java/TestSort.java").getBytes()));
            while( true )   System.out.print((char)in.readByte());
        }catch(EOFException e){
            System.out.println();
        }
    }

注意:以上用捕获异常来检测输入的结束,但是,使用异常进行流控制,被认为是对异常特性的错误使用。正确的方法是:

public static void main(String[] args) throws IOException {
        TestSort t = new TestSort();
        DataInputStream in = new DataInputStream(
                new ByteArrayInputStream(
                        TestSort.read("f://java/TestSort.java").getBytes()));
        while( in.available() != 0 )
            System.out.print((char)in.readByte());    
    }

但是avialable(方法)对于不同的类型的流,应该谨慎使用。

基本的文件输出:

FileWriter对象向文件写入数据。通常用BufferedWriter将其包装起来用于缓冲输出(缓冲能显著增加性能),也可以不用包装

public static void main(String[] args) throws IOException {
        String file = "f://java/test.java";
        PrintWriter pw = new PrintWriter(file);
//        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        BufferedReader in = new BufferedReader(new FileReader("f://java/TestSort.java"));
        String s;
        while( (s = in.readLine() ) != null )
            pw.write(s+"\n");
        pw.close();
        System.out.println(TestSort.read(file));
       
    }

用以下方式,可以将信息存储在文件中,读出的结果正确。

但是有两个问题,1、打开文件却是乱码,怎么解决?待解决……………………………………………………………

2、非得用in.readXXX()的形式吗,如果我不知道类型呢? 答:保险起见,写入文件是全用UTF形式,读的时候也全用UTF。


public static void main(String[] args) throws IOException {
        DataOutputStream out = new DataOutputStream(
                new BufferedOutputStream(new FileOutputStream("f://java/test.txt")));
        out.writeUTF("面朝大海,春暖花开\nWhat's you name?");
        out.writeInt(21313);
        out.close();
        DataInputStream in = new DataInputStream(
                new BufferedInputStream(new FileInputStream("f://java/test.txt")));
        while( in.available() != 0 )
            System.out.println(in.readUTF());
        in.close();
    }

输出:

面朝大海,春暖花开
What's you name?
201314

 

networkslog.com 说:
2023年4月16日 16:18

There are thousands of Branch and ATM centers from Canara Bank, which areWe discuss, review, write and explain about different products, services, technology and more through our website which are for learning and educational purposes only. That being said, though we try to be up to date with the information. networkslog.com Different ways are available to check Canara Bank balance, Find each way and Canara bank balance check number for each type to get on demand… Canara Bank is one of the largest Banks in India, which has over Crore of Active customers.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta
Butterfly Theme | Design: HRS Hersteller of mobile Hundeschule.