1. 程式人生 > >java -io 讀取文件操作

java -io 讀取文件操作

import cep 字符流 weight exc drl junit throws har

主要分為字節讀取和字符讀取,字節讀取可以一個一個讀取和字節數組讀取,字符讀取同樣之,字符讀取適合文本讀取,字節讀取皆可以

這裏直接上代碼,讀取文件的9個小demo


package com.io;

import org.junit.Test;

import java.io.*;

public class FileTest {
//1、字節流字節一個一個讀取
@Test
public void test() throws IOException{
InputStream fis = new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt"));
int len;
//按字節一個一個讀取
while ((len = fis.read())!=-1){
System.out.print((char)len+"\t");
};
fis.close();
}
//輸出結果h e l l o w o r l d

//2、字節流字節數組讀取
@Test
public void test1() throws IOException{
InputStream fis = new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt"));
byte[] bytes = new byte[2];
int len ;
//按字節數組讀取 bytes存儲的是讀取的數據
while ((len = fis.read(bytes))!=-1){
System.out.print((new String(bytes))+"\t");
};
fis.close();
}
//輸出結果 he ll ow or ld

//3、緩沖字節流字節一個一個讀取
@Test
public void test2() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt")));
int len ;
while ((len = bis.read())!=-1){
System.out.print((char)len+"\t");
};
bis.close();
}
//輸出結果h    e    l    l    o    w    o    r    l    d  

//4、緩沖字節流字節數組讀取
@Test
public void test3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt")));
byte[] bytes = new byte[3];
int len ;
//按字節數組讀取 bytes存儲的是讀取的數據
while ((len = bis.read(bytes))!=-1){
System.out.print(new String(bytes)+"\t");
};
bis.close();
}
//輸出結果hel low orl drl

//5、字符流一個一個讀取
@Test
public void test4() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt")));
int len ;
//按字節數組讀取 bytes存儲的是讀取的數據
while ((len = isr.read())!=-1){
System.out.print((char)len+"\t");
};
isr.close();
}


//6、字符流字符數組讀取
@Test
public void test5() throws IOException{
InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt")));
char[] chars = new char[3];
int len ;
//按字節數組讀取 bytes存儲的是讀取的數據
while ((len = isr.read(chars))!=-1){
System.out.print(new String(chars)+"\t");
};
isr.close();
}

//7、緩沖字符流字符一個一個讀取
@Test
public void test6() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt"))));
int len ;
//按字節數組讀取 bytes存儲的是讀取的數據
while ((len = br.read())!=-1){
System.out.print((char)len+"\t");
};
br.close();
}

//8、緩沖字符流字符數組讀取
@Test
public void test7() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt"))));
char[] chars = new char[3];
int len ;
//按字節數組讀取 bytes存儲的是讀取的數據
while ((len = br.read(chars))!=-1){
System.out.print(new String(chars)+"\t");
};
br.close();
}

//9、緩沖字符流按行讀取
@Test
public void test8() throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\\project_test\\src\\com\\io\\readme.txt"))));
//按字節數組讀取 bytes存儲的是讀取的數據
String str;
while ( (str = br.readLine())!=null){
System.out.print(str+"\t");
};
br.close();
}
}
待更。。。。。。。。。。。。。。

java -io 讀取文件操作