Java基礎(chǔ)之I/O流詳解
總結(jié)一下Java I/O文件讀寫基本類相關(guān)知識(shí)和概念,對(duì)于程序設(shè)計(jì)者來說,創(chuàng)建一個(gè)好的輸入/輸出系統(tǒng)是一項(xiàng)艱難的任務(wù),其中挑戰(zhàn)來源于所有的可能性,不僅存在各種源端 與接收端(文件,控制臺(tái),網(wǎng)絡(luò)鏈接等),而且還需要以各種不同的方式與它們通信(順序,隨機(jī)存取,緩沖,二進(jìn)制,按字符,按行,按字等)。
Java I/O主要包括如下幾個(gè)層次:
- File(文件特征與管理):用于文件或者目錄的描述信息,例如生成新目錄,修改文件名,刪除文件,判斷文件所在路徑等。
- InputStream(二進(jìn)制格式操作):抽象類,基于字節(jié)的輸入操作,是所有輸入流的父類。定義了所有輸入流都具有的共同特征。
- OutputStream(二進(jìn)制格式操作):抽象類?;谧止?jié)的輸出操作。是所有輸出流的父類。定義了所有輸出流都具有的共同特征。Java中字符是采用Unicode標(biāo)準(zhǔn),一個(gè)字符是16位,即一個(gè)字符使用兩個(gè)字節(jié)來表示。為此,JAVA中引入了處理字符的流。
- Reader(文件格式操作):抽象類,基于字符的輸入操作。
- Writer(文件格式操作):抽象類,基于字符的輸出操作。
- RandomAccessFile(隨機(jī)文件操作):它的功能豐富,可以從文件的任意位置進(jìn)行存?。ㄝ斎胼敵觯┎僮?。
1. File
它是獨(dú)立于系統(tǒng)平臺(tái)的,利用其構(gòu)造函數(shù)創(chuàng)建出相應(yīng)的File 對(duì)象;再調(diào)用其中的方法實(shí)現(xiàn)對(duì)文件的各個(gè)屬性方面的操作。
構(gòu)造函數(shù):
- File( String path)
- File(String path, String FileName)
- File(File dir, String name)
用途:File類提供了一種與機(jī)器無關(guān)的方式來描述一個(gè)文件對(duì)象的屬性,通過類File所提供的方法,可以得到文件或目錄的描述信息,這主要包括名稱、所在路經(jīng)、可讀性、可寫性、文件的長(zhǎng)度等,還可以生成新的目錄、改變文件名、刪除文件、列出一個(gè)目錄中所有的文件等。
- public static void main(String[] args) throws IOException {
- File f = new File("dir");
- f.createNewFile();// 創(chuàng)建一個(gè).txt這個(gè)文件
- f.mkdir();// 創(chuàng)建一個(gè)名為.txt的目錄
- /*
- * 使用絕對(duì)路徑
- *
- * File f=new File("D:\\dir\\src\\A.java");
- *
- * f.createNewFile();
- */
- /*
- * 跨平臺(tái)使用
- *
- * 根據(jù)不同操作系統(tǒng)獲得對(duì)應(yīng)的分隔符 File fDir=new File(File.separator);
- *
- * String strFile="dir"+File.separator+"src"+File.separator +"A.java";
- *
- * File f=new File(fDir,strFile);
- *
- * f.createNewFile();
- *
- * f.delete();//刪除文件或目錄
- *
- * //f.deleteOnExit();
- */
- /*
- * 在缺省的臨時(shí)文件目錄下創(chuàng)建臨時(shí)文件
- *
- * for(int i=0;i<5;i++)
- *
- * {
- *
- * File f=File.createTempFile("winTemp",".tmp");
- *
- * f.deleteOnExit();//退出時(shí)刪除
- *
- *
- *
- * }
- */
- /*
- * 列出指定目錄下所有子目錄及文件的名稱
- */
- File fDir = new File(File.separator);
- String strFile = "dir" + File.separator + "src";
- File f = new File(fDir, strFile);
- String[] names = f.list();
- for (int i = 0; i < names.length; i++) {
- System.out.println(names[i]);
- }
- // 有過濾器的情況FilenameFilter是個(gè)接口
- File dir = new File(File.separator);
- String filepath = "dir" + File.separator + "src";
- /**
- * dir
- * 上級(jí)抽象路徑,如果dir為null,那么程序?qū)⒆詣?dòng)調(diào)用單個(gè)參數(shù)的File構(gòu)造方法,同時(shí)將filepath路徑應(yīng)用到File但構(gòu)造參數(shù)
- * 如果dir為//,則此路徑為本文件所在磁盤根目錄
- */
- File f = new File(dir, filepath);
- if (f.exists()) {
- } else {
- f.mkdirs();
- }
- String[] names = f.list(new FilenameFilter() { // 實(shí)現(xiàn)了FilenameFilter接口的匿名類,實(shí)現(xiàn)accept方法過濾文件
- @Override
- public boolean accept(File dir, String name) {
- System.out.println(name.indexOf(".java"));
- return name.indexOf(".java") != -1;
- }
- });
- for (int i = 0; i < names.length; i++) {
- System.out.println(names[i]);
- }
- }
#p#
- public static void main(String[] args) throws IOException {
- File f = new File("dir");
- f.createNewFile();// 創(chuàng)建一個(gè).txt這個(gè)文件
- f.mkdir();// 創(chuàng)建一個(gè)名為.txt的目錄
- /*
- * 使用絕對(duì)路徑
- *
- * File f=new File("D:\\dir\\src\\A.java");
- *
- * f.createNewFile();
- */
- /*
- * 跨平臺(tái)使用
- *
- * 根據(jù)不同操作系統(tǒng)獲得對(duì)應(yīng)的分隔符 File fDir=new File(File.separator);
- *
- * String strFile="dir"+File.separator+"src"+File.separator +"A.java";
- *
- * File f=new File(fDir,strFile);
- *
- * f.createNewFile();
- *
- * f.delete();//刪除文件或目錄
- *
- * //f.deleteOnExit();
- */
- /*
- * 在缺省的臨時(shí)文件目錄下創(chuàng)建臨時(shí)文件
- *
- * for(int i=0;i<5;i++)
- *
- * {
- *
- * File f=File.createTempFile("winTemp",".tmp");
- *
- * f.deleteOnExit();//退出時(shí)刪除
- *
- *
- *
- * }
- */
- /*
- * 列出指定目錄下所有子目錄及文件的名稱
- */
- File fDir = new File(File.separator);
- String strFile = "dir" + File.separator + "src";
- File f = new File(fDir, strFile);
- String[] names = f.list();
- for (int i = 0; i < names.length; i++) {
- System.out.println(names[i]);
- }
- // 有過濾器的情況FilenameFilter是個(gè)接口
- File dir = new File(File.separator);
- String filepath = "dir" + File.separator + "src";
- /**
- * dir
- * 上級(jí)抽象路徑,如果dir為null,那么程序?qū)⒆詣?dòng)調(diào)用單個(gè)參數(shù)的File構(gòu)造方法,同時(shí)將filepath路徑應(yīng)用到File但構(gòu)造參數(shù)
- * 如果dir為//,則此路徑為本文件所在磁盤根目錄
- */
- File f = new File(dir, filepath);
- if (f.exists()) {
- } else {
- f.mkdirs();
- }
- String[] names = f.list(new FilenameFilter() { // 實(shí)現(xiàn)了FilenameFilter接口的匿名類,實(shí)現(xiàn)accept方法過濾文件
- @Override
- public boolean accept(File dir, String name) {
- System.out.println(name.indexOf(".java"));
- return name.indexOf(".java") != -1;
- }
- });
- for (int i = 0; i < names.length; i++) {
- System.out.println(names[i]);
- }
- }
- public static void main(String[] args) throws IOException {
- File f = new File("dir");
- f.createNewFile();// 創(chuàng)建一個(gè).txt這個(gè)文件
- f.mkdir();// 創(chuàng)建一個(gè)名為.txt的目錄
- /*
- * 使用絕對(duì)路徑
- *
- * File f=new File("D:\\dir\\src\\A.java");
- *
- * f.createNewFile();
- */
- /*
- * 跨平臺(tái)使用
- *
- * 根據(jù)不同操作系統(tǒng)獲得對(duì)應(yīng)的分隔符 File fDir=new File(File.separator);
- *
- * String strFile="dir"+File.separator+"src"+File.separator +"A.java";
- *
- * File f=new File(fDir,strFile);
- *
- * f.createNewFile();
- *
- * f.delete();//刪除文件或目錄
- *
- * //f.deleteOnExit();
- */
- /*
- * 在缺省的臨時(shí)文件目錄下創(chuàng)建臨時(shí)文件
- *
- * for(int i=0;i<5;i++)
- *
- * {
- *
- * File f=File.createTempFile("winTemp",".tmp");
- *
- * f.deleteOnExit();//退出時(shí)刪除
- *
- *
- *
- * }
- */
- /*
- * 列出指定目錄下所有子目錄及文件的名稱
- */
- File fDir = new File(File.separator);
- String strFile = "dir" + File.separator + "src";
- File f = new File(fDir, strFile);
- String[] names = f.list();
- for (int i = 0; i < names.length; i++) {
- System.out.println(names[i]);
- }
- // 有過濾器的情況FilenameFilter是個(gè)接口
- File dir = new File(File.separator);
- String filepath = "dir" + File.separator + "src";
- /**
- * dir
- * 上級(jí)抽象路徑,如果dir為null,那么程序?qū)⒆詣?dòng)調(diào)用單個(gè)參數(shù)的File構(gòu)造方法,同時(shí)將filepath路徑應(yīng)用到File但構(gòu)造參數(shù)
- * 如果dir為//,則此路徑為本文件所在磁盤根目錄
- */
- File f = new File(dir, filepath);
- if (f.exists()) {
- } else {
- f.mkdirs();
- }
- String[] names = f.list(new FilenameFilter() { // 實(shí)現(xiàn)了FilenameFilter接口的匿名類,實(shí)現(xiàn)accept方法過濾文件
- @Override
- public boolean accept(File dir, String name) {
- System.out.println(name.indexOf(".java"));
- return name.indexOf(".java") != -1;
- }
- });
- for (int i = 0; i < names.length; i++) {
- System.out.println(names[i]);
- }
- }
#p#
RandomAccessFile(隨機(jī)文件讀寫類):
(1)RandomAccessFile類:它直接繼承于Object類而非InputStream/OutputStream類,從而可以實(shí)現(xiàn)讀寫文件中任何位置中的數(shù)據(jù)(只需要改變文件的讀寫位置的指針)。
(2)由于RandomAccessFile類實(shí)現(xiàn)了DataOutput與DataInput接口,因而利用它可以讀寫Java中的不同類型的基本類型數(shù)據(jù)(比如采用readLong()方法讀取長(zhǎng)整數(shù),而利用 readInt()方法可以讀出整數(shù)值等)。
RandomFileRW.java
- import java.io.IOException;
- import java.io.RandomAccessFile;
- public class RandomFileRW {
- public static void main(String args[]) {
- StringBuffer buf = new StringBuffer();
- char ch;
- try {
- while ((ch = (char) System.in.read()) != '\n') {
- buf.append(ch);
- }
- // 讀寫方式可以為"r" or "rw"
- /**
- * @param mode 1. r 2. rw 3. rws 4. rwd
- * "r" Open for reading only. Invoking any of the write methods of the resulting object will
- * cause an IOException to be thrown.
- * "rw" Open for reading and writing. If the file does not already exist then an attempt will
- * be made to create it.
- * "rws" Open for reading and writing, as with "rw", and also require that every update to the
- * file's content or metadata be written synchronously to the underlying storage device.
- * "rwd" Open for reading and writing, as with "rw", and also require that every update to the
- * file's content be written synchronously to the underlying storage device.
- */
- RandomAccessFile myFileStream = new RandomAccessFile("c:\\UserInput.txt", "rw");
- myFileStream.seek(myFileStream.length());
- myFileStream.writeBytes(buf.toString());
- // 將用戶從鍵盤輸入的內(nèi)容添加到文件的尾部
- myFileStream.close();
- } catch (IOException e) {
- }
- }
- }
原文鏈接http://blog.csdn.net/zhouzhiwengang/article/details/10427717: