一、簡(jiǎn)介
1.文件的概念:數(shù)據(jù)在磁盤的唯一最小描述,上層應(yīng)用程序必須通過(guò)文件來(lái)操作磁盤上的數(shù)據(jù)
注:Java中的File不是真實(shí)文件
網(wǎng)絡(luò)下載的數(shù)據(jù)(文本 圖片 視頻 音頻)寫入緩存,讀取緩存中的數(shù)據(jù)需要流來(lái)實(shí)現(xiàn)
2.讀寫文件的步驟一般為
(1)創(chuàng)建文件、創(chuàng)建目錄(文件夾)
(2)判斷文件是不是存在(判斷目錄是否存在)
(3)寫入數(shù)據(jù)
(4)刪除文件、刪除目錄
3.寫入、讀取數(shù)據(jù)
Java中的File是沒有具體讀取和寫入的方法,需要通過(guò)輸入輸出流來(lái)操作
輸出流:內(nèi)存-外部(硬盤,網(wǎng)絡(luò),設(shè)備)
輸入流:外部-內(nèi)存
4.數(shù)據(jù)的存儲(chǔ)有兩種
(1)字節(jié)形式:圖片,音頻,視頻,exe,這些是以二進(jìn)制形式存儲(chǔ)的
(2)字符形式:文本
所以讀取方式有所不同
字節(jié)形式用字節(jié)流,一次讀取一個(gè)字節(jié),InputStream/OutputStream
字符形式用字符流,一次讀取一個(gè)字符(兩個(gè)字節(jié))Reader/Writer
輸入輸出流都是抽象類,所以我們需要使用輸入輸出流的具體實(shí)現(xiàn)類
字節(jié)輸入輸出流:FileInputStream/FileOutputStream
字符輸入輸出流:FileReader/FileWriter
二、字節(jié)流
類1——?jiǎng)?chuàng)建一個(gè)文件
class creatFile {
public static void creatFile() {
//關(guān)鍵字File,讓file指向一個(gè)對(duì)應(yīng)文件,需要文件名
//new File并不會(huì)自動(dòng)創(chuàng)建文件
File file = new File("C:\\Users\\Administrator\\Desktop\\1.text");
//判斷文件或者目錄是否存在
if (!file.exists()) {
//3.創(chuàng)建文件,捕獲異常
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
類2——?jiǎng)?chuàng)建一個(gè)目錄
class creatDir {
public static void creatDir() {
File file = new File("C:\\Users\\Administrator\\Desktop\\", "file");
if (!file.exists()) {
try {
file.mkdir();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}
類3——?jiǎng)h除文件
class deleteFile {
File file = new File("C:\\Users\\Administrator\\Desktop\\", "1.text");
public void deleteFile() {
if (file.exists()) {
file.delete();
}
}
}
類4——判斷是文件還是目錄
class FileOperation {
File file = new File("C:\\Users\\Administrator\\Desktop\\代碼");
public void operation() {
if (file.isFile()) {
System.out.println("是文件");
}
if (file.isDirectory()) {
System.out.println("是目錄");
}
}
}
類5——查看當(dāng)前文件的所有內(nèi)容
class List {
File file = new File("C:\\Users\\Administrator\\Desktop\\", "1.text");
String[] fileNameList = file.list();
public void List() {
for (String name : fileNameList) {
System.out.print(name);//輸出所有列表的名稱
}
}
}
類6——過(guò)濾文件(按要求篩選文件)
class Choose {
File file = new File("C:\\Users\\Administrator\\Desktop\\", "1.text");
public void choose() {
String[] chooseNameList = file.list(new FilenameFilter() {
//篩選文件需要實(shí)現(xiàn)FilenameFilter類,這里使用匿名內(nèi)部類減少代碼量
@Override
public boolean accept(File dir, String name) {
//如果返回true,當(dāng)前文件會(huì)被選中
//如果返回false,當(dāng)前文件會(huì)被過(guò)濾掉
File f = new File(dir, name);
if (f.isDirectory()) {
return true;
}
return false;
}
});
}
}
類7——寫入數(shù)據(jù)
class Write {
String des = "C:\\Users\\Administrator\\Desktop\\";
public static void WriteToFile(String des) {
//1.準(zhǔn)備數(shù)據(jù)
String text = "Hello World";
FileWriter fw = null;
//2.判斷是需要字符流還是字節(jié)流
try {
//3.寫入數(shù)據(jù)
fw = new FileWriter(des);
fw.write(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
類8——復(fù)制字節(jié)型文件
class CopyChar {
public static void CopyImageToFile(String src, String des) {
//1.將圖片讀取到內(nèi)存中
//字節(jié)輸入流FileInputStream
//2.將內(nèi)存中的數(shù)據(jù)寫入到磁盤中
//字節(jié)流 輸出流 FileOutputStream
//凡是實(shí)現(xiàn)了closeable接口的類,都可以在try()括號(hào)內(nèi)部創(chuàng)建對(duì)象
//當(dāng)try代碼塊執(zhí)行完畢或者有異常,系統(tǒng)自動(dòng)close
try (FileInputStream inf = new FileInputStream(src);
FileOutputStream outf = new FileOutputStream(des)) {
int b;
//byte[] bytes = new byte[1024];操作一個(gè)字節(jié)數(shù)組,解決read()效率低的問(wèn)題
while (true) {
b = inf.read();
//read();方法是一個(gè)一個(gè)字節(jié)的讀,當(dāng)文件較大時(shí),效率就會(huì)非常低
//b = inf.read(byte);
if (b == -1) {
break;
}//當(dāng)讀取完畢后返回值默認(rèn)為-1,操作字節(jié)數(shù)組時(shí)是一樣的
outf.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
類9——復(fù)制字符型文件
class CopyText {
public static void CopyTextToFile(String src, String des) {
//復(fù)制字符和復(fù)制字節(jié)方法是完全一樣的,只是關(guān)鍵字不一樣
try (FileReader inf = new FileReader(src);
FileWriter outf = new FileWriter(des)) {
int b;
//byte[] bytes = new byte[1024];操作一個(gè)字符數(shù)組
while (true) {
b = inf.read();
//b = inf.read(byte);
if (b == -1) {
break;
}
outf.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、處理流——緩沖流
緩沖輸入流BufferedInputStream BufferedReader
緩沖輸出流BufferedOutputStream BufferedWriter
雖然使用了處理流比較快,但是真正讀取數(shù)據(jù)的是節(jié)點(diǎn)流
1.必須先創(chuàng)建節(jié)點(diǎn)流對(duì)象,將數(shù)據(jù)從磁盤讀到內(nèi)存緩沖區(qū)
2.將內(nèi)存緩沖區(qū)的數(shù)據(jù)讀到處理流對(duì)應(yīng)的緩沖區(qū)
3.從處理流的緩沖區(qū)將數(shù)據(jù)讀取到對(duì)應(yīng)的地方
輸入輸出重定向——打印流(PrintStream)指定輸入輸出的位置(默認(rèn)是輸出到終端)
類1——用處理流復(fù)制字節(jié)
class BufferChar{
public void Operation(String scr,String des){
BufferedOutputStream bos = null;
try {
//創(chuàng)建緩沖輸入流
FileInputStream fis = new FileInputStream(scr);
BufferedInputStream bis = new BufferedInputStream(fis);
//創(chuàng)建緩沖輸出流
FileOutputStream fos = new FileOutputStream(des);
bos = new BufferedOutputStream(fos);
int b;
while((b = bis.read())!= -1){
bos.write(b);
}
bos.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
類2——用處理流復(fù)制字符
class BufferText{
public void Operation(String scr,String des){
BufferedWriter bw = null;
try{
//創(chuàng)建輸入流
FileInputStream fis = new FileInputStream(scr);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
//創(chuàng)建輸出流
FileOutputStream fos = new FileOutputStream(des);
OutputStreamWriter osw = new OutputStreamWriter(fos);
bw = new BufferedWriter(osw);
int b;
while((b = br.read())!= -1){
bw.write(b);
}
bw.flush();//當(dāng)使用處理讀取和輸出時(shí),需要調(diào)用flush()來(lái)刷新數(shù)據(jù)流
}catch (Exception e){
e.printStackTrace();
}finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
類3——重定向輸入輸出——在指定位置輸出、在指定位置輸入
class ReDirection{
public void redirection_out(String des){
FileOutputStream fos = null;
try {
fos = new FileOutputStream(des);
PrintStream ps = new PrintStream(fos);//重定向輸出,在指定位置輸出
System.setOut(ps);
System.out.println("Hello World");
}catch (Exception e){
e.printStackTrace();
}finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void redirection_in(String src){
FileInputStream fis = null;
try {
fis = new FileInputStream(src);
Scanner scanner = new Scanner(fis);//重定向輸入,在指定位置輸入
while (scanner.hasNext()){
//把所有行的內(nèi)容顯示出來(lái)(默認(rèn)只輸出一行,所以寫循環(huán))
System.out.println(scanner.next());
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、對(duì)象處理
把一個(gè)對(duì)象通過(guò)文件的方式存儲(chǔ)和讀取
class Save_Read_Object{
class Person implements Serializable{
int age;
String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
}
public void saveobject(String des) {
try {
FileOutputStream fos = new FileOutputStream(des);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Person p = new Person(18,"Jack");
oos.writeObject(p);
}catch (Exception e){
e.printStackTrace();
}
}//將對(duì)象保存在指定文件里面
public void readobject(String src){
try{
FileInputStream fis = new FileInputStream(src);
ObjectInputStream ois = new ObjectInputStream(fis);
Person p = (Person)ois.readObject();
}catch (Exception e){
e.printStackTrace();
}
}//從指定文件里面讀取對(duì)象
}
五、RandomAccessFile類
當(dāng)一個(gè)文件存在的時(shí)候,寫入數(shù)據(jù)時(shí)會(huì)把原來(lái)的文件覆蓋掉
如果不想發(fā)生此情況,需要使用RandomAccessFile
class RandromAccess{
public void randomaccess(String src){
try {
RandomAccessFile raf = new RandomAccessFile(src,"rw");
//有幾種模式mode:r,rw,rws
//r為只讀,rw為可讀寫,rws為同時(shí)讀寫
}catch (Exception e){
e.printStackTrace();
}
}
}