一、概述
????????IO流是用來傳輸數(shù)據(jù)的,如文件的讀寫,有字節(jié)流和字符流之分,字節(jié)流可以處理任何文件數(shù)據(jù),字符流只能處理純文本文件,這里說的是字符流。
????????1、字符流分類:
????????????字符輸入流:FileReader、BufferedReader
????????????字符輸出流:FileWriter、BufferedWriter
????????2、FileWriter使用:
????????????創(chuàng)建:FileWriter fw? =? new FileWriter("xxx.txt");
????????????寫入:fw.write("Hoello");
????????????關(guān)閉:fw.close();
????????3、追加寫入:
????????????FileWriter fw? =? new FileWriter("xxx.txt",true);? ? //第二個(gè)參數(shù)為 true
????????4、實(shí)現(xiàn)換行
????????????在Windows中: \r\n
????????????在macOS中:\r或\n
????????????在Linux中:\n
????????5、五種writer方法的重載
????????????寫字符串的整體:public? void? writer(String? str);
????????????寫字符串的部分:public? void? writer(String? str,int? offset,int? count);
????????????寫字符數(shù)組的整體:public? void? writer(Char[]? array);
????????????寫字符數(shù)組的部分:public? void? writer(Char[]? array,int? offset,int? count);
????????????寫單個(gè)字符對(duì)應(yīng)的數(shù)字(參考ASCII碼或Unicode表):public? void? writer(int? ch);
????????6、FileReader的使用
????????????創(chuàng)建:FileReader? fr? =? new? FileReader("xxx.txt");
????????????讀單個(gè)字符:
????????????????int? ch;? ? //單個(gè)字符的ASCII值
????????????????while((ch = fr.read()) != -1)
????????????????{
? ? ????????????????//......
????????????????}
????????????讀字符數(shù)組:
????????????????char[]? buf? =? new? char[1024];
????????????????int? len;? ? //有效個(gè)數(shù)
????????????????while((len = fr.read(buf)) != -1)
????????????????{
? ? ????????????????String? str? =? new? String(buf,0,len);
????????????????}
? ? ? ? ? ?7、BufferedXxx和FileXxx的區(qū)別:
????????????????????BufferedXxx有一個(gè)8192長(zhǎng)度的char[]字符數(shù)組作為緩沖區(qū)
????????????????????BufferedXxx性能更高
????????????8、BufferedWriter額外方法:
????????????????????public? void? newLine(),根據(jù)操作系統(tǒng)自動(dòng)換行
????????????9、BufferedReader額外方法:
????????????????????public void newLine(),讀取一行字符串,不包含換行符
????????????10、代碼實(shí)例:
????????????????????FileWriter? &??FileReader
package File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Main {
? ? public static void main(String[] args) throws IOException {
? ? ? ? FileWriter fw1 = new FileWriter("test1.txt");? ? //創(chuàng)建FileWrite對(duì)象(覆蓋寫入)
? ? ? ? //重載一:寫字符串的整體
? ? ? ? fw1.write("Hello,my love!");? ? //調(diào)用writer方法寫數(shù)據(jù)
? ? ? ? fw1.write("\r\n");? ? ? //Windows系統(tǒng)中的換行方式
? ? ? ? fw1.write("LongRu\r\n");
? ? ? ? //重載二:寫字符串的部分
? ? ? ? String str = "Hello,my love,Longru!";
? ? ? ? fw1.write(str,14,7);
? ? ? ? fw1.write("\r\n");
? ? ? ? //重載三:寫字符數(shù)組的整體
? ? ? ? char[] array1 = {'H','e','l','l','o'};
? ? ? ? fw1.write(array1);
? ? ? ? fw1.write("\r\n");
? ? ? ? //重載四:寫字符數(shù)組的部分
? ? ? ? char[] array2 = {'M','y',' ','l','o','v','e'};
? ? ? ? fw1.write(array2,3,4);
? ? ? ? fw1.write("\r\n");
? ? ? ? //重載五:寫單個(gè)字符
? ? ? ? fw1.write(65);
? ? ? ? fw1.write(48);
? ? ? ? fw1.write("\r\n");
? ? ? ? fw1.close();? ? //調(diào)用close方法關(guān)閉流
? ? ? ? FileWriter fw2 = new FileWriter("test2.txt",true);? ? //創(chuàng)建FileWrite對(duì)象(追加寫入)
? ? ? ? fw2.write("Hello,my love!");? ? //調(diào)用writer方法寫數(shù)據(jù)
? ? ? ? fw2.write("LongRu");
? ? ? ? fw2.close();? ? //調(diào)用close方法關(guān)閉流
? ? ? ? System.out.println("==================================================");
? ? ? ? //重載一:讀取單個(gè)字符
? ? ? ? FileReader fr1 = new FileReader("test1.txt");
? ? ? ? int ch;
? ? ? ? while ((ch = fr1.read()) != -1)
? ? ? ? {
? ? ? ? ? ? System.out.println((char) ch);
? ? ? ? }
? ? ? ? //重載二:讀取指定長(zhǎng)度字符
? ? ? ? FileReader fr2 = new FileReader("test2.txt");
? ? ? ? char[] buf = new char[2];
? ? ? ? int len;? ? //代表有效個(gè)數(shù)
? ? ? ? while ((len = fr2.read(buf)) != -1)
? ? ? ? {
? ? ? ? ? ? String str3 = new String(buf,0,len);
? ? ? ? ? ? System.out.println(str3);
? ? ? ? }
? ? ? ? fr2.close();
? ? }
}
BufferedWriter? ?&? ?BufferedReader?
package BufferFile;
import java.io.*;
public class Main {
? ? public static void main(String[] ages) throws IOException {
? ? ? ? FileWriter fw = new FileWriter("test3.txt");? ? ? ? //創(chuàng)建BufferedWriter
? ? ? ? BufferedWriter bw = new BufferedWriter(fw);
? ? ? ? bw.write("hello");? ? ? //寫入
? ? ? ? bw.newLine();? ? ? //BufferedWriter特有的自動(dòng)根據(jù)操作系統(tǒng)換行功能
? ? ? ? bw.write("My love!");
? ? ? ? bw.close();? ? //關(guān)閉
? ? ? ? //讀單個(gè)字符,其實(shí)是從8192的緩沖區(qū)讀取單個(gè)字符
? ? ? ? FileReader fr = new FileReader("test3.txt");? ? ? ? // 創(chuàng)建BufferedReader
? ? ? ? BufferedReader br = new BufferedReader(fr);
? ? ? ? int ch;
? ? ? ? while ((ch = br.read()) != -1)
? ? ? ? {
? ? ? ? ? ? System.out.println((char)ch);
? ? ? ? }
? ? ? ? br.close();
? ? ? ? //BufferedReader提供了一個(gè)讀取一行字符串的方法
? ? ? ? FileReader fr2 = new FileReader("test1.txt");? ? ? ? // 創(chuàng)建BufferedReader
? ? ? ? BufferedReader br2 = new BufferedReader(fr2);
? ? ? ? String line;? ? ? ? //代表一行字符串
? ? ? ? while ((line = br2.readLine()) != null)
? ? ? ? {
? ? ? ? ? ? System.out.println(line);
? ? ? ? }
? ? }
}
二、File 類
????????1、File類的概述和作用
????????????File類是文件和目錄路徑名的抽象表示形式
????????????Java中把文件或者目錄(文件夾)都封裝成File對(duì)象
????????????在硬盤上操作文件,直接使用 File 類
????????2、File類靜態(tài)的成員變量
? ? ? ? ? ? ? ?pathSeparator:與系統(tǒng)有關(guān)的路徑分隔符,為了方便,它被表示為一個(gè)字符串
????????????????separator:與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符,為了方便,它被表示為一個(gè)字符串
public static void main(String[] args) {
? ? //File類靜態(tài)成員變量
? ? //與系統(tǒng)有關(guān)的路徑分隔符
? ? String separator = File.pathSeparator;
? ? System.out.println(separator);// 是一個(gè)分號(hào),目錄的分割(window中環(huán)境變量配置各個(gè)路徑用分號(hào)分割,表示一個(gè)完整的路徑結(jié)束)? Linux中是冒號(hào) :
? ? //與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符
? ? separator = File.separator;
? ? System.out.println(separator);// 向右 \? 目錄名稱分割? Linux /
}
????????????3、File 類的構(gòu)造方法
????????????????????File(String pathname):將路徑封裝File類型對(duì)象
????????????????????File(String parent,String child):根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 對(duì)象
????????????????????File(File parent,String child):將parent封裝成file對(duì)象
public static void main(String[] args){
? ? //將路徑封裝File類型對(duì)象
? ? String pathName = "F:\\TestPath";
? ? File f1 = new File(pathName);//將Test22文件封裝成File對(duì)象。注意;有可以封裝不存在文件或者文件夾,變成對(duì)象。
? ? System.out.println(f1);
? ? //根據(jù) parent 路徑名字符串和 child 路徑名字符串創(chuàng)建一個(gè)新 File 對(duì)象
? ? File f2 = new File("F:\\TestPath","a.txt");
? ? System.out.println(f2);
? ? //將parent封裝成file對(duì)象。
? ? File dir = new File("F:\\TestPath");
? ? File f3 = new File(dir,"a.txt");
? ? System.out.println(f3);
}
????????????4、File 類創(chuàng)建、刪除文件(文件、文件夾)
????????????????????public boolean createNewFile():創(chuàng)建文件 如果存在這樣的文件,就不創(chuàng)建了
????????????????????public boolean mkdir():創(chuàng)建文件夾 如果存在這樣的文件夾,就不創(chuàng)建了
????????????????????public boolean mkdirs():創(chuàng)建文件夾,如果父文件夾不存在,會(huì)幫你創(chuàng)建出來
????????????????????public boolean delete():刪除文件或者文件夾
public static void main(String[] args) throws IOException {
? ? //創(chuàng)建文件
? ? File F1 = new File("F:\\TestPath\\a.txt");
? ? boolean B1 = F1.createNewFile();
? ? System.out.println(B1);
? ? //創(chuàng)建文件夾
? ? File F2 = new File("F:\\TestPath\\a");
? ? boolean B2 = F2.mkdir();
? ? System.out.println(B2);
? ? //刪除文件
? ? File F3 = new File("F:\\TestPath\\a.txt");
? ? boolean B3 = F3.delete();
? ? System.out.println(B3);
}
????????????5、File 類獲取功能
????????????????????String getName():返回路徑中表示的文件或者文件夾名(獲取路徑中的最后部分的名字)
????????????????????long length():?返回路徑中表示的文件的字節(jié)數(shù)
????????????????????String getAbsolutePath():獲取絕對(duì)路徑,返回String對(duì)象
????????????????????File ? getAbsoluteFile() :獲取絕對(duì)路徑,返回File對(duì)象
????????????????????String getParent():獲取父路徑,返回String對(duì)象
????????????????????File getParentFile():獲取父路徑,返回File對(duì)象
public static void main(String[] args) throws IOException {
? ? File F1 = new File("F:\\TestPath\\a.txt");
? ? F1.createNewFile();
? ? String S1 = F1.getName();? //返回路徑中表示的文件或者文件夾名,即獲取路徑中最后部分的名字
? ? long L1 = F1.length();? ? ? //返回路徑中表示的文件的字節(jié)數(shù)
? ? File absolute = F1.getAbsoluteFile();? //獲取絕對(duì)路徑,返回File對(duì)象
? ? File parent = F1.getParentFile();? ? //獲取父路徑,返回File對(duì)象
? ? System.out.println(S1);
? ? System.out.println(L1);
? ? System.out.println(absolute);
? ? System.out.println(parent);
}
????????????6、File 類判斷功能
????????????????????boolean exists():判斷File構(gòu)造方法中封裝路徑是否存在
????????????????????boolean isDirectory():判斷File構(gòu)造方法中封裝的路徑是不是文件夾
????????????????????boolean isFile():判斷File構(gòu)造方法中封裝的路徑是不是文件
????????????7、File 類 list 獲取功能
????????????????????String[] list():獲取到File構(gòu)造方法中封裝的路徑中的文件和文件夾名 (遍歷一個(gè)目錄)
????????????????????File[] listFiles():獲取到,File構(gòu)造方法中封裝的路徑中的文件和文件夾名 (遍歷一個(gè)目錄)
????????????????????static File[] listRoots(): 列出可用的文件系統(tǒng)根?
public static void main(String[] args)
{
? ? File dir = new File("F:\\TestPath");
? ? //獲取的是目錄下的當(dāng)前的文件以及文件夾的名稱。
? ? String[] names = dir.list();
? ? for(String name : names){
? ? ? ? System.out.println(name);
? ? }
? ? System.out.println("========================");
? ? //獲取目錄下當(dāng)前文件以及文件對(duì)象,只要拿到了文件對(duì)象,那么就可以獲取其中想要的信息
? ? File[] files = dir.listFiles();
? ? for(File file : files){
? ? ? ? System.out.println(file);
? ? }
}
????????注意:
? ? ? ? ? ? ? 指定的目錄必須存在
? ? ? ? ? ? ? ?指定的必須是目錄,否則容易返回?cái)?shù)組null,出現(xiàn)NullPointerException
????????????8、文件過濾器
????????????????????使用文件過濾器可以刪選出某些關(guān)鍵字的文件或文件夾,即過濾一個(gè)目錄下的指定擴(kuò)展名的文件,或者包含某些關(guān)鍵字的文件夾
????????????????????常用方法:
????????????????????????????????public String[] list(FilenameFilter filter):
????????????????????????????????public File[] listFiles(FileFilter filter):
自定義過濾器類
//自定義過濾器實(shí)現(xiàn)FileFilter接口,重寫抽象方法
public class myFilter implements FileFilter {
? ? @Override
? ? public boolean accept(File pathname) {
? ? ? ? return pathname.getName().endsWith(".txt");
? ? }
}
在main中調(diào)用
public static void main(String[] args)
{
? ? File F = new File("F:\\TestPath");
? ? File[] filearr = F.listFiles(new myFilter());
? ? for(File f : filearr)
? ? {
? ? ? ? System.out.println(f);
? ? }
}
????????????分析:
????????????????listFiles()遍歷目錄的同時(shí),獲取到了文件名全路徑,調(diào)用過濾器的方法accept,將獲取到的路徑傳遞給accept方法的參數(shù)pathname
????????????????accept方法接收了參數(shù)pathname,參數(shù)是listFiles傳遞來的
????????????????在accept方法中,進(jìn)行判斷,如果這個(gè)路徑是txt文件,返回true,走著返回false
????????????????一旦方法返回了true,listFiles將路徑保存到File數(shù)組中
????????????9、遞歸全目錄,尋找指定文件
自定義過濾器
//自定義過濾器實(shí)現(xiàn)FileFilter接口,重寫抽象方法
public class myFilter implements FileFilter {
? ? public boolean accept(File pathname) {
? ? ? ? if(pathname.isDirectory()) {
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return pathname.getName().endsWith(".txt");
? ? }
}
在main中遞歸調(diào)用
public static void main(String[] args)
{
? ? File F = new File("F:\\TestPath");
? ? gitAllDir(F);? //遞歸遍歷所有目錄
? ? gitAllTxt(F);? ? //遞歸遍歷所有txt文件
}
public static void gitAllDir(File F)
{
? ? //System.out.println(F);
? ? //調(diào)用方法listFiles()對(duì)目錄,dir進(jìn)行遍歷
? ? File[] allFile = F.listFiles();
? ? for(File f : allFile)
? ? {
? ? ? ? //System.out.println(f);
? ? ? ? //判斷變量f表示的路徑是不是文件夾
? ? ? ? if(f.isDirectory())
? ? ? ? {
? ? ? ? ? ? //是一個(gè)目錄,就要去調(diào)用getAllDir遞歸遍歷這個(gè)目錄
? ? ? ? ? ? gitAllDir(f);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? System.out.println(f);
? ? ? ? }
? ? }
}
private static void gitAllTxt(File F)
{
? ? File[] fileTxt = F.listFiles(new myFilter());
? ? for(File f : fileTxt)
? ? {
? ? ? ? //System.out.println(f);
? ? ? ? if(f.isDirectory())
? ? ? ? {
? ? ? ? ? ? gitAllTxt(f);
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? System.out.println(f);
? ? ? ? }
? ? }
}