天天看點

File 類和檔案流

所在包:Java.io.File

  1. File與流無關,不能通過file完成檔案的讀寫
  2. File表示的是檔案和目錄路徑的抽象表現形式

File fiile = new File(“D:/java”);

輸出一下file,發現是一個字元串路徑

File 類和檔案流

判斷檔案是否存在

File.exists()

File既可以是檔案也可以是目錄

判斷file是檔案還是目錄

File.isFile(),file.isDirectory()

如果沒有,建立一個

If(!file.exists()){

File.createNewFile()//建立一個新檔案

File.mkdir()//建立一個目錄

File.mkdirs()//建立多個目錄

}

//擷取檔案名

File.getName();

//擷取絕對路徑

File.getAbsolutePath();

//周遊一個目錄下的多個檔案

Public static void method(File file){

If(file.isFile()){

Return ;

}

File[] files = file.listFiles();

Sysotem.out.println(f.getAbsolutePath());

//遞歸

Method(f);

}

File 類和檔案流

For(File f : files){

System.out.println(f.getAbsolutePath());

Method(f);

}

java中流一共有16個

有些流成對出現

傳輸方式:輸入流、輸出流

表現形式:位元組流(處理二進制對象、位元組)、字元流(處理字元、字元串)

1.檔案流(相當于水管流水)

FileInputStream()、FileOutputStream()--->輸入、輸出位元組流

FileReader、FileWriter輸入輸出字元流

2.緩沖流(相當于水缸用來屯水,在記憶體裡開辟空間 減少對硬碟的頻繁讀取)

BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

3.序列化流

ObjectInputStream、ObjectOutputStream

4.轉換流(将位元組流轉換為字元流)

InputStreamReader、OutputStreamWriter

5.資料流

DataInputStream、DataOutputStream

6.列印流

PrintWriter、PrintStream

一個一個讀取檔案的缺點:

  1. 效率低
  2. 頻繁的通路,對磁盤有傷害

Public static void main (String[] args) throws IOException{

//用檔案流對檔案進行操作,首先需要一個檔案

File file = new File(“D:/a.java”);

//有檔案流

FileInputStream fis = new FileInputStream(file);

//檔案的輸入輸出是以什麼為參照物的?  記憶體

Int i1 = fis.read();//---->傳回的是位元組

System.out.println(i1);//輸出的是a的ASCII碼----97

是以轉換為字元

System.out.println((char)i1);

Int i2 = fis.read();

System.out.println((char)i2);

}

File 類和檔案流

輸出的是a的ASCII碼----97

File 類和檔案流
File 類和檔案流

?的ASCII碼是-1,是以可以将其作為判斷條件

是以修改方法為循環讀,通過-1判斷

Public static void main (String[] args) throws IOException{

//用檔案流對檔案進行操作,首先需要一個檔案

File file = new File(“D:/a.java”);

//有檔案流

FileInputStream fis = new FileInputStream(file);

While(true){

Int tem = fis.read();

If(tem == -1){

Break;

}

System.out.println((char)tem);

Fis.close();

}

}

File 類和檔案流

Public static void main (String[] args) throws IOException{

//用檔案流對檔案進行操作,首先需要一個檔案

File file = new File(“D:/a.java”);

//有檔案流

FileInputStream fis = new FileInputStream(file);

//檔案的輸入輸出是以什麼為參照物的?  記憶體

While(fis.read() != -1){

System.out.println(char(fis.read()));

}

Fis.close();

}

File 類和檔案流

Public class FileInputStreamTest_2{

Public static void main(String[] args) throws IOException{

File file = new File(“D:/a.java”);

FileInputStream fis = new FileInputStream(file);

//每次讀三個

Byte[] bytes = new byte[3];

Int i1 = fis.read(bytes);

System.out.println(i1);//3---位元組數

System.out.println(new String(bytes));//abc

Int i2 = fis.read(bytes);

System.out.println(i2);//2---位元組數

System.out.println(new String(bytes));//dec

Int i3 = fis.read(bytes);

System.out.println(i3);//-1---位元組數,因為沒有了,是以輸出-1

//用循環

While(fis.read() != -1){

System.out.println(new String(bytes,0,fis.read()));

}

Fis.close();

}

}

File 類和檔案流
File 類和檔案流
File 類和檔案流
File 類和檔案流

Public class FileInputStreamTest_2{

Public static void main(String[] args) throws IOException{

File file = new File(“D:/a.java”);

FileInputStream fis = new FileInputStream(file);

//每次讀三個

Byte[] bytes = new byte[3];

//用循環

While(fis.read(bytes ) != -1){

System.out.println(new String(bytes,0,fis.read()));

}

Fis.close();

}

}

結果:adc,de

Public class FileInputStreamTest_2{

Public static void main(String[] args) throws IOException{

//讀自己

File file = new File(“這個檔案的路徑”);

FileInputStream fis = new FileInputStream(file);

Byte[] bytes = new Byte(1024);

Int temp;

While((temp = fis.read(bytes)) != -1){

System.out.println(new String(bytes,0,temp));

}

Fis.close();

}

}

File 類和檔案流

//輸出流

Public class FileOutputStreamTest_1{

Public static void main(String[] args) throws IOException{

String info = “你我”;

//首先有一個檔案接收,将字元串中的内容寫入檔案中

File file = new File(“D:/b.java”);//沒有檔案接收會自動建立

FileOutputStream fos = new FileOutputStream(file);

Byte[] bytes = info.getBytes();//擷取位元組

Fos.write(bytes);

//重新整理

Fos.flush();

//關閉

Fos.close();

}

}

File 類和檔案流

複制字元 代碼

public class CopyCharTest {

public static void main(String[] args) throws IOException{

//将d.java檔案中的内容複制到b.java檔案中

File file1 = new File("D:/b.java");

File file2 = new File("D:/d.java");

//讀b.java資料

FileReader fr = new FileReader(file1);

//寫資料

FileWriter fw = new FileWriter(file2);//覆寫

//FileWriter fw = new FileWriter(file2,true);//追加

char[] ch = new char[512];

int temp;

while ((temp = fr.read(ch)) != -1) {

//寫到接收的檔案中去

fw.write(ch,0,temp);

}

fr.close();

fw.close();

}

}