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

📄 java07_02.htm

📁 JAVA的课件
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Java程序设计</title>
</head>

<body background="Bg.gif">

<p align="center"><font size="5"><b>§7.2 InputStream和OutputStream</b></font></p> 
 
<p align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InputStream和OutputStream定义了最基本的输入和输出功能。但它们都是抽象类,并不能完成实际的操作,程序员需要根据实际情况来选择子类生成对象。</p> 
<p align="left">1、InputStream类的基本方法</p> 
<table height="448" border=1> 
  <tr class="TableHeadingColor" bgColor="#ccccff"> 
    <td colSpan="2" height="26"><font size="+2"><b>Method Summary</b></font></td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="50"><font size="-1"><code>&nbsp;int</code></font></td> 
    <td height="50"><code><b>available</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the  
      number of bytes that can be read (or skipped over) from this input stream  
      without blocking by the next caller of a method for this input stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="50"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td height="50"><code><b>close</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Closes this  
      input stream and releases any system resources associated with the stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="37"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td height="37"><code><b>mark</b>(int&nbsp;readlimit)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Marks the  
      current position in this input stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="34"><font size="-1"><code>&nbsp;boolean</code></font></td> 
    <td height="34"><code><b>markSupported</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tests if this  
      input stream supports the <code>mark</code> and <code>reset</code>  
      methods.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="34"><font size="-1"><code><b>abstract</b>  
      &nbsp;int</code></font></td> 
    <td height="34"><code><b>read</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reads the next  
      byte of data from the input stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="53"><font size="-1"><code>&nbsp;int</code></font></td> 
    <td height="53"><code><b>read</b>(byte[]&nbsp;b)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reads some  
      number of bytes from the input stream and stores them into the buffer  
      array <code>b</code>.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="37"><font size="-1"><code>&nbsp;int</code></font></td> 
    <td height="37"><code><b>read</b>(byte[]&nbsp;b, int&nbsp;off, int&nbsp;len)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reads up to <code>len</code>  
      bytes of data from the input stream into an array of bytes.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="50"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td height="50"><code><b>reset</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Repositions  
      this stream to the position at the time the <code>mark</code> method was  
      last called on this input stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%" height="37"><font size="-1"><code>&nbsp;long</code></font></td> 
    <td height="37"><code><b>skip</b>(long&nbsp;n)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Skips over and  
      discards <code>n</code> bytes of data from this input stream.</td> 
  </tr> 
</table> 
<p align="left">2、OutputStream类的基本方法</p> 
<table border=1> 
  <tr class="TableHeadingColor" bgColor="#ccccff"> 
    <td colSpan="2"><font size="+2"><b>Method Summary</b></font></td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td><code><b>close</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Closes this  
      output stream and releases any system resources associated with this  
      stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td><code><b>flush</b>()</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Flushes this  
      output stream and forces any buffered output bytes to be written out.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td><code><b>write</b>(byte[]&nbsp;b)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writes <code>b.length</code>  
      bytes from the specified byte array to this output stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>&nbsp;void</code></font></td> 
    <td><code><b>write</b>(byte[]&nbsp;b, int&nbsp;off, int&nbsp;len)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writes <code>len</code>  
      bytes from the specified byte array starting at offset <code>off</code> to  
      this output stream.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td vAlign="top" align="right" width="1%"><font size="-1"><code>abstract  
      &nbsp;void</code></font></td> 
    <td><code><b>write</b>(int&nbsp;b)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Writes the  
      specified byte to this output stream.</td> 
  </tr> 
</table> 
<p align="left">3、使用文件输入输出流对文件进行读写</p> 
<p align="left">&nbsp;&nbsp;&nbsp; 我们这里利用FileInputStream和FileOutputStream来对文件进行读写,同时由于屏幕和键盘是文件的一种,所以也可以利用它们来进行读写。下面是这两个类的构造方法:</p>
<table border=1>
  <tr class="TableHeadingColor" bgColor="#ccccff">
    <td colSpan="2"><font size="+2"><b>FileInputStream Constructor Summary</b></font></td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileInputStream</b>(File&nbsp;file)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a <code>FileInputStream</code>  
      by opening a connection to an actual file, the file named by the <code>File</code>  
      object <code>file</code> in the file system.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileInputStream</b>(FileDescriptor&nbsp;fdObj)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a <code>FileInputStream</code>  
      by using the file descriptor <code>fdObj</code>, which represents an  
      existing connection to an actual file in the file system.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileInputStream</b>(String&nbsp;name)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a <code>FileInputStream</code>  
      by opening a connection to an actual file, the file named by the path name  
      <code>name</code> in the file system.</td> 
  </tr> 
