IO流的总结
Java I/O主要包括如下幾個(gè)層次:
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ī)器無(wú)關(guān)的方式來(lái)描述一個(gè)文件對(duì)象的屬性,通過(guò)類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]); } // 有過(guò)濾器的情況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方法過(guò)濾文件 @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]); } // 有過(guò)濾器的情況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方法過(guò)濾文件 @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]); } // 有過(guò)濾器的情況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方法過(guò)濾文件 @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]); } }
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ù)值等)。
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) { } } }
轉(zhuǎn)載于:https://www.cnblogs.com/haitang/p/4769444.html
總結(jié)
- 上一篇: 北京证券交易所将带来什么 中小板可能迎来
- 下一篇: C#生成时间戳