自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java基礎(chǔ)之I/O流詳解

開發(fā) 后端
總結(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)制,按字符,按行,按字等)。

總結(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è)層次:

  1.  File(文件特征與管理):用于文件或者目錄的描述信息,例如生成新目錄,修改文件名,刪除文件,判斷文件所在路徑等。
  2.  InputStream(二進(jìn)制格式操作):抽象類,基于字節(jié)的輸入操作,是所有輸入流的父類。定義了所有輸入流都具有的共同特征。
  3.  OutputStream(二進(jìn)制格式操作):抽象類?;谧止?jié)的輸出操作。是所有輸出流的父類。定義了所有輸出流都具有的共同特征。Java中字符是采用Unicode標(biāo)準(zhǔn),一個(gè)字符是16位,即一個(gè)字符使用兩個(gè)字節(jié)來表示。為此,JAVA中引入了處理字符的流。
  4.  Reader(文件格式操作):抽象類,基于字符的輸入操作。
  5.  Writer(文件格式操作):抽象類,基于字符的輸出操作。
  6.  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è)目錄中所有的文件等。

  1. public static void main(String[] args) throws IOException {   
  2.         File f = new File("dir");   
  3.    
  4.         f.createNewFile();// 創(chuàng)建一個(gè).txt這個(gè)文件   
  5.    
  6.         f.mkdir();// 創(chuàng)建一個(gè)名為.txt的目錄   
  7.    
  8.         /*  
  9.          * 使用絕對(duì)路徑  
  10.          *   
  11.          * File f=new File("D:\\dir\\src\\A.java");  
  12.          *   
  13.          * f.createNewFile();  
  14.          */   
  15.    
  16.         /*  
  17.          * 跨平臺(tái)使用  
  18.          *   
  19.          * 根據(jù)不同操作系統(tǒng)獲得對(duì)應(yīng)的分隔符 File fDir=new File(File.separator);  
  20.          *   
  21.          * String strFile="dir"+File.separator+"src"+File.separator +"A.java";  
  22.          *   
  23.          * File f=new File(fDir,strFile);  
  24.          *   
  25.          * f.createNewFile();  
  26.          *   
  27.          * f.delete();//刪除文件或目錄  
  28.          *   
  29.          * //f.deleteOnExit();  
  30.          */   
  31.    
  32.         /*  
  33.          * 在缺省的臨時(shí)文件目錄下創(chuàng)建臨時(shí)文件  
  34.          *   
  35.          * for(int i=0;i<5;i++)  
  36.          *   
  37.          * {  
  38.          *   
  39.          * File f=File.createTempFile("winTemp",".tmp");  
  40.          *   
  41.          * f.deleteOnExit();//退出時(shí)刪除  
  42.          *   
  43.          *   
  44.          *   
  45.          * }  
  46.          */   
  47.    
  48.         /*   
  49.          * 列出指定目錄下所有子目錄及文件的名稱   
  50.          */   
  51.         File fDir = new File(File.separator);   
  52.         String strFile = "dir" + File.separator + "src";   
  53.         File f = new File(fDir, strFile);   
  54.         String[] names = f.list();   
  55.         for (int i = 0; i < names.length; i++) {   
  56.             System.out.println(names[i]);   
  57.         }   
  58.    
  59.         // 有過濾器的情況FilenameFilter是個(gè)接口   
  60.         File dir = new File(File.separator);   
  61.    
  62.         String filepath = "dir" + File.separator + "src";   
  63.    
  64.         /**  
  65.          * dir  
  66.          * 上級(jí)抽象路徑,如果dir為null,那么程序?qū)⒆詣?dòng)調(diào)用單個(gè)參數(shù)的File構(gòu)造方法,同時(shí)將filepath路徑應(yīng)用到File但構(gòu)造參數(shù)  
  67.          * 如果dir為//,則此路徑為本文件所在磁盤根目錄  
  68.          */   
  69.         File f = new File(dir, filepath);   
  70.         if (f.exists()) {   
  71.         } else {   
  72.             f.mkdirs();   
  73.         }   
  74.    
  75.         String[] names = f.list(new FilenameFilter() { // 實(shí)現(xiàn)了FilenameFilter接口的匿名類,實(shí)現(xiàn)accept方法過濾文件   
  76.    
  77.                     @Override   
  78.                     public boolean accept(File dir, String name) {   
  79.                         System.out.println(name.indexOf(".java"));   
  80.                         return name.indexOf(".java") != -1;   
  81.                     }   
  82.                 });   
  83.    
  84.         for (int i = 0; i < names.length; i++) {   
  85.             System.out.println(names[i]);   
  86.         }   
  87.     }   

 #p#

  1. public static void main(String[] args) throws IOException {   
  2.         File f = new File("dir");   
  3.    
  4.         f.createNewFile();// 創(chuàng)建一個(gè).txt這個(gè)文件   
  5.    
  6.         f.mkdir();// 創(chuàng)建一個(gè)名為.txt的目錄   
  7.    
  8.         /*  
  9.          * 使用絕對(duì)路徑  
  10.          *   
  11.          * File f=new File("D:\\dir\\src\\A.java");  
  12.          *   
  13.          * f.createNewFile();  
  14.          */   
  15.    
  16.         /*  
  17.          * 跨平臺(tái)使用  
  18.          *   
  19.          * 根據(jù)不同操作系統(tǒng)獲得對(duì)應(yīng)的分隔符 File fDir=new File(File.separator);  
  20.          *   
  21.          * String strFile="dir"+File.separator+"src"+File.separator +"A.java";  
  22.          *   
  23.          * File f=new File(fDir,strFile);  
  24.          *   
  25.          * f.createNewFile();  
  26.          *   
  27.          * f.delete();//刪除文件或目錄  
  28.          *   
  29.          * //f.deleteOnExit();  
  30.          */   
  31.    
  32.         /*  
  33.          * 在缺省的臨時(shí)文件目錄下創(chuàng)建臨時(shí)文件  
  34.          *   
  35.          * for(int i=0;i<5;i++)  
  36.          *   
  37.          * {  
  38.          *   
  39.          * File f=File.createTempFile("winTemp",".tmp");  
  40.          *   
  41.          * f.deleteOnExit();//退出時(shí)刪除  
  42.          *   
  43.          *   
  44.          *   
  45.          * }  
  46.          */   
  47.    
  48.         /*   
  49.          * 列出指定目錄下所有子目錄及文件的名稱   
  50.          */   
  51.         File fDir = new File(File.separator);   
  52.         String strFile = "dir" + File.separator + "src";   
  53.         File f = new File(fDir, strFile);   
  54.         String[] names = f.list();   
  55.         for (int i = 0; i < names.length; i++) {   
  56.             System.out.println(names[i]);   
  57.         }   
  58.    
  59.         // 有過濾器的情況FilenameFilter是個(gè)接口   
  60.         File dir = new File(File.separator);   
  61.    
  62.         String filepath = "dir" + File.separator + "src";   
  63.    
  64.         /**  
  65.          * dir  
  66.          * 上級(jí)抽象路徑,如果dir為null,那么程序?qū)⒆詣?dòng)調(diào)用單個(gè)參數(shù)的File構(gòu)造方法,同時(shí)將filepath路徑應(yīng)用到File但構(gòu)造參數(shù)  
  67.          * 如果dir為//,則此路徑為本文件所在磁盤根目錄  
  68.          */   
  69.         File f = new File(dir, filepath);   
  70.         if (f.exists()) {   
  71.         } else {   
  72.             f.mkdirs();   
  73.         }   
  74.    
  75.         String[] names = f.list(new FilenameFilter() { // 實(shí)現(xiàn)了FilenameFilter接口的匿名類,實(shí)現(xiàn)accept方法過濾文件   
  76.    
  77.                     @Override   
  78.                     public boolean accept(File dir, String name) {   
  79.                         System.out.println(name.indexOf(".java"));   
  80.                         return name.indexOf(".java") != -1;   
  81.                     }   
  82.                 });   
  83.    
  84.         for (int i = 0; i < names.length; i++) {   
  85.             System.out.println(names[i]);   
  86.         }   
  87.     }   

 

  1. public static void main(String[] args) throws IOException { 
  2.         File f = new File("dir"); 
  3.  
  4.         f.createNewFile();// 創(chuàng)建一個(gè).txt這個(gè)文件 
  5.  
  6.         f.mkdir();// 創(chuàng)建一個(gè)名為.txt的目錄 
  7.  
  8.         /* 
  9.          * 使用絕對(duì)路徑 
  10.          *  
  11.          * File f=new File("D:\\dir\\src\\A.java"); 
  12.          *  
  13.          * f.createNewFile(); 
  14.          */ 
  15.  
  16.         /* 
  17.          * 跨平臺(tái)使用 
  18.          *  
  19.          * 根據(jù)不同操作系統(tǒng)獲得對(duì)應(yīng)的分隔符 File fDir=new File(File.separator); 
  20.          *  
  21.          * String strFile="dir"+File.separator+"src"+File.separator +"A.java"; 
  22.          *  
  23.          * File f=new File(fDir,strFile); 
  24.          *  
  25.          * f.createNewFile(); 
  26.          *  
  27.          * f.delete();//刪除文件或目錄 
  28.          *  
  29.          * //f.deleteOnExit(); 
  30.          */ 
  31.  
  32.         /* 
  33.          * 在缺省的臨時(shí)文件目錄下創(chuàng)建臨時(shí)文件 
  34.          *  
  35.          * for(int i=0;i<5;i++) 
  36.          *  
  37.          * { 
  38.          *  
  39.          * File f=File.createTempFile("winTemp",".tmp"); 
  40.          *  
  41.          * f.deleteOnExit();//退出時(shí)刪除 
  42.          *  
  43.          *  
  44.          *  
  45.          * } 
  46.          */ 
  47.  
  48.         /* 
  49.          * 列出指定目錄下所有子目錄及文件的名稱 
  50.          */ 
  51.         File fDir = new File(File.separator); 
  52.         String strFile = "dir" + File.separator + "src"
  53.         File f = new File(fDir, strFile); 
  54.         String[] names = f.list(); 
  55.         for (int i = 0; i < names.length; i++) { 
  56.             System.out.println(names[i]); 
  57.         } 
  58.  
  59.         // 有過濾器的情況FilenameFilter是個(gè)接口 
  60.         File dir = new File(File.separator); 
  61.  
  62.         String filepath = "dir" + File.separator + "src"
  63.  
  64.         /** 
  65.          * dir 
  66.          * 上級(jí)抽象路徑,如果dir為null,那么程序?qū)⒆詣?dòng)調(diào)用單個(gè)參數(shù)的File構(gòu)造方法,同時(shí)將filepath路徑應(yīng)用到File但構(gòu)造參數(shù) 
  67.          * 如果dir為//,則此路徑為本文件所在磁盤根目錄 
  68.          */ 
  69.         File f = new File(dir, filepath); 
  70.         if (f.exists()) { 
  71.         } else { 
  72.             f.mkdirs(); 
  73.         } 
  74.  
  75.         String[] names = f.list(new FilenameFilter() { // 實(shí)現(xiàn)了FilenameFilter接口的匿名類,實(shí)現(xiàn)accept方法過濾文件 
  76.  
  77.                     @Override 
  78.                     public boolean accept(File dir, String name) { 
  79.                         System.out.println(name.indexOf(".java")); 
  80.                         return name.indexOf(".java") != -1
  81.                     } 
  82.                 }); 
  83.  
  84.         for (int i = 0; i < names.length; i++) { 
  85.             System.out.println(names[i]); 
  86.         } 
  87.     } 

