科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道入门:Java 中对文件的读写操作之比较

入门:Java 中对文件的读写操作之比较

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

在 JDK 1.1中,支持两个新的对象 Reader &Writer, 它们只能用来对文本文件进行操作,而JDK1.1中的 InputStream &OutputStream 可以对文本文件或二进制文件进行操作。

作者:执木 来源:IT专家网 2008年4月25日

关键字: 操作 读写 文件 java

  • 评论
  • 分享微博
  • 分享邮件
Java 对文件进行读写操作的例子很多,让初学者感到十分困惑,我觉得有必要将各种方法进行一次分析,归类,理清不同方法之间的异同点。

  一.在 JDK 1.0 中,通常是用 InputStream & OutputStream 这两个基类来进行读写操作的。InputStream 中的 FileInputStream 类似一个文件句柄,通过它来对文件进行操作,类似的,在OutputStream 中我们有 FileOutputStream 这个对象。

  用FileInputStream 来读取数据的常用方法是:FileInputStream fstream = new FileInputStream(args[0]);DataInputStream in = new DataInputStream(fstream);

  用 in.readLine() 来得到数据,然后用 in.close() 关闭输入流。完整代码见 Example 1。

  用FileOutputStream 来写入数据的常用方法是:FileOutputStream out out = new FileOutputStream(myfile.txt);PrintStream p = new PrintStream( out );

  用 p.println() 来写入数据,然后用 p.close() 关闭输入。完整代码见 Example 2。

  二.在 JDK 1.1中,支持两个新的对象 Reader & Writer, 它们只能用来对文本文件进行操作,而JDK1.1中的 InputStream & OutputStream 可以对文本文件或二进制文件进行操作。

  用FileReader 来读取文件的常用方法是:FileReader fr = new FileReader(mydata.txt);BufferedReader br = new BufferedReader(fr);

  用 br.readLing() 来读出数据,然后用br.close() 关闭缓存,用fr.close() 关闭文件。完整代码见 Example 3。

  用 FileWriter 来写入文件的常用方法是:FileWriter fw = new FileWriter(mydata.txt);PrintWriter out = new PrintWriter(fw);

  在用out.print 或 out.println 来往文件中写入数据,out.print 和 out.println的唯一区别是后者写入数据或会自动开一新行。写完后要记得 用out.close() 关闭输出,用fw.close() 关闭文件。

完整代码见 Example 4  

Example 1:// FileInputDemo
  // Demonstrates FileInputStream and DataInputStream
  import java.io.*;
  class FileInputDemo {
  public static void main(String args[]) {
  // args.length is equivalent to argc in C
  if (args.length == 1) {
  try {
  // Open the file that is the first command line parameter
  FileInputStream fstream = new FileInputStream(args[0]);
  // 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 (in.available() !=0) {
  // Print file line to screen
  System.out.println (in.readLine());
  }
  in.close();
  } catch (Exception e) {
  System.err.println(File input error);
  }
  }
  else
  System.out.println(Invalid parameters);
  }
  }
  Example 2:
  // FileOutputDemo
  // Demonstration of FileOutputStream and PrintStream classes
  import java.io.*;
  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 myfile.txt
  out = new FileOutputStream(myfile.txt);
  // Connect print stream to the output stream
  p = new PrintStream( out );
  p.println (This is written to a file);
  p.close();
  } catch (Exception e) {
  System.err.println (Error writing to file);
  }
  }
  }
  Example 3:
  // FileReadTest.java
  // User FileReader in JDK1.1 to read a file
  import java.io.*;
  class FileReadTest {
  public static void main (String[] args) {
  FileReadTest t = new FileReadTest();
  t.readMyFile();
  }
  void readMyFile() {
  String record = null;
  int recCount = 0;
  try {
  FileReader fr = new FileReader(mydata.txt);
  BufferedReader br = new BufferedReader(fr);
  record = new String();
  while ((record = br.readLine()) != null) {
  recCount++;
  System.out.println(recCount + : + record);
  }
  br.close();
  fr.close();
  } catch (IOException e) {
  System.out.println(Uh oh, got an IOException error!);
  e.printStackTrace();
  }
  }
  }

  Example 4:

