基礎知識——Java檔案IO

來源:互聯網
上載者:User

標籤:

建立檔案

??

File file=new File("c:/test.txt");

if (!file.exists()) {

try {

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

??

得到目錄下檔案名稱列表

??

同樣是file,傳入目錄的路徑即可,得到名為dir的File類,利用dir的isDirector方法即可判斷是否為目錄

??

然後用file的list()方法得到目錄下的所有檔案名稱,利用dir的getpath獲得目錄路徑,利用File類的separator方法得到分隔字元,再加上檔案名稱即可得到檔案的全路徑名

??

寫一個複製檔案的程式

??

主要用到FileInputStream以及FileOutputStream,用到前者的read方法和後者的write方法,另外還要用到一個byte數組用於存放讀進來的資料

??

FileInputStream fis=new FileInputStream("c:/test.txt");

FileOutputStream fos=new FileOutputStream("c:/test_out.txt");

byte[] buff=new byte[1024];

int len=0;

while ((len=fis.read(buff))>0) {

fos.write(buff);

}

fis.close();

fos.close();

??

Stream類

??

根據資料的格式不同,可以分為位元組流和字元流

??

位元組流的處理方式

??

Java中的基礎位元組輸入資料流和輸出資料流是InputStream和OutputStream,通過它們衍生出FileInputStream和FileOutputStream;ObjectInputStream和ObjectOutputStream;BufferedInputStream和BufferedOutputStream;等

??

位元組流的一個最大的特點就是,每次的輸入和輸出都是一個位元組,而電腦處理資料也總是以一個位元組為基本單位的

??

所以它應用在最原始的流的處理上,比如記憶體快取作業,檔案複製等不關心流的內容具體是什麼

??

但是在處理一些具體資料格式的時候,比如文字檔,就需要用到字元流,在這種情況下字元流的效率就要比位元組流高很多

??

字元流的處理方式

??

字元流是由位元組流封裝而來,它的輸入和輸出資料流類型包括StringReader和StringWriter以及BufferedReader和BufferedWriter,對於前者的使用方法上來說根位元組流的類似,還是用read和write方法,而對於後者,多了一個針對文本資料的高效方法,那就是readline方法

??

BufferedReader需要用到InputStreamReader作為輸入,InputStreamReader將位元組流轉換為字元流,構造InputStreamReader對象時需要用到輸入資料流InputStream以及字元編碼作為參數

??

InputStream iStream=new FileInputStream("c:/test.txt");

InputStreamReader isReader=new InputStreamReader(iStream);

BufferedReader bReader=new BufferedReader(isReader);

String line=null;

StringBuffer sBuffer=new StringBuffer();

while ((line=bReader.readLine())!=null) {

//System.out.println(line);

sBuffer.append(line);

}

System.out.println(sBuffer);

bReader.close();

??

??

序列化和還原序列化一個對象

??

主要用到ObjectInputStream和ObjectOutputStream,另外要序列化的類要繼承Serializable介面

??

class Student implements Serializable {

private static final long serialVersionUID = 1L;

??

然後在需要序列化和還原序列化的地方用ObjectInputStream和ObjectOutputStream就可以了,注意輸入輸出的參數,還是要利用一個路徑字串建立一個位元組流

??

Student student = new Student("Tong", 23);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(

"c:/objTest.dat"));

oos.writeObject(student);

oos.close();

ObjectInputStream ois = new ObjectInputStream(new FileInputStream(

"c:/objTest.dat"));

Student student2 = (Student) ois.readObject();

System.out.println(student2.toString());

??

基礎知識——Java檔案IO

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.