</table> 
<table border=1> 
  <tr class="TableHeadingColor" bgColor="#ccccff"> 
    <td colSpan="2"><font size="+2"><b>FileOutputStream Constructor Summary</b></font></td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileOutputStream</b>(File&nbsp;file)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a file  
      output stream to write to the file represented by the specified <code>File</code>  
      object.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileOutputStream</b>(File&nbsp;file, boolean&nbsp;append)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a file  
      output stream to write to the file represented by the specified <code>File</code>  
      object.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileOutputStream</b>(FileDescriptor&nbsp;fdObj)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates an  
      output file stream to write to the specified file descriptor, which  
      represents an existing connection to an actual file in the file system.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileOutputStream</b>(String&nbsp;name)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates an  
      output file stream to write to the file with the specified name.</td> 
  </tr> 
  <tr class="TableRowColor" bgColor="white"> 
    <td><code><b>FileOutputStream</b>(String&nbsp;name, boolean&nbsp;append)</code><br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates an  
      output file stream to write to the file with the specified <code>name</code>.</td> 
  </tr> 
</table> 
<p align="left">例1、从键盘读入一行字符,写到文件output.txt中去</p>
<p align="left">import java.io.*;<br>  
class MyFileOutput{<br>  
&nbsp; public static void main(String argv[]){<br>  
&nbsp;&nbsp;&nbsp; FileInputStream fin;<br>  
&nbsp;&nbsp;&nbsp; FileOutputStream fout;<br>  
&nbsp;&nbsp;&nbsp; char ch;<br>  
&nbsp;&nbsp;&nbsp; try{<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin=new FileInputStream(FileDescriptor.in);<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout=new FileOutputStream("output.txt");<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("请输入一行字符:");<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((ch=(char)fin.read())!='\n')<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout.write(ch);<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin.close();<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout.close();&nbsp;<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("文件写入成功!");<br> 
&nbsp;&nbsp;&nbsp;&nbsp; }<br> 
&nbsp;&nbsp;&nbsp;&nbsp; catch(FileNotFoundException e){<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("不能创建文件");<br> 
&nbsp;&nbsp;&nbsp;&nbsp; }<br> 
&nbsp;&nbsp;&nbsp;&nbsp; catch(IOException e){<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("输入流有误!");<br> 
&nbsp;&nbsp;&nbsp;&nbsp; }<br> 
&nbsp; }<br> 
}</p> 
<p align="left">例2、读出指定文件中的内容,显示在屏幕上</p>
<p align="left">import java.io.*;<br> 
class TypeFile{<br> 
&nbsp; public static void main(String argv[]){<br> 
&nbsp;&nbsp;&nbsp; FileInputStream fin;<br> 
&nbsp;&nbsp;&nbsp; FileOutputStream fout;<br> 
&nbsp;&nbsp;&nbsp; int ch;&nbsp;&nbsp; //这里要用int型<br>
&nbsp;&nbsp;&nbsp; if (argv.length&lt;1)<br> 
&nbsp;&nbsp;&nbsp; { System.out.println("请指定文件名");<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin=new FileInputStream(argv[0]);<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout=new FileOutputStream(FileDescriptor.out);<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((ch=fin.read())!=-1)&nbsp; //用char型这里会出错<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout.write(ch);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // System.out.print((char)ch);&nbsp; 
用这个输出中文会有问题<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin.close();&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout.close();<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; catch(FileNotFoundException e){<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("文件没找到!");<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; catch(IOException e){<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("读入文件有误!");<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp; }<br>
}</p>
<p align="left">例3、复制文件</p>
<p align="left">import java.io.*;<br> 
class CopyFile{<br> 
&nbsp; public static void main(String argv[]){<br> 
&nbsp;&nbsp;&nbsp; FileInputStream fin;<br> 
&nbsp;&nbsp;&nbsp; FileOutputStream fout;<br> 
&nbsp;&nbsp;&nbsp; int ch;<br> 
&nbsp;&nbsp;&nbsp; if (argv.length!=2)<br> 
&nbsp;&nbsp;&nbsp; { System.out.println("参数格式不对,应该为:java CopyFile 源文件名 目标文件名");<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br>
&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin=new FileInputStream(argv[0]);<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout=new FileOutputStream(argv[1]);<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while((ch=fin.read())!=-1)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout.write(ch);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fin.close();&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fout.close();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("文件复制成功");<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; catch(FileNotFoundException e){<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("文件无法打开!");<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; catch(IOException e){<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("读写文件有误!");<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp; }<br>
}</p>
<p align="left"><a href="index.htm">回目录</a>    <a href="java07_01.htm">上一课</a> 
<a href="java07_03.htm">下一课</a></p>

</body>

</html>

⌨️ 快捷键说明

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