#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

  1. import java.io.IOException;   
  2. import java.io.RandomAccessFile;   
  3.    
  4. public class RandomFileRW {   
  5.    
  6.     public static void main(String args[]) {   
  7.         StringBuffer buf = new StringBuffer();   
  8.         char ch;   
  9.            
  10.         try {   
  11.             while ((ch = (char) System.in.read()) != '\n') {   
  12.                 buf.append(ch);   
  13.             }   
  14.                
  15.             // 讀寫方式可以為"r" or "rw"   
  16.                
  17.             /**  
  18.              * @param mode 1. r 2. rw 3. rws 4. rwd  
  19.              * "r" Open for reading only. Invoking any of the write methods of the resulting object will  
  20.              *      cause an IOException to be thrown.    
  21.              * "rw" Open for reading and writing. If the file does not already exist then an attempt will  
  22.              *      be made to create it.    
  23.              * "rws" Open for reading and writing, as with "rw", and also require that every update to the  
  24.              *      file's content or metadata be written synchronously to the underlying storage device.    
  25.              * "rwd"   Open for reading and writing, as with "rw", and also require that every update to the  
  26.              *      file's content be written synchronously to the underlying storage device.   
  27.              */   
  28.             RandomAccessFile myFileStream = new RandomAccessFile("c:\\UserInput.txt""rw");   
  29.             myFileStream.seek(myFileStream.length());   
  30.             myFileStream.writeBytes(buf.toString());   
  31.                
  32.             // 將用戶從鍵盤輸入的內(nèi)容添加到文件的尾部   
  33.             myFileStream.close();   
  34.         } catch (IOException e) {   
  35.         }   
  36.     }   
  37. }   

