📄 java07_02.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"> 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> int</code></font></td>
<td height="50"><code><b>available</b>()</code><br>
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> void</code></font></td>
<td height="50"><code><b>close</b>()</code><br>
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> void</code></font></td>
<td height="37"><code><b>mark</b>(int readlimit)</code><br>
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> boolean</code></font></td>
<td height="34"><code><b>markSupported</b>()</code><br>
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>
int</code></font></td>
<td height="34"><code><b>read</b>()</code><br>
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> int</code></font></td>
<td height="53"><code><b>read</b>(byte[] b)</code><br>
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> int</code></font></td>
<td height="37"><code><b>read</b>(byte[] b, int off, int len)</code><br>
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> void</code></font></td>
<td height="50"><code><b>reset</b>()</code><br>
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> long</code></font></td>
<td height="37"><code><b>skip</b>(long n)</code><br>
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> void</code></font></td>
<td><code><b>close</b>()</code><br>
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> void</code></font></td>
<td><code><b>flush</b>()</code><br>
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> void</code></font></td>
<td><code><b>write</b>(byte[] b)</code><br>
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> void</code></font></td>
<td><code><b>write</b>(byte[] b, int off, int len)</code><br>
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
void</code></font></td>
<td><code><b>write</b>(int b)</code><br>
Writes the
specified byte to this output stream.</td>
</tr>
</table>
<p align="left">3、使用文件输入输出流对文件进行读写</p>
<p align="left"> 我们这里利用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 file)</code><br>
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 fdObj)</code><br>
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 name)</code><br>
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 file)</code><br>
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 file, boolean append)</code><br>
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 fdObj)</code><br>
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 name)</code><br>
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 name, boolean append)</code><br>
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>
public static void main(String argv[]){<br>
FileInputStream fin;<br>
FileOutputStream fout;<br>
char ch;<br>
try{<br>
fin=new FileInputStream(FileDescriptor.in);<br>
fout=new FileOutputStream("output.txt");<br>
System.out.println("请输入一行字符:");<br>
while((ch=(char)fin.read())!='\n')<br>
fout.write(ch);<br>
fin.close();<br>
fout.close(); <br>
System.out.println("文件写入成功!");<br>
}<br>
catch(FileNotFoundException e){<br>
System.out.println("不能创建文件");<br>
}<br>
catch(IOException e){<br>
System.out.println("输入流有误!");<br>
}<br>
}<br>
}</p>
<p align="left">例2、读出指定文件中的内容,显示在屏幕上</p>
<p align="left">import java.io.*;<br>
class TypeFile{<br>
public static void main(String argv[]){<br>
FileInputStream fin;<br>
FileOutputStream fout;<br>
int ch; //这里要用int型<br>
if (argv.length<1)<br>
{ System.out.println("请指定文件名");<br>
return;<br>
} <br>
try{<br>
fin=new FileInputStream(argv[0]);<br>
fout=new FileOutputStream(FileDescriptor.out);<br>
while((ch=fin.read())!=-1) //用char型这里会出错<br>
fout.write(ch);<br>
// System.out.print((char)ch);
用这个输出中文会有问题<br>
fin.close(); <br>
fout.close();<br>
}<br>
catch(FileNotFoundException e){<br>
System.out.println("文件没找到!");<br>
}<br>
catch(IOException e){<br>
System.out.println("读入文件有误!");<br>
}<br>
}<br>
}</p>
<p align="left">例3、复制文件</p>
<p align="left">import java.io.*;<br>
class CopyFile{<br>
public static void main(String argv[]){<br>
FileInputStream fin;<br>
FileOutputStream fout;<br>
int ch;<br>
if (argv.length!=2)<br>
{ System.out.println("参数格式不对,应该为:java CopyFile 源文件名 目标文件名");<br>
return;<br>
} <br>
try{<br>
fin=new FileInputStream(argv[0]);<br>
fout=new FileOutputStream(argv[1]);<br>
while((ch=fin.read())!=-1)<br>
fout.write(ch);<br>
fin.close(); <br>
fout.close();<br>
System.out.println("文件复制成功");<br>
}<br>
catch(FileNotFoundException e){<br>
System.out.println("文件无法打开!");<br>
}<br>
catch(IOException e){<br>
System.out.println("读写文件有误!");<br>
}<br>
}<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 + -