// FileWriteTest.java
  // User FileWriter in JDK1.1 to writer a file
  import java.io.*;
  class FileWriteTest {
  public static void main (String[] args) {
  FileWriteTest t = new FileWriteTest();
  t.WriteMyFile();
  }
  void WriteMyFile() {
  try {
  FileWriter fw = new FileWriter(mydata.txt);
  PrintWriter out = new PrintWriter(fw);
  out.print(“hi,this will be wirte into the file!”);
  out.close();
  fw.close();
  } catch (IOException e) {
  System.out.println(Uh oh, got an IOException error!);
  e.printStackTrace();
  }
  }
  }

    • 评论
    • 分享微博
    • 分享邮件
    闂傚倸鍊搁崐鎼佸磹妞嬪孩顐介柨鐔哄Т閻骞栧ǎ顒€濡肩紒鎰殜閺岋繝宕堕埡浣锋睏闂佸搫顑呴柊锝夊蓟閺囷紕鐤€閻庯綆浜炴禒鐐節濞堝灝鐏犻柕鍫熸倐瀵寮撮敍鍕澑闁诲函缍嗘禍鏍磻閹捐鍐€妞ゆ挶鍔庣粙蹇涙⒑鐠恒劌娅愰柟鍑ゆ嫹

    婵犵數濮烽弫鍛婃叏閻戝鈧倹绂掔€n亞鍔﹀銈嗗坊閸嬫捇鏌涢悢閿嬪仴闁糕斁鍋撳銈嗗坊閸嬫挾绱撳鍜冭含妤犵偛鍟灒閻犲洩灏欑粣鐐烘⒑瑜版帒浜伴柛鎾寸洴閹儳煤椤忓應鎷洪梻鍌氱墛閸楁洟宕奸妷銉ф煣濠电姴锕ょ€氼參宕h箛鏃傜瘈濠电姴鍊绘晶娑㈡煕鐎c劌濡介柕鍥у瀵粙濡歌閳ь剚甯¢弻鐔兼寠婢跺﹥娈婚梺鍝勭灱閸犳牠骞冨⿰鍫濈厸闁稿本绋撹ぐ瀣煟鎼淬値娼愭繛鍙壝悾婵堢矙鐠恒劍娈鹃梺鍓插亝濞叉牠鎮″☉銏$厱閻忕偛澧介惌瀣箾閸喐鍊愭慨濠勭帛閹峰懐绮电€n亝鐣伴梻浣规偠閸斿宕¢崘鑼殾闁靛繈鍊曢崘鈧銈嗗姂閸庡崬鐨梻鍌欑劍鐎笛呯矙閹寸姭鍋撳鐓庡籍鐎规洑鍗冲畷鍗炍熼梹鎰泿闂備線娼ч悧鍡涘箠鎼淬垺鍙忔い鎺嗗亾闁宠鍨块崺銉╁幢濡炲墽鍑规繝鐢靛О閸ㄦ椽鏁嬮柧鑽ゅ仦娣囧﹪濡堕崨顔兼闂佺ǹ顑呴崐鍦崲濞戙垹骞㈡俊顖濐嚙绾板秹鏌f惔銏e妞わ妇鏁诲璇差吋閸偅顎囬梻浣告啞閹搁箖宕版惔顭戞晪闁挎繂顦介弫鍡椼€掑顒婂姛闁活厽顨嗙换娑㈠箻閺夋垹鍔伴梺绋款儐閹瑰洭寮婚敐鍛婵炲棙鍔曠壕鎶芥⒑閸濆嫭婀扮紒瀣灴閸╃偤骞嬮敃鈧婵囥亜閺囩偞鍣洪柍璇诧功缁辨捇宕掑▎鎴濆濡炪們鍔岄幊姗€骞嗗畝鍕<闁绘劙娼х粊锕傛煙閸忚偐鏆橀柛鏂跨焸閹偤宕归鐘辩盎闂佸湱鍎ら崹鐢割敂閳哄懏鍊垫慨姗嗗墻濡插綊鏌曢崶褍顏€殿喕绮欐俊姝岊槼闁革絻鍎崇槐鎾存媴缁涘娈┑鈽嗗亝缁诲牆顕f繝姘亜缁炬媽椴搁弲锝夋偡濠婂啰效闁诡喗锕㈤幊鐘活敆閸屾粣绱查梺鍝勵槸閻楀嫰宕濇惔锝囦笉闁绘劗鍎ら悡娑㈡倶閻愯泛袚闁哥姵锕㈤弻鈩冩媴閻熸澘顫掗悗瑙勬礈閸犳牠銆佸鈧幃鈺呮惞椤愩倝鎷婚梻鍌氬€峰ù鍥х暦閸偅鍙忛柟鎯板Г閳锋梻鈧箍鍎遍ˇ顖炲垂閸岀偞鐓㈡俊顖滃皑缁辨岸鏌ㄥ┑鍡╂Ц缂佲偓鐎n偁浜滈柡宥冨妿閳藉绻涢崼鐔虹煉婵﹨娅e☉鐢稿川椤斾勘鈧劕顪冮妶搴′簼婵炶尙鍠栧畷娲焵椤掍降浜滈柟鍝勬娴滈箖姊洪幐搴㈢┛濠碘€虫搐鍗遍柟鐗堟緲缁秹鏌涢锝囩畼妞ゆ挻妞藉铏圭磼濡搫顫岄悗娈垮櫘閸撴瑨鐏冮梺鍛婁緱閸犳岸宕㈤幖浣光拺闁告挻褰冩禍浠嬫煕鐎n亜顏柟顔斤耿閺佸啴宕掑☉姘箞闂佽鍑界紞鍡涘磻閸℃ɑ娅犳い鎺戝€荤壕濂告煕鐏炲墽鈽夌紒妞﹀洦鐓欓柣鐔告緲椤忣參鏌熼悡搴㈣础闁瑰弶鎸冲畷鐔兼濞戞瑦鐝¢梻鍌氬€搁崐椋庣矆娓氣偓楠炴牠顢曢妶鍌氫壕婵ê宕崢瀵糕偓瑙勬礀缂嶅﹪寮婚崱妤婂悑闁告侗鍨界槐閬嶆煟鎼达紕鐣柛搴ㄤ憾钘濆ù鍏兼綑绾捐法鈧箍鍎遍ˇ浼存偂閺囥垺鐓涢柛銉e劚婵$厧顭胯閸ㄤ即婀侀梺缁樓圭粔顕€顢旈崼鐔虹暢闂傚倷鐒︾€笛呮崲閸屾娑樜旈崨顓犲幒闂佸搫娲㈤崹娲偂閸愵亝鍠愭繝濠傜墕缁€鍫熸叏濡寧纭鹃柦鍐枛閺屾洘绻涜鐎氱兘宕戦妸鈺傗拺缂備焦锚婵洦銇勯弴銊ュ籍闁糕斂鍨藉鎾閳ユ枼鍋撻悽鍛婄叆婵犻潧妫楅埀顒傛嚀閳诲秹宕堕妸锝勭盎闂婎偄娲︾粙鎰板箟妤e啯鐓涢悘鐐靛亾缁€瀣偓瑙勬礋娴滃爼銆佸鈧幃銏$附婢跺澶�

    重磅专题
    往期文章
    最新文章