文章目錄
- 1.IO流概述
- 1.1 什么是IO流
- 1.2 IO流的分類
- 2.File類
- 2.1構造方法
- 2.1常用方法
- 3.字符流
- 3.1 字符輸入流
- 字符流讀資料 – 按單個字符讀取
- 字符流讀資料 – 按字符陣列讀取
- 3.2字符輸出流
- 字符流寫資料 – 按單個字符寫入
- 字符流寫資料 – 按字符陣列寫入
- 3.3 FileReader和FileWriter完成文本復制
- 3.4 字符緩沖流檔案拷貝標準代碼
- 4.位元組流
- 4.1 構造方法
- 4.2 位元組流拷貝檔案 – 按單個位元組讀寫
- 4.3 位元組流拷貝檔案 – 按位元組陣列讀寫
- 5.緩沖流
- 5.1位元組緩沖流
- 構造方法
- 位元組緩沖流拷貝檔案的標準代碼
- 5.2字符緩沖流
- 構造方法
- 字符緩沖流特有方法
- 字符緩沖流拷貝檔案的標準代碼
1.IO流概述
1.1 什么是IO流
I/O,即輸入(Input)/ 輸出(Output),IO流指的是資料像連綿的流體一樣進行傳輸,
Java中I/O的操作是指使用java.io包下的內容進行輸入、輸出操作,
1.2 IO流的分類
按資料的流向分為:輸入流和輸出流
按資料的型別分為:
?位元組流:以位元組為單位來操作資料,
??InputStream: 位元組輸入流的頂層抽象類.
???FileInputStream:普通的位元組輸入流.
???BufferedInputStream: 高效的位元組輸入流(也叫: 位元組緩沖輸入流)
??OutputStream:位元組輸出流的頂層抽象類.
???FileOutputStream:普通的位元組輸出流.
???BufferedOutputStream:高效的位元組輸出流(也叫: 位元組緩沖輸出流).
?字符流: 以字符為單位來操作資料
??Reader: 字符輸入流的頂層抽象類.
???FileReader:普通的字符輸入流.
???BufferedReader:高效的字符輸入流(也叫: 字符緩沖輸入流)
??Writer: 字符輸出流的頂層抽象類.
???FileWriter:普通的字符輸出流.
???BufferedWriter:高效的字符輸出流(也叫: 字符緩沖輸出流)
2.File類
java.io.File 類是專門對檔案進行操作的類,只能對檔案本身進行操作,不能對檔案內容進行操作,java.io.File 類是檔案和目錄路徑名的抽象表示,主要用于檔案和目錄的創建、查找和洗掉等操作,
也就是說,File類只能針對檔案和目錄進行操作,不能對檔案內容進行讀寫操作,
2.1構造方法
構造方法:File類有三個常用的構造方法//1.根據字串形式獲取File物件
public File(String pathname);
//2.根據字串形式的父目錄以及子目錄創建File物件
public File(String parent, String child);
//3.根據父目錄物件以及字串形式的子目錄物件獲取File物件
public File(File parent, String child);
也就是我們有三種方法創建File物件
String pathname = "E:\\test\\1.txt"
String parent = "E:\\test\\"
String child = "1.txt"
//方式一:根據字串形式獲取File物件
File file1 = new File(pathname);
//方式二:根據字串形式的父目錄以及子目錄創建File物件
File file2 = new File(parent,child);
//方式三:根據父目錄物件以及字串形式的子目錄物件獲取File物件
File file3 = new File(new File(parent),child);
2.1常用方法
- createNewFile():創建檔案 ,
- mkdir()和mkdirs():創建單極目錄和多級目錄,
- isDirectory():判斷File物件是否為目錄,
- isFile():判斷File物件是否為檔案,
- exists():判斷File物件是否存在,
- getAbsolutePath():獲取絕對路徑,
- getPath():獲取檔案的相對路徑,
- getName():獲取檔案名,
- list():獲取指定目錄下所有檔案(夾)名稱陣列,
- listFiles():獲取指定目錄下所有檔案(夾)File陣列,
//在 E:\test\ 檔案夾下創建 1.txt 檔案
File file1 = new File("E:\\test\\2.txt");
Boolean flag = file5.createNewFile();
System.out.println(flag);
//在 E:\test\ 下創建 test1 檔案夾,創建單極目錄
File file2 = new File("E:\\test\\test1");
file2.mkdir();
//在 E:\test\ 下創建 a\b\c 檔案夾,創建多極目錄
File file3 = new File("E:\\test\\a\\b\\c");
file7.mkdirs(); //創建多級目錄
System.out.println("------------------測驗判斷功能------------------");
File file4 = new File("E:\\test\\a\\b\\c");
file8.mkdirs();
System.out.println("測驗file8是否是檔案夾"+file8.isDirectory());
System.out.println("測驗file8是否是檔案"+file8.isFile());
System.out.println("測驗file8是否存在"+file8.exists());
System.out.println("------------------測驗獲取功能------------------");
File file5 = new File("a/1.txt");
//獲取絕對路徑
String path1 = file9.getAbsolutePath();
System.out.println("絕對路徑: "+path1);
//獲取相對路徑
String path2 = file9.getPath();
System.out.println("相對路徑:"+path2);
//獲取檔案名
String filename = file9.getName();
System.out.println("檔案名:"+filename);
System.out.println("------------------------------------");
//獲取lib檔案夾下所有檔案(夾)的:名稱陣列String[]
File file6 = new File("a");
String[] names = file6.list();
for (String name:names){
System.out.println(name);
}
//獲取lib檔案夾下所有檔案(夾)的:File物件陣列 File[]
File[] files = file6.listFiles();
for(File file:files){
System.out.println(file);
}
3.字符流
3.1 字符輸入流
字符流讀資料 – 按單個字符讀取
讀取字符:read方法,每次可以讀取一個字符的資料,提升為int型別,讀取到檔案末尾,回傳-1,回圈讀取,代碼使用演示:
public class TestReader {
public static void main(String[] args) throws Exception {
//通過字符流讀取資料
Reader reader = new FileReader("a/1.txt");
int ch;
while ((ch = reader.read()) != -1){
System.out.println((char)ch);
}
//關閉資源
reader.close();
}
}
字符流讀資料 – 按字符陣列讀取
public class TestReader2 {
public static void main(String[] args) throws IOException {
//通過字符陣列讀取資料
Reader reader = new FileReader("a/1.txt");
char[] chs = new char[3];
int len = 0;
// 讀取字符到陣列中,回傳讀取的字符數,若到達流的末尾,回傳-1
while ((len = reader.read(chs))!=-1){
String s = new String(chs,0,len);
System.out.println(s);
}
reader.close();
}
}
3.2字符輸出流
字符流寫資料 – 按單個字符寫入
public class TestWrite {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建流物件
FileWriter fw = new FileWriter("1.txt");
// 寫出資料
fw.write(97); // 寫出第1個字符
fw.write('b'); // 寫出第2個字符
fw.write('C'); // 寫出第3個字符
//關閉資源,
// fw.close();
}
}
字符流寫資料 – 按字符陣列寫入
public class Testwriter {
public static void main(String[] args) throws IOException {
Writer writer = new FileWriter("a/1.txt");
//每寫入一次會覆寫之前的內容
char[] chars = {'女','朋','朋','很','帥'};
writer.write(chars,0,3);
writer.write("好好學習");
writer.close();
}
}
3.3 FileReader和FileWriter完成文本復制
每次讀取一個字符,效率太低,因此我們一般都采用陣列進行讀寫操作,
public class CopyFile {
public static void main(String[] args) throws IOException {
//將 1.txt 檔案中的內容拷貝到 2.txt 中
//創建輸入流物件
FileReader fileReader = new FileReader("1.txt");
//創建輸出流物件
FileWriter fileWriter = new FileWriter("2.txt");
int len;
char[] chars = new char[1024];
while ((len = fileReader.read(chars))!=-1){ //讀資料
//寫資料
fileWriter.write(chars,0,len);
}
fileReader.close();
fileWriter.close();
}
}
最后強調:
字符流,只能操作文本檔案,不能操作圖片,視頻等非文本檔案,當我們單純讀或者寫文本檔案時使用字符流其他情況使用位元組流
3.4 字符緩沖流檔案拷貝標準代碼
字符緩沖流自帶有緩沖區,大小為8192個字符,也就是16KB,底層是按照字符陣列進行讀取的,在實際中我們一般使用字符緩沖流,
public class 拷貝檔案標準代碼 {
public static void main(String[] args) throws IOException {
//字符緩沖流自帶有緩沖區,大小為8192個字符,也就是16KB,底層是按照字符陣列進行讀取的
//1.創建字符緩沖輸入流讀檔案物件
BufferedReader br = new BufferedReader(new FileReader("a/1.txt"));
//1.創建字符緩沖輸出流讀檔案物件
BufferedWriter bw = new BufferedWriter(new FileWriter("a/2.txt"));
int len;
while ((len = br.read())!=-1){
bw.write(len);
}
br.close();
bw.close();
}
4.位元組流
4.1 構造方法
FileInputStream的構造方法:
//通過打開與實際檔案的連接來創建一個 FileInputStream ,該檔案由檔案系統中的 File物件 file命名,
FileInputStream(File file)
//通過打開與實際檔案的連接來創建一個 FileInputStream ,該檔案由檔案系統中的路徑名name命名,
FileInputStream(String name)
FileOutputStream的構造方法:
//根據File物件為引數創建物件,
public FileOutputStream(File file)
//根據名稱字串為引數創建物件,
public FileOutputStream(String name)
一般使用第二種構造方法,
位元組流的用法和字符流大體一致,因此在這里我們直接進行應用,并且字符流無法對圖片進行讀寫操作,下面我們用位元組流對圖片進行讀寫操作,
4.2 位元組流拷貝檔案 – 按單個位元組讀寫
public class CopyFile{
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建位元組流讀檔案物件
FileInputStream fls = new FileInputStream("1.png");
// 使用檔案名稱創建位元組流寫檔案物件
FileOutputStream fos = new FileOutputStream("2.png");
int len;
while ((len = fls.read())!=-1){
fos.write(len);
}
fls.close();
fos.close();
}
}
4.3 位元組流拷貝檔案 – 按位元組陣列讀寫
在開發中一般強烈推薦使用陣列讀取檔案,代碼如下:
public class CopyFile {
public static void main(String[] args) throws IOException {
// 使用檔案名稱創建位元組流讀檔案物件
FileInputStream fls = new FileInputStream("1.png");
// 使用檔案名稱創建位元組流寫檔案物件
FileOutputStream fos = new FileOutputStream("2.png");
int len;
//定義位元組陣列,每次讀取1024個位元組
byte[] bytes = new byte[1024];
while ((len = fls.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fls.close();
fos.close();
}
}
5.緩沖流
緩沖流的基本原理:
- 使用了底層流物件從具體設備上獲取資料,并將資料存盤到緩沖區的陣列內,
- 通過緩沖區的read()方法從緩沖區獲取具體的字符資料,這樣就提高了效率,
- 如果用read方法讀取字符資料,并存盤到另一個容器中,直到讀取到了換行符時,將另一個容器臨時存盤的資料轉成字串回傳,就形成了readLine()功能,
也就是說在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖區讀寫,減少系統IO次數,從而大大提高讀寫的效率,
5.1位元組緩沖流
構造方法
//創建一個新的緩沖輸入流,注意引數型別為InputStream,
public BufferedInputStream(InputStream in)
//創建一個新的緩沖輸出流,注意引數型別為OutputStream,
public BufferedOutputStream(OutputStream out)
位元組緩沖流拷貝檔案的標準代碼
public class CopyFile{
public static void main(String[] args) throws IOException {
//記錄開始時間
long start = System.currentTimeMillis();
//創建位元組緩沖流物件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.png"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.png"));
int len;
while ((len = bis.read())!=-1){ //讀資料
//寫資料
bos.write(len);
}
bis.close();
bos.close();
//記錄結束時間
long end = System.currentTimeMillis();
System.out.println("緩沖流使用陣列復制時間:"+(end - start)+" 毫秒");
}
}
那么能不能更快呢,答案是可以的,下面我們使用位元組緩沖流和位元組陣列進行讀寫操作,代碼如下:
public class CopyFile{
public static void main(String[] args) throws IOException {
//記錄開始時間
long start = System.currentTimeMillis();
//創建位元組緩沖流物件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.png"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.png"));
int len;
byte[] bytes = new byte[2048];
while ((len = bis.read(bytes))!=-1){ //讀資料
//寫資料
bos.write(bytes,0,len);
}
bis.close();
bos.close();
//記錄結束時間
long end = System.currentTimeMillis();
System.out.println("緩沖流使用陣列復制時間:"+(end - start)+" 毫秒");
}
}
5.2字符緩沖流
構造方法
//創建一個新的緩沖輸入流,注意引數型別為Reader,
public BufferedReader(Reader in)
//創建一個新的緩沖輸出流,注意引數型別為Writer,
public BufferedWriter(Writer out)
字符緩沖流特有方法
字符緩沖流的使用方法和位元組緩沖流的方法基本一致,但字符緩沖流具有一些特有的方法,
- BufferedReader:
public String readLine():讀一行資料, 讀取到最后回傳null, - BufferedWriter:
public void newLine(): 換行,由系統屬性定義符號,
字符緩沖流拷貝檔案的標準代碼
我們使用字符緩沖流和位元組陣列進行讀寫操作
public class 拷貝檔案標準代碼 {
public static void main(String[] args) throws IOException {
//記錄開始時間
long start = System.currentTimeMillis();
//1.創建字符緩沖輸入流讀檔案物件
BufferedReader br = new BufferedReader(new FileReader("1.txt"));
//1.創建字符緩沖輸出流讀檔案物件
BufferedWriter bw = new BufferedWriter(new FileWriter("2.txt"));
int len;
char[] chars = new char[1024];
while ((len = br.read(chars))!=-1){ //讀資料
//寫資料
bw.write(chars,0,len);
}
br.close();
bw.close();
//記錄結束時間
long end = System.currentTimeMillis();
System.out.println("緩沖流使用陣列復制時間:"+(end - start)+" 毫秒");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/262193.html
標籤:java
上一篇:Mybatis入門
