輸入流:數據源數據流向計算機內存的過程
輸出流:把數據從程序流向目標數據源的過程
字節流:以字節爲數據單位來處理的流
字符流:以字符爲數據單位來處理的流
流的基類:
輸入流:以InputStream(字節輸入流)和Reader(字符輸入流)爲基類
輸入流:以OUtputStream(字節輸出流)和Writer(字符輸出流)爲基類
方法的使用:
1.從文件地址中讀取內容到程序中,並把字節數組轉換成字符串
方法一:
public static void main(String[] args) {
//磁盤路徑兩種表示方式:1.\\ 2./
try {
//從文件地址中讀取內容到程序中
InputStream is=new FileInputStream("D:/iodemo/ch01.txt");
//開始讀取信息
//先定義一個字節數組存放數據
byte[] b=new byte[5];
//聲明一個int存儲每次讀取到的數據
int i=0;
//定義一個記錄索引的變量
int index=0;
//循環讀取每個數據
while((i=is.read())!=-1) {
b[index]=(byte) i;
index++;
}
//把字節數組轉成字符串
System.out.println(new String(b));
//關閉流
is.close();
} catch (FileNotFoundException e) {
//系統強制解決的問題:文件沒有找到
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//文件讀寫異常
// TODO Auto-generated catch block
e.printStackTrace();
}
}
方法二:
public static void main(String[] args) {
try {
InputStream is = new FileInputStream("E:/iodemo/ch01.txt");
byte[] b = new byte[5];//把所有的數據讀取到這個字節當中
//完整的讀取一個文件
int off = 0;
while(is.read(b, off, 2)!=-1){
off+=1;
}
is.read(b,off,2);
//read:返回的是讀取的文件大小
//最大不超過b.length,返回實際讀取的字節個數
System.out.println(Arrays.toString(b));//讀取的是字節數組
System.out.println(new String(b));
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.在讀取文件時,不全部讀取,跳過n個字節後再開始讀取
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("E:/iodemo/ch01.txt");
byte[] b = new byte[fis.available()];
//skip跳過n個字節後再開始讀取
fis.skip(2);
fis.read(b);
System.out.println(new String(b));
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3.讀取時把2個流合併在一起讀取public static void main(String[] args) {
try {
//讀第一個文件流
FileInputStream fis1 = new FileInputStream("E:/iodemo/ch01.txt");
//讀第二個文件流
FileInputStream fis2 = new FileInputStream("E:/iodemo/ch04.txt");
//合併到序列流中
SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
//方式1
/*int len = fis1.available()+fis2.available();
//臨時存放數據的數組
byte[] b = new byte[2*len+1];
//一次性讀取所有的內容
int off = 0;
int i = 0;
while((i = sis.read(b,off,len)) != -1){
off += i;
}
System.out.println(new String(b));*/
//方式2
byte[] b = new byte[1024];
while(sis.read(b)!=-1){
System.out.println(new String(b));
}
sis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.讀取時把3個流合併在一起讀取public static void main(String[] args) {
try {
FileInputStream fis1 = new FileInputStream("E:/iodemo/a.txt");
FileInputStream fis2 = new FileInputStream("E:/iodemo/b.txt");
FileInputStream fis3 = new FileInputStream("E:/iodemo/c.txt");
//把三個流添加到集合中
Vector<FileInputStream> vector = new Vector<>();
vector.add(fis1);
vector.add(fis2);
vector.add(fis3);
//elements方法的返回值類型是Enumeration類型
// vector.elements();
//合併在一個序列流中
SequenceInputStream sis = new SequenceInputStream(vector.elements());
byte[] b = new byte[fis1.available()+fis2.available()+fis3.available()];
//數據讀取
int off = 0;
for(int i = 0;i<vector.size();i++){
//off是數組中存放數據的起始下標位置
//read的返回值就是數組中第i個的長度
off += sis.read(b,off,vector.get(i).available());
}
System.out.println(new String(b));
sis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
5.IO流中的printStream()方法public static void main(String[] args) {
// 構造參數傳System.out就是在控制檯打印信息
// PrintStream ps = new PrintStream(System.out);
// ps.print("skdfhskl");
PrintStream ps;
try {
ps = new PrintStream(new FileOutputStream("E:/iodemo/print.txt"));
ps.println("牀前明月光,");
ps.println("疑是地上霜。");
ps.println("舉頭望明月,");
ps.println("低頭思故鄉。");
ps.flush();
ps.close();
System.out.println("打印成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}