java.io package 基本操作

图示

reader,writer与InputStream,OutputStream的区别?
InputStreamReader,OutputStreamWriter 是桥梁
字符流和字节流的区别?

http://www.runoob.com/wp-content/uploads/2013/12/iostream2xx.png

源码分析

BufferedReader源码分析与使用

Java IO 之 FileInputStream & FileOutputStream源码分析

InputStreamReader OutputStreamReader 源码分析

常用操作

1.从控制台读取

public static void readTest() throws IOException {
    String c;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enput , 'q' return");

    do {
        c = br.readLine();
        System.out.println(c);
    } while (c != "q");
}

2. 文件读取

 public static void fileTest() throws IOException {
    byte bWrite[] = {96, 95, 97, 98, 99};
    OutputStream os = new FileOutputStream("D:/test.txt");
    os.write(bWrite);
    os.close();

    InputStream is = new FileInputStream("D:/test.txt");
    int size = is.available();
    bWrite = new byte[size];
    is.read(bWrite);
    is.close();

    for (int i = 0; i < size; i++) {
        System.out.print((char) bWrite[i]);
    }

    is.close();
}

3. OutputStreamWriter,InputStreamReader

public static void fileTest2() throws IOException {
    File f = new File("D:/a.txt");
    FileOutputStream fos = new FileOutputStream(f);

    OutputStreamWriter writer = new OutputStreamWriter(fos,"UTF-8");
    writer.append("中文输入");
    writer.append("\r\n");
    writer.append("English");
    writer.close();
    fos.close();

    FileInputStream fis = new FileInputStream(f);
    InputStreamReader reader = new InputStreamReader(fis,"UTF-8");

    StringBuffer sb =new StringBuffer();
    while(reader.ready()){
        sb.append((char)reader.read());
    }
    System.out.println(sb.toString());
    reader.close();
    fis.close();
}

4. File,FileReader,FileWriter

public static void fileTest3() throws IOException {
    String path = "D:/aaa/bbb/ccc/ddd";
    File d = new File(path);
    d.mkdirs();
    FileWriter fw = new FileWriter(path+"aaa.txt");
    fw.write("test");
    fw.close();
    deleteFolder(new File("D:/aaa"));
}

private static void deleteFolder(File folder){
    File[] files = folder.listFiles();
    if(files!=null){
        for(File f:files){
            if(f.isDirectory()){
                deleteFolder(f);
            }else{
                f.delete();
            }
        }
    }
    folder.delete();
}

参考

java.io

java.io package 基本操作》有1个想法

  1. Emphasis added: “showing no effort to actually sell it in large scale while the tax incentive still applies That sentence pretty clearly indicates that the subject is (at least primarily) the US. Unless you would like to suggest some other tax incentive that”s about to expire? If we”re discussing foreign markets this discussion doesn”t even make sense, as Nissan has delivered far more Leafs outside of the US than anything Tesla offers. Your claim that the downmarket segments “lack compelling offers would be easier to accept had the Volt and Bolt not each won a raft of awards. From a critical reception standpoint, the Volt was literally the best car GM has ever made. It was also the most awarded car in U.S. automaker history until the Model S came along a couple of years later and unseated it. I guess that must mean the Volt was always trash. Tesla is quite popular among the very wealthy; that”s an enviable position to be in, as they have established a strong brand in that market segment. But it”s pretty clear that at a price point where you have to make more sacrifices in what features you get , the general public is not particularly warm to EVs right now. As the technology improves and automakers can offer 300 mile EVs with traditional styling & cargo space at Camry prices, that should change.

发表评论

电子邮件地址不会被公开。

12 + 15 =