IO流文件的讀寫操作


  • 字符流有兩個抽象類:Writer   Reader。其對應子類FileWriter,FileReader可實現文件的讀寫操作
  • 同樣,字節流也有兩個抽象類:InputStream OutputStream。其對應子類有FileInputStream,FileOutputStream可實現文件讀寫
  • IO流中的重要方法:read()方法返回-1,readLine方法返回null。用法列如:while((line=br.readLine())!=null)。Scanne類中的hasNext()方法如果此掃描器的輸入中有另一個標記,則返回 true。hasNextLine()方法如果在此掃描器的輸入中存在另一行,則返回 true。next() 方法查找並返回來自此掃描器的下一個完整標記,nextLine() 此掃描器執行當前行,並返回跳過的輸入信息。
  • 在I/O流中可以創建文件對象,並且帶入文件名的類,常見的有以下7種:
  File file=new File("word.txt");
  FileInputStream fis=new FileInputStream("word.txt");
  FileOutputStream fos=new FileOutputStream("word.txt");
  FileReader (String filename);
  FileWriter (String filename);
  PrintStream (String filename);
  PrintWriter (String filename);
  • 文件File是java.io包中的一個重要的非流類。File以一種系統無關的方式表示一個文件對象的屬性。
  • File類用於創建目錄的方法是mkdir(),用於創建父目錄的方法是mkdirs().

InputStream,BufferedInputStream

read()
public abstract int read() throws IOException
從輸入流中讀取數據的下一個字節。返回 0 到 255 范圍內的 int 字節值。如果因為已經到達流末尾而沒有可用的字節,則返回值 -1。在輸入數據可用、檢測到流末尾或者拋出異常前,此方法一直阻塞。
返回:
下一個數據字節;如果到達流的末尾,則返回 -1。
close() 
關閉此輸入流並釋放與該流關聯的所有系統資源。
OutputStream,BufferedOutputStream
write()
public void write(int b) throws IOException
將指定的字節寫入此緩沖的輸出流。
參數:
b - 要寫入的字節。
close() 
關閉此輸出流並釋放與此流有關的所有系統資源。
BufferedReader
read()
public int read() throws IOException
讀取單個字符
返回:
作為一個整數(其范圍從 0 到 65535 (0x00-0xffff))讀入的字符,如果已到達流末尾,則返回 -1
readLine
public String readLine() throws IOException
讀取一個文本行。通過下列字符之一即可認為某行已終止:換行 ('\n')、回車 ('\r') 或回車后直接跟着換行。
返回:
包含該行內容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
close() 
關閉該流並釋放與之關聯的所有資源。
Reader,InputStreamReader
read()
public int read() throws IOException
將字符讀入數組。在某個輸入可用、發生 I/O 錯誤或者已到達流的末尾前,此方法一直阻塞
返回:
讀取的字符,如果已到達流的末尾,則返回 -1
close() 
關閉該流並釋放與之關聯的所有資源。
BufferedWriter
write(int c) 
寫入單個字符。
newLine()
public void newLine() throws IOException
寫入一個行分隔符。行分隔符字符串由系統屬性 line.separator 定義,並且不一定是單個新行 ('\n') 符。
返回:
包含該行內容的字符串,不包含任何行終止符,如果已到達流末尾,則返回 null
close() 
關閉此流,但要先刷新它。
Writer,OutputStreamWriter
write()
public void write(int c) throws IOException
寫入單個字符。要寫入的字符包含在給定整數值的 16 個低位中,16 高位被忽略。
用於支持高效單字符輸出的子類應重寫此方法。
參數:
c - 指定要寫入字符的 int。
close() 
關閉此流,但要先刷新它。
 
例題:復制文件:把文件a.txt從D盤復制到E盤,操作方法如下。
  • 方法一:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class Q2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\a.txt"));
String line = null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
}
bw.close();
}
}
  • 方法二
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
 
public class test4 {
public static void main(String[] args) throws IOException {
File file = new File("C://a.txt");
Scanner sc=new Scanner(file);
PrintWriter pw=new PrintWriter("D://a.txt");
while(sc.hasNextLine()){
String str=sc.nextLine();
pw.println(str);
}
pw.close();
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM