熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Java 中對文件的讀寫操作之比較

2013-11-15 11:41:41  來源: JSP教程 

  Java 對文件進行讀寫操作的例子很多讓初學者感到十分困惑我覺得有必要將各種方法進行
  一次分析歸類理清不同方法之間的異同點
  
  一.在 JDK 通常是用 InputStream & OutputStream 這兩個基類來進行讀寫操作的
  InputStream 中的 FileInputStream 類似一個文件句柄通過它來對文件進行操作類似的
  OutputStream 中我們有 FileOutputStream 這個對象
  
  用FileInputStream 來讀取數據的常用方法是
  FileInputStream fstream = new FileInputStream(args[]);
  DataInputStream in = new DataInputStream(fstream);
  用 inreadLine() 來得到數據然後用 inclose() 關閉輸入流
  完整代碼見 Example
  
  用FileOutputStream 來寫入數據的常用方法是
  FileOutputStream out out = new FileOutputStream(myfiletxt);
  PrintStream p = new PrintStream( out );
  用 pprintln() 來寫入數據然後用 pclose() 關閉輸入
  完整代碼見 Example
  
  
  二.在 JDK 支持兩個新的對象 Reader & Writer 它們只能用來對文本文件進行操作
  JDK中的 InputStream & OutputStream 可以對文本文件或二進制文件進行操作
  
  用FileReader 來讀取文件的常用方法是
  FileReader fr = new FileReader(mydatatxt);
  BufferedReader br = new BufferedReader(fr);
  用 brreadLing() 來讀出數據然後用brclose() 關閉緩存用frclose() 關閉文件
  完整代碼見 Example
  
  用 FileWriter 來寫入文件的常用方法是
  FileWriter fw = new FileWriter(mydatatxt);
  PrintWriter out = new PrintWriter(fw);
  在用outprint 或 outprintln 來往文件中寫入數據outprint 和 outprintln的唯一區別是後者寫
  入數據或會自動開一新行寫完後要記得 用outclose() 關閉輸出用fwclose() 關閉文件
  完整代碼見 Example
  
  
  
  
  Example :
  // FileInputDemo
  // Demonstrates FileInputStream and DataInputStream
  import javaio*;
  
  class FileInputDemo {
  public static void main(String args[]) {
  // argslength is equivalent to argc in C
  if (argslength == ) {
  try {
  // Open the file that is the first command line parameter
  FileInputStream fstream = new FileInputStream(args[]);
  // Convert our input stream to a DataInputStream
  DataInputStream in = new DataInputStream(fstream);
  // Continue to read lines while there are still some left to read
  while (inavailable() !=) {
  // Print file line to screen
  Systemoutprintln (inreadLine());
  }
  inclose();
  } catch (Exception e) {
  Systemerrprintln(File input error);
  }
  }
  else
  Systemoutprintln(Invalid parameters);
  }
  }
  
  Example :
  // FileOutputDemo
  // Demonstration of FileOutputStream and PrintStream classes
  import javaio*;
  
  class FileOutputDemo
  {
  public static void main(String args[]) {
  FileOutputStream out; // declare a file output object
  PrintStream p; // declare a print stream object
  
  try {
  // connected to myfiletxt
  out = new FileOutputStream(myfiletxt);
  // Connect print stream to the output stream
  p = new PrintStream( out );
  pprintln (This is written to a file);
  pclose();
  } catch (Exception e) {
  Systemerrprintln (Error writing to file);
  }
  }
  }
  
  Example :
  // FileReadTestjava
  // User FileReader in JDK to read a file
  import javaio*;
  
  class FileReadTest {
  public static void main (String[] args) {
  FileReadTest t = new FileReadTest();
  treadMyFile();
  }
  
  void readMyFile() {
  String record = null;
  int recCount = ;
  try {
  FileReader fr = new FileReader(mydatatxt);
  BufferedReader br = new BufferedReader(fr);
  record = new String();
  while ((record = brreadLine()) != null) {
  recCount++;
  Systemoutprintln(recCount + : + record);
  }
  brclose();
  frclose();
  } catch (IOException e) {
  Systemoutprintln(Uh oh got an IOException error!);
  eprintStackTrace();
  }
  }
  
  }
  
  Example :
  // FileWriteTestjava
  // User FileWriter in JDK to writer a file
  import javaio*;
  
  class FileWriteTest {
  public static void main (String[] args) {
  FileWriteTest t = new FileWriteTest();
  tWriteMyFile();
  }
  
  void WriteMyFile() {
  try {
  FileWriter fw = new FileWriter(mydatatxt);
  PrintWriter out = new PrintWriter(fw);
  outprint(hithis will be wirte into the file!);
  outclose();
  fwclose();
  } catch (IOException e) {
  Systemoutprintln(Uh oh got an IOException error!);
  eprintStackTrace();
  }
  }
  
  }
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19453.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.