Java基礎之基本IO操作

文章的內容參考了 http://blog.csdn.net/yczz/article/details/38761237,對博主表示感謝。

java的IO體系包括字符流和字節流,如下圖所示:



下面對常見的IO操作舉例,如果想要參考IO的其他流操縱,請閱讀官方的API文檔

import java.io.*;

import static java.lang.System.*;

/**
 * Created by leo on 17-7-11.
 */
public class IONote {
    public static void main(String[] args){
        IONote note = new IONote();

//        note.createFile("/home/leo/Java/io.txt");
//        note.copyFile();
//        note.countLines();
        note.useBufferWriter();
    }

    public void createFile(String fileName){
        File file = new File(fileName);
        try {
            file.createNewFile();
//            out.println(file.getTotalSpace()/1024/1024/1024 + "G");
            file.mkdirs();
            out.println(file.getName());
            out.println(file.getParent());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    copy file
    public void copyFile(){
        byte[] buffer = new byte[1024];
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;

        try {
            inputStream = new FileInputStream("/home/leo/Java/io.txt");
            outputStream = new FileOutputStream("/home/leo/Java/io.txt.copy1");
            int numberRead = inputStream.read(buffer);
            while (numberRead != -1){
                outputStream.write(buffer, 0, numberRead);
                numberRead = inputStream.read();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//    BufferReader,FileInputStream
    public void countLines(){
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("/home/leo/Java/io.txt.copy")));
            int charCounts = 0;
            Long lineCounts = 0L;
//            while (reader.read() != -1){
//                charCounts += 1;
//            }
//          無論是讀一個字符還是一行,每read或者readLine一次,讀的指針就往前一步
            while (reader.readLine() != null){
                out.println(reader.readLine());
                lineCounts += 1;
            }
            out.println("charCounts=" + charCounts + " " + "lineCounts=" + lineCounts);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

//    BufferWriter
    public void useBufferWriter(){
        try {
            FileWriter fileWriter = new FileWriter("/home/leo/Java/io.txt.copy1");
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write("hello");
            bufferedWriter.newLine(); //寫入一個換行符
            bufferedWriter.write("world");
            bufferedWriter.flush();
            bufferedWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章