⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 89.html

📁 关于jsp的一些好文章 主要介绍一些关于JSP的应用技巧方面的东西
💻 HTML
字号:

<STYLE type=text/css>
<!--
body,td { font-size:9pt;}
hr { color: #000000; height: 1px}
-->
</STYLE>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD><TITLE>精选文章 >> 文件操作 >> Java 中对文件的读写操作之比较</title>
</head>
<body >

<p><IMG SRC="../image/jsp001_middle_logo.gif" WIDTH="180" HEIGHT="60" BORDER=0 ALT=""></p>

<table width=100% bgcolor="#cccccc" align=center cellpadding="2" cellspacing="0" border=1 bordercolorlight="#000000" bordercolordark="#FFFFFF">
<tr bgcolor="#EFF8FF"><td>
<a href=http://www.jsp001.com/list_thread.php?int_attribute=2>精选文章</a>
>> <a href=http://www.jsp001.com/list_thread.php?forumid=16&int_attribute=2>文件操作</a>
>> Java 中对文件的读写操作之比较 [<a href=http://www.jsp001.com/forum/showthread.php?goto=newpost&threadid=89>查看别人的评论</a>]<br>

<hr><p>由 webmaster 发布于: 2001-01-21 08:55</p><p> </p><p>作者:Jeru Liu<br>日期:November 29,2000<br>版本:1.0<br><br>Java 对文件进行读写操作的例子很多,让初学者感到十分困惑,我觉得有必要将各种方法进行<br>一次分析,归类,理清不同方法之间的异同点。<br><br>一.在 JDK 1.0 中,通常是用 InputStream &amp; OutputStream 这两个基类来进行读写操作的。<br>InputStream 中的 FileInputStream 类似一个文件句柄,通过它来对文件进行操作,类似的,在 <br>OutputStream 中我们有 FileOutputStream 这个对象。<br><br>用FileInputStream 来读取数据的常用方法是:<br>FileInputStream fstream = new FileInputStream(args[0]);<br>DataInputStream in = new DataInputStream(fstream);<br>用 in.readLine() 来得到数据,然后用 in.close() 关闭输入流。<br>完整代码见 Example 1。<br><br>用FileOutputStream 来写入数据的常用方法是:<br>FileOutputStream out out = new FileOutputStream("myfile.txt");  <br>PrintStream p = new PrintStream( out );<br>用 p.println() 来写入数据,然后用 p.close() 关闭输入。<br>完整代码见 Example 2。<br><br><br>二.在 JDK 1.1中,支持两个新的对象 Reader &amp; Writer, 它们只能用来对文本文件进行操作,而 <br>JDK1.1中的 InputStream &amp; OutputStream 可以对文本文件或二进制文件进行操作。<br><br>用FileReader 来读取文件的常用方法是:<br>FileReader fr = new FileReader("mydata.txt");<br>BufferedReader br = new BufferedReader(fr); <br>用 br.readLing() 来读出数据,然后用br.close() 关闭缓存,用fr.close() 关闭文件。<br>完整代码见 Example 3。 <br><br>用 FileWriter 来写入文件的常用方法是:<br>FileWriter fw = new FileWriter("mydata.txt");<br>PrintWriter out = new PrintWriter(fw); <br>在用out.print 或 out.println 来往文件中写入数据,out.print 和 out.println的唯一区别是后者写<br>入数据或会自动开一新行。写完后要记得 用out.close() 关闭输出,用fw.close() 关闭文件。  <br>完整代码见 Example 4。<br><br>-------------------------------------------------------------- following is the source code of examples------------------------------------------------------<br><br>Example 1:<br>// FileInputDemo<br>// Demonstrates FileInputStream and DataInputStream<br>import java.io.*;<br><br>class FileInputDemo {<br> public static void main(String args[]) {<br>  // args.length is equivalent to argc in C<br>  if (args.length == 1) {<br>   try {<br>    // Open the file that is the first command line parameter<br>    FileInputStream fstream = new FileInputStream(args[0]);<br>    // Convert our input stream to a DataInputStream<br>    DataInputStream in = new DataInputStream(fstream);<br>    // Continue to read lines while there are still some left to read<br>    while (in.available() !=0) {<br>     // Print file line to screen<br>     System.out.println (in.readLine());<br>    }<br>    in.close();<br>   } catch (Exception e) {<br>    System.err.println("File input error");<br>   }<br>  }<br>  else<br>   System.out.println("Invalid parameters");<br> }<br>}<br><br>Example 2:<br>// FileOutputDemo<br>// Demonstration of FileOutputStream and PrintStream classes<br>import java.io.*;<br><br>class FileOutputDemo <br>{  <br> public static void main(String args[]) {       <br> FileOutputStream out; // declare a file output object<br>  PrintStream p; // declare a print stream object<br><br>try {<br> // connected to "myfile.txt"<br>   out = new FileOutputStream("myfile.txt");<br>   // Connect print stream to the output stream<br>   p = new PrintStream( out );<br>   p.println ("This is written to a file");<br>   p.close();<br>  } catch (Exception e) {<br>   System.err.println ("Error writing to file");<br>  }<br> }<br>}<br><br>Example 3:<br>// FileReadTest.java<br>// User FileReader in JDK1.1 to read a file <br>import java.io.*;<br><br>class FileReadTest {   <br> public static void main (String[] args) {<br>  FileReadTest t = new FileReadTest();<br>  t.readMyFile();<br>} <br>  <br> void readMyFile() { <br>  String record = null;<br>  int recCount = 0; <br>  try { <br>FileReader fr = new FileReader("mydata.txt");<br>    BufferedReader br = new BufferedReader(fr);<br>    record = new String();<br>    while ((record = br.readLine()) != null) {<br>     recCount++;<br>     System.out.println(recCount + ": " + record); <br>}<br>br.close();<br>fr.close(); <br>   } catch (IOException e) { <br>     System.out.println("Uh oh, got an IOException error!");<br>     e.printStackTrace();<br>   }<br>} <br> <br>}  <br><br>Example 4:<br>// FileWriteTest.java<br>// User FileWriter in JDK1.1 to writer a file <br>import java.io.*;<br><br>class FileWriteTest {   <br> public static void main (String[] args) {<br>  FileWriteTest t = new FileWriteTest();<br>  t.WriteMyFile();<br>} <br>  <br> void WriteMyFile() { <br>  try { <br>FileWriter fw = new FileWriter("mydata.txt");<br>PrintWriter out = new PrintWriter(fw);  <br>out.print(“hi,this will be wirte into the file!”);  <br>out.close();<br>fw.close();<br>   } catch (IOException e) { <br>     System.out.println("Uh oh, got an IOException error!");<br>     e.printStackTrace();<br>   }<br>} <br> <br>}  <br><br></p></td>
  </tr>
</table>

<p>
<CENTER><a href="http://www.jsp001.com/forum/newreply.php?action=newreply&threadid=89">点这里对该文章发表评论</a></CENTER>
<p>该文章总得分是 <font color=red>0</font> 分,你认为它对你有帮助吗?
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=89&intVote=4","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>非常多</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=89&intVote=2","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>有一些</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=89&intVote=1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>无帮助</a>](<font color=red>0</font>) 
				[<a href=javascript:void(0) onclick=window.open("http://www.jsp001.com/forum/codeVote.php?threadid=89&intVote=-1","","menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no,width=70,height=40,top=0,left=0")>是灌水</a>](<font color=red>0</font>) </p>
<script language="javascript" src="http://www.jsp001.com/include/read_thread_script.php?threadid=89"></script>
<p><CENTER>
Copyright &copy; 2001 - 2009 JSP001.com . All Rights Reserved <P>

<IMG SRC="../image/jsp001_small_logo.gif" WIDTH="85" HEIGHT="30" BORDER=0 ALT="">
</CENTER></p>

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -