java基礎-IO流

這裏寫圖片描述

字節流和字符流

1.區別

  • 字節流:操作的數據單元是8位的字節
  • 字符流:操作的數據單元是16位的字節

2.輸入流

  • 字節輸入流(InputStream)
int read():從輸入流中讀取單個字節,返回所讀取的字節數據;
int read(byte[] b):從輸入流中讀取b.length個字節數據,並存儲在字節數組b中,返回實際             讀取的字節數;
int read(byte[] b , int off , int len):從輸入流中最多讀取len個字節數據,並將存放在數組b從off位置開始存儲數據,返回實際讀取到的字節數
  • 字符輸入流(Reader)
int read()
int read(char[] cbuf)
int read(char[] cbuf , int off , int len)

3.FileInputStream和Reader

package com.gdy.file;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by RS on 2017/10/16.
 */
public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {

        //創建字符輸入流
        FileInputStream fis = new FileInputStream("test.txt");

        byte[] read1 = new byte[1024];
        //用於保存實際讀取的字節數
        int readlength = 0 ;

        while((readlength = fis.read(read1)) > 0 ){
            System.out.println(new String(read1 , 0 , readlength));
        }

        fis.close();

    }
}
package com.gdy.file;

import java.io.FileReader;
import java.io.IOException;

/**
 * @author rs
 * Created by RS on 2017/10/17.
 */
public class FileReadDemo {
    public static void main(String[] args) throws IOException {

        FileReader fileRead = new FileReader("test.txt");
        char[] chbf = new char[32];
        int hasRead = 0 ;
        while ((hasRead = fileRead.read(chbf)) >0){
            System.out.println(new String(chbf,0,hasRead));
        }
    }
}

3.輸出流

1.OutputStream和Writer

void write(int c)將指定的字節/字符數組中的數據輸出到指定輸出流中
void writer(byte[]/char[] buf):將字節數組/字符數組中的數據輸出到指定輸出流中
void wirter(byte[]/char[],int off.int len):將字節數組/字符數組中從off位置開始,長度爲len的數據出處到輸出流中
void witer(String str):將str字符串中包含的字符輸出到指定的輸出流中
void witer(String str,int off,int len):將sr字符串中從off位置開始,長度爲len的字符輸出到指定輸出流中

2.FileInputStream複製文件

package com.gdy.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
    public static void main(String[] args) {
        try {
            //創建輸入字節流
            FileInputStream fileInputStream = new FileInputStream("data/FileTest.txt");
            //創建輸出字節流
            FileOutputStream fileOutputStream = new FileOutputStream("data/FileTest222.txt");
            byte[] bytes = new byte[1024];
            int readLength = 0 ;

            while( (readLength = fileInputStream.read(bytes) ) > 0){
                fileOutputStream.write(bytes , 0 , readLength);
            }


        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.FileWriter寫文件

package com.gdy.file;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by RS on 2017/10/18.
 * 會覆蓋之前的文件
 */
public class FIleWriterDemo {
    public static void main(String[] args) {
        FileWriter fileWriter = null;
        try {
             fileWriter = new FileWriter("newtest.txt");
             fileWriter.write("123");
            fileWriter.write("123");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
發佈了74 篇原創文章 · 獲贊 16 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章