原文鏈接http://blog.csdn.net/zhouzhiwengang/article/details/10427717

責(zé)任編輯:陳四芳 來源: zhouzhiwengang的專欄
相關(guān)推薦

2013-05-28 10:08:41

IO輸出

2013-09-17 15:13:28

IO

2011-12-19 14:05:01

JavaIO

2013-09-17 13:43:51

IO

2020-06-03 17:30:42

LinuxIO

2017-01-19 19:14:20

Linux重定向命令

2010-05-11 13:36:50

Unix標(biāo)準(zhǔn)

2019-02-25 08:40:28

Linux磁盤IO

2022-12-08 09:10:11

I/O模型Java

2010-06-25 09:47:29

Linux系統(tǒng)監(jiān)控

2009-05-14 10:16:36

Oracle優(yōu)化磁盤

2022-10-12 23:39:46

Java接口屬性

2025-03-07 10:14:03

2015-08-10 14:39:46

Java 操作建議

2015-10-19 09:34:42

TCPIP網(wǎng)絡(luò)協(xié)議

2020-04-28 10:40:54

Python開發(fā)工具

2018-11-05 11:20:54

緩沖IO

2023-07-26 08:22:17

JavaIO流

2011-01-14 09:25:28

LinuxIO機(jī)制

2020-12-11 11:04:07

NettyIO
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)