乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      文件讀寫的基本類

       昵稱22369024 2015-05-02

      1.文件讀寫相關(guān)的類

      (1) java.io.InputStream: 抽象類,輸入字節(jié)流,可以將本輸入流中的字節(jié)讀取出來。

        主要方法: void read(byte[] b):從輸入流中讀取一定數(shù)量的字節(jié),將其存儲在緩沖區(qū)數(shù)組b中。

       

      (2) java.io.OutputStream: 抽象類,輸出字節(jié)流,可以接收待輸出的字節(jié)并將這些字節(jié)發(fā)送到某個接收器。

           主要方法: void write(byte[] b):接收byte數(shù)組中的字節(jié),將byte數(shù)組中的字節(jié)寫入此輸出流。

       

      (3) FileInputStream: 是InputStream的子類,文件輸入流,可以從文件系統(tǒng)中的某個文件中獲得輸入字節(jié)。

        主要方法: FileInputStream FileInputStream(File file):通過打開一個到實際文件的連接來創(chuàng)建一個文件輸入流,該文件通過File對象指定。

                void read(byte[] b):將此文件輸入流中將最多b.length個字節(jié)的數(shù)據(jù)讀入一個byte數(shù)組b中。

       

      (4) FileOutputStream: 是OutputStream的子類,文件輸出流,可以從byte數(shù)組中接收數(shù)據(jù)并將數(shù)據(jù)寫入某個文件。

        主要方法: FileOutputStream FileOutputStream(File file):創(chuàng)建一個準備向file所表示的文件中寫入數(shù)據(jù)的文件輸出流。

             void write(byte[] b):將byte數(shù)組b中的字節(jié)寫入此文件輸出流。

       

      (5) File: 文件或目錄路徑名的抽象表示形式,此類的實例可能表示實際的文件系統(tǒng)對象(一個文件或一個目錄),也可能不表示實際的文件系統(tǒng)對象。File類的實例時不可變的,一旦創(chuàng)建,其對象表示的抽象路徑名將永不改變。

        主要方法: File File(String pathname):通過將給定的路徑名字符串pathname轉(zhuǎn)換為抽象路徑來創(chuàng)建一個新的File實例。

              boolean delete():刪除此抽象路徑名表示的文件或目錄。

              boolean exists():測試此抽象路徑名表示的文件或目錄是否實際存在。

              String getAbsolutePath():返回此抽象路徑名的絕對路徑名字符串(帶盤符)。

              String getPath():將此抽象路徑名轉(zhuǎn)換成一個路徑名字符串。

              String getParent():返回此抽象路徑名的父目錄的路徑名字符串,如果沒有父目錄則返回null。

              boolean isDirectory():測試此抽象路徑名表示的文件是否是一個目錄,只有是目錄且實際存在才會返回true。

              boolean isFile():測試此抽象路徑名表示的文件是否是一個標準文件,只有是標準文件且實際存在才會返回true。

              String[] list():返回一個字符串?dāng)?shù)組,這些字符串指定此抽象路徑名所表示的目錄中的文件了目錄,即返回一個文件夾下所有的文件名和文件夾名。

              boolean mkdir():創(chuàng)建此抽象路徑名指定的目錄,即創(chuàng)建文件夾。如果此路徑中有不存在的父文件夾,則創(chuàng)建失敗,返回false。

                   boolean mkdirs():創(chuàng)建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。

              boolean createNewFile():當(dāng)且僅當(dāng)不存在此抽象路徑名指定的文件時,不可分地創(chuàng)建一個新的空文件。

       

      (6) DataInputStream: java.io.FilterInputStream的子類,數(shù)據(jù)輸入流,允許應(yīng)用程序以與機器無關(guān)的方式從底層輸入流中讀取基本Java數(shù)據(jù)類型。

        主要方法: DataInputStream DataInputStream(InputStream in):使用指定的底層InputStream創(chuàng)建一個DataInputStream。

             String readUTF():讀入一個使用UTF-8格式編碼的字符串,以String形式返回此字符串。

       

      (7) DataOutputStream: java.io.FilterOutputStream的子類,數(shù)據(jù)輸出流,允許應(yīng)用程序以適當(dāng)?shù)姆绞綄ava基本數(shù)據(jù)類型的數(shù)據(jù)寫入輸出流中。

        主要方法: DataOutputStream DataOutputStream(OutputStream out):使用指定的底層OutputStream創(chuàng)建一個新的數(shù)據(jù)輸出流。

              void writeUTF(String str):以與機器無關(guān)的方式使用UTF-8編碼將一個字符串寫入基礎(chǔ)輸出流。

       

      (8) InputStreamReader: java.io.Reader的子類,是字節(jié)流(文件)通向字符流的橋梁,可以使用指定的字符集讀取字節(jié),并將其解碼為字符。

        主要方法: InputStreamReader InputStreamReader(InputStream in,CharSet c s):由輸入流對象創(chuàng)建使用指定字符集的InputStreamReader。

              int read():讀取單個字符。

       

      (9) OutputStreamWriter: java.io.Writer的子類,是字符流通向字節(jié)流(文件)的的橋梁,可使用指定的字符集將要寫入流中的字符編碼成字節(jié)。

        主要方法: OutputStreamWriter OutputStreamWriter(OutputStream out,Charset cs):由輸出流對象創(chuàng)建使用指定符集的OutputStreamWriter。

              void write(int c):寫入單個字符。

              void write(String str,int off,int len):寫入字符串的一部分到文件中。

       

      (10) BufferedReader: java.io.Reader的子類,從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取。

         主要方法: BufferedReader BufferedReader(Reader in):創(chuàng)建一個使用默認大小輸入緩沖區(qū)的緩沖字符輸入流。

               int read():讀取單個字符。

              String readLine():讀取一行文本。

       

      (11) BufferedWriter: java.io.Writer的子類,將文本寫入字符輸出流,緩沖各個字符,提供單個字符、數(shù)組和字符串的高效寫入。

         主要方法: BufferedWriter BufferedWriter(Writer out):創(chuàng)建一個使用默認大小輸出緩沖區(qū)的緩沖字符輸出流。

              void write(int c):寫入單個字符。

              void write(String s,int off,int len):寫入字符串的某一部分到文件中。

              void newLine():寫入一個行分隔符。

       

      (12) FileReader: java.io.InputStreamReader的子類,是用來讀取字符文件的便捷類,用于讀取字符流。此類的構(gòu)造方法默認的字符編碼和默認字節(jié)緩沖區(qū)大小都是適當(dāng)?shù)摹?/p>

          主要方法: FileReader FileReader(File file):為給定的File對象創(chuàng)建一個新的FileReader。

       

      (13) FileWriter: java.io.OutputStreamWriter的子類,是用來寫入字符文件的便捷類,用于寫入字節(jié)流。如果要寫入原始字節(jié)流,則考慮使用FileOutputStream。

             主要方法: FileWriter FileWriter(File file):為給定的FIle對象創(chuàng)建一個新的FileWriter。

       

      (14) PrintWriter: java.io.Writer的子類,向文本輸出流打印對象的格式化表示形式,不包含用于寫入原始字節(jié)的方法。

         主要方法: PrintWriter PrintWriter(File file,String csn): 使用指定文件和字符集創(chuàng)建一個不具有自動行刷新的新PrintWriter對象。

                          PrintWriter PrintWriter(OutputStream out,boolean autoFlush):根據(jù)現(xiàn)有的OutputStream創(chuàng)建新的PrintWriter對象。

                          PrintWriter PrintWriter(Writer out,boolean autoFlush):根據(jù)Writer創(chuàng)建新的PrintWriter。

                          PrintWriter append(CharSequence):將指定的字符序列添加到此writer。

             void println(String x):打印字符串到文件中,然后終止該行。

               void write(String s):寫入字符串道文件中。

       

      (15) PrintStream: java.io.FilterOutputStream的子類,為其他的輸出流添加了功能,使他們能夠方便地打印各種數(shù)據(jù)值表示形式。與其他輸出流不同,PrintStream永遠不會拋出IOException。 

         主要方法:PrintStream PrintStream(File file,String csn):創(chuàng)建具有指定名稱和字符集且不帶自動行刷新的打印流。

                    PrintStream PrintStream(OutputStream out,boolean autoFlush):根據(jù)已有的OutputStream對象創(chuàng)建新的打印流。

               PrintStream append(CharSequence csq):將指定字符序列添加到此輸出流。

               void println(String x):打印一行字符串。

               void write(byte[] buf,int off,int len):將len字節(jié)從知道那個的初始偏移量為off的byte數(shù)組中寫入此流?!  ?nbsp; 

       

      2. 讀取文件byte內(nèi)容并輸出到控制臺

       

        讀取文件byte內(nèi)容的處理流程:由文件的File對象生成FileInputStream對象 --> FileInputStream對象調(diào)用read函數(shù)將文件內(nèi)容讀入byte數(shù)組 --> 輸出byte數(shù)組中的內(nèi)容到控制臺。其實現(xiàn)源代碼如下所示,

      復(fù)制代碼
       1 import java.io.FileInputStream;
       2 import java.io.IOException;
       3 import java.util.Arrays;
       4 
       5 import org.dom4j.DocumentException;
       6 
       7 public class FileIO 
       8 {
       9     void readFile(String filepath)throws IOException
      10     {
      11         File f=new File(filepath);
      12         if(!f.exists())
      13         {
      14             System.out.println("文件"+f.getAbsolutePath()+"不存在!");
      15             return;
      16         }
      17         else
      18         {
      19             FileInputStream fis=new FileInputStream(f);//創(chuàng)建對應(yīng)f的文件輸入流
      20             byte[] b=new byte[(int)f.length()];//創(chuàng)建一個長度等于文件f長度的byte數(shù)組,用于存放從文件中讀出的數(shù)據(jù)
      21             fis.read(b);
      22             String str=Arrays.toString(b);
      23             System.out.println(str);
      24             fis.close();   
      25         }
      26     }
      27     
      28     public static void main(String args[])throws IOException, DocumentException
      29     {
      30         FileIO fio=new FileIO();
      31         fio.readFile("WebRoot\\accessed_files\\test.txt");  
      33     }
      34 }
      復(fù)制代碼

         其中test.txt中只有一行內(nèi)容:“你好,我是chloe”,運行程序輸出的byte數(shù)組內(nèi)容為

      [-60, -29, -70, -61, -93, -84, -50, -46, -54, -57, 99, 104, 108, 111, 101]

      3. 向文件中寫入byte數(shù)據(jù)

        向文件中輸入byte數(shù)據(jù)的處理過程:由文件的File對象生成FileOutputStream對象 --> FileOutputStream對象調(diào)用write函數(shù)將byte數(shù)組中的內(nèi)容寫入文件。其實現(xiàn)源代碼如下

      復(fù)制代碼
       1 package chloe.fileio;
       2 import java.io.File;
       3 import java.io.FileInputStream;
       4 import java.io.FileOutputStream;
       5 import java.io.IOException;
       6 import java.util.Arrays;
       7 
       8 import org.dom4j.DocumentException;
       9 
      10 public class FileIO 
      11 {    
      12     void writeToFile(String filepath,byte b[])throws IOException
      13     {
      14         File f=new File(filepath);
      15         if(!f.exists())
      16         {
      17             f.createNewFile();
      18             System.out.println("文件"+f.getPath()+"已創(chuàng)建");
      19         }
      20         FileOutputStream fos=new FileOutputStream(f);
      21         fos.write(b);
      22         System.out.println("文件內(nèi)容寫入完畢");
      23     }
      24     
      25     public static void main(String args[])throws IOException, DocumentException
      26     {
      27         FileIO fio=new FileIO();
      28         byte[] b=new byte[15];
      29         b[0]=(byte)-60;
      30         b[1]=(byte)-29;
      31         b[2]=(byte)-70;
      32         b[3]=(byte)-61;
      33         b[4]=(byte)-93;
      34         b[5]=(byte)-84;
      35         b[6]=(byte)-50;
      36         b[7]=(byte)-46;
      37         b[8]=(byte)-54;
      38         b[9]=(byte)-57;
      39         b[10]=(byte)99;
      40         b[11]=(byte)104;
      41         b[12]=(byte)108;
      42         b[13]=(byte)111;
      43         b[14]=(byte)101;
      44         fio.writeToFile("WebRoot\\accessed_files\\test1.txt", b);
      45                 
      46     }
      47     
      48 }
      復(fù)制代碼

        運行后打開生成的文件test1.txt,其中的內(nèi)容為“你好,我是chloe”。

        以上是從文件中讀取byte數(shù)組數(shù)據(jù)和向?qū)懭隻yte數(shù)組,如果需要直接從文件中讀取字符串或者向文件中寫入字符串,則要使用類型DataInputStream和DataOutputStream.

      4. 使用DataOutputStream和DataInputStream寫入和讀取字符串

        寫入處理流程:由文件的File對象生成FileOutputStream對象 --> 由FileOutputStream對象生成DataOutputStream對象 --> DataOutputStream對象調(diào)用writeUTF函數(shù)將字符串寫入文件

        讀取處理流程:由文件的File對象生成FileInputStream對象 --> 由FileInputStream對象生成DataInputStream對象 --> DataInputStream對象調(diào)用readUTF函數(shù)將文件內(nèi)容讀入字符串 --> 輸出字符串內(nèi)容到控制臺

            其實現(xiàn)代碼如下所示,

      復(fù)制代碼
       1     void writeStrToFile(String filepath,String str)throws IOException
       2     {
       3         File f=new File(filepath);
       4         if(!f.exists())
       5         {
       6             f.createNewFile();
       7             System.out.println("文件"+f.getPath()+"已創(chuàng)建");
       8         }
       9         FileOutputStream fos=new FileOutputStream(f);
      10         DataOutputStream dos=new DataOutputStream(fos);
      11         dos.writeUTF(str);
      12         System.out.println("文件內(nèi)容寫入完畢");
      13         dos.close();
      14         fos.close();
      15     }
      16     
      17     String readStrFromFile(String filepath)throws IOException
      18     {
      19         String result;
      20         File f=new File(filepath);
      21         if(!f.exists())
      22         {
      23             System.out.println("文件"+f.getAbsolutePath()+"不存在!");
      24             return "";
      25         }
      26         else
      27         {
      28             FileInputStream fis=new FileInputStream(f);//創(chuàng)建對應(yīng)f的文件輸入流
      29             DataInputStream dis=new DataInputStream(fis);
      30             result=dis.readUTF();//只有先用DataOutputStream的write方法寫入合規(guī)則的數(shù)據(jù)后才能正常讀出內(nèi)容
      31             dis.close();
      32             fis.close();
      33             if (result!=null)
      34                 return result;
      35             else 
      36                 return "";
      37             
      38         }
      39     }
      40     
      41     public static void main(String args[])throws IOException, DocumentException
      42     {
      43         FileIO fio=new FileIO();
      44         fio.writeStrToFile("WebRoot\\accessed_files\\test.txt", "你好,我是Chloe~");
      45         System.out.println("讀取的文件內(nèi)容為:"+fio.readStrFromFile("WebRoot\\accessed_files\\test.txt"));
      46         
      47         
      48     }
      49     
      50 }
      復(fù)制代碼

        運行后輸出如下結(jié)果,

      1 文件內(nèi)容寫入完畢
      2 讀取的文件內(nèi)容為:你好,我是Chloe~

        注意,使用DataInputStream讀取文件內(nèi)容時,只有讀取由DataOutputStream寫入的數(shù)據(jù)才會正常,否則會報EOFException,所以我感覺這兩個類不是很好用。

      5. 使用BufferedReader和BufferedWriter寫入和讀取字符串

         讀取字符串時,只要在創(chuàng)建InputStreamReader時指定正確的編碼方式,即其編碼方式與文件本身的編碼方式一致就可以正確讀取文件內(nèi)容,代碼如下,

      復(fù)制代碼
       1     String readLinesFromFile(String filepath) throws IOException
       2     {
       3         String result="";
       4         String line;
       5         File f=new File(filepath);
       6         if(!f.exists())
       7         {
       8             System.out.println("文件"+f.getAbsolutePath()+"不存在!");
       9             return "";
      10         }
      11         else
      12         {
      13             FileInputStream fis=new FileInputStream(f);//創(chuàng)建對應(yīng)f的文件輸入流
      14             InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
      15             BufferedReader br=new BufferedReader(isr);
      16             while((line=br.readLine())!=null)
      17             {
      18                 result=result+line+"\n";
      19             }
      20             return result;    
      21         }
      22     }
      23     public static void main(String args[])throws IOException, DocumentException
      24     {
      25         FileIO fio=new FileIO();
      26         System.out.print(fio.readLinesFromFile("WebRoot\\accessed_files\\test.txt"));
      27     }
      復(fù)制代碼

        另外,還可以利用更加便捷的寫入字符文件的類FileReader實現(xiàn)上述功能,只用將上面的第13~15行改為:

      FileReader fr=new FileReader(f);
      BufferedReader br=new BufferedReader(fr);

        向文件中寫入字符串時,創(chuàng)建OutputStreamWriter也盡量要指明編碼方式,方便之后正確讀取。

      復(fù)制代碼
       1 void writeLinesToFile(String filepath,String[]strs)throws IOException
       2     {
       3         File f=new File(filepath);
       4         if(!f.exists())
       5         {
       6             f.createNewFile();
       7             System.out.println("文件"+f.getPath()+"已創(chuàng)建");
       8         }
       9         FileOutputStream fos=new FileOutputStream(f);
      10         OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
      11         BufferedWriter bw=new BufferedWriter(osw);
      12         for(int i=0;i<strs.length;i++)
      13         {
      14             bw.write(strs[i]);
      15             bw.newLine();
      16         }
      17         System.out.println("文件內(nèi)容寫入完畢");
      18         bw.close();
      19         osw.close();
      20         fos.close();
      21     }
      22     public static void main(String args[])throws IOException, DocumentException
      23     {
      24         FileIO fio=new FileIO();
      25         String[] s={"第一行","第二行","   最后一行"};
      26         fio.writeLinesToFile("WebRoot\\accessed_files\\test.txt", s);
      27     }v

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多