Java开发核心技术-下_第1页
Java开发核心技术-下_第2页
Java开发核心技术-下_第3页
Java开发核心技术-下_第4页
Java开发核心技术-下_第5页
已阅读5页,还剩157页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、第1章 输入输出【目标】懂得Java语言是把从键盘输入数据,在文件中读写数据,在网络上传输数据这些不同来源和目标的数据都统一抽象为流,并通过一系列完整的类来完成I0操作。学会用法JAVA的各种流。本章叙述以下内容:输入输出的基础字节流文本流桥接流序列化1.1 输入输出基础IO(Input/Output)是计算机输入/输出的接口。Java的核心库java.io供应了全面的IO接口,包括:文件读写,标准设备输出等等。Java中IO是以流为基础进行输入输出的,全部数据被串行化写入输出流,或者从输入流读入。此外,Java也对块传输供应支持,在核心库java.nio中接受的便是块IO。流IO的好处是简洁

2、易用,缺点是效率较低。块IO效率很高,但编程比较简单。1.1.1 标准设备的输入输出在Java中,我们可以利用java.lang.System类中的两种属性,它供应从键盘输入和从终端输出的方法System.in和System.out。我们将在下面具体介绍这两种方法。1.1.2 读取把握台输入Java中,把握台输入由从System.in读取数据来完成。为获得属于把握台的字符流,在BufferedReader对象中包装System.in。BufferedReader 支持缓冲输入流。它最常见的构造函数如下:BufferedReader(Reader inputReader)这里,inputRead

3、er是链接被创建的BufferedReader实例的流。Reader是一个抽象类。它的一个具体的子类是InputStreamReader,该子类把字节转换成字符。为获得链接System.in的一个InputStreamReader的对象,用下面的构造函数:InputStreamReader(InputStream inputStream)因为System .in引用了InputStream 类型的对象,它可以用于inputStream。综上所述,下面的一行代码创建了与键盘相连的BufferedReader对象。BufferedReader br = new BufferedReader(new

4、InputStreamReader(System.in);当该语句执行后,br是通过System.in生成的链接把握台的字符流。1 读取字符从BufferedReader读取字符,用read()。我们所用的read()版本如下:int read( ) throws IOException该方法每次执行都从输入流读取一个字符然后以整型返回。当遇到流的末尾时它返回-1。我们可以看到,它要引发一个IOException异样。下面的例子程序演示了read()方法,从把握台读取字符直到用户键入“q”:import java.io.*;public class QExit public static vo

5、id main(String args) throws IOException char c;BufferedReader br = new BufferedReader(new InputStreamReader(System.in);System.out.println(输入字符 q 则退出.);do c = (char) br.read();System.out.println(c); while (c != q);留意 :因为System.in在默认状况下是以行来缓冲的。这意味着在我们键入ENTER以前实际上是没有输入的。我们能猜想,这不能充分体现交互式把握台输入条件下read()的独

6、特价值2 读取字符串从键盘读取字符串,用法readLine()。它是BufferedReader 类的成员。它的通常形式如下:String readLine( ) throws IOException它返回一个String对象。下面的例子阐述了BufferedReader类和readLine()方法;程序读取和显示文本的行直到键入“stop”:import java.io.*;public class StopExit public static void main(String args) throws IOException BufferedReader br = new Buffered

7、Reader(new InputStreamReader(System.in);String str;System.out.println(请输入一行文本.);System.out.println(输入 stop 则退出.);do str = br.readLine();System.out.println(str); while (!str.equals(stop);1.1.3 向把握台输出把握台输出由前面描述过的print( ) 和 println( )来完成最为简洁,它们被用在本书的大多数例题中。这两种方法由PrintStream(System.out引用的对象类型)定义。尽管Syste

8、m.out是一个字节流,用它作为简洁程序的输出是可行的。字符流输出在下节介绍。因为PrintStream是从OutputStream派生的输出流,它同样实现低级方法write( ),write( )可用来向把握台写数据。PrintStream 定义的write( )的最简洁的形式如下:void write(int byteval)该方法依据byteval指定的数向文件写字节。尽管byteval 定义成整数,但只有低位的8个字节被写入。下面的短例用 write( )向屏幕输出字符“A”,然后是新的行。public class WriteDemo public static void main(S

9、tring args) int b;b = A;System.out.write(b);System.out.write(n);一般不常用write( )来完成向把握台的输出(尽管这样做在某些场合格外有用),因为print( )和println( ) 更简洁用。1 System.outSystem.out是PrintStream,它可以被用作发送输出到终端。它将为我们处理串的其他类型的转换。例如,假如我们想要读取一个整数,加1,然后输出到终端,我们会看到下面的代码,它显示了输出工作的过程。 import java.util.Date;public class ShowSysOut public

10、 static void main(String args) String s = String;int i = 12;Date d = new Date();System.out.println(this is +s);System.out.println(i);System.out.println(d);输出为:this is String12Sat Jul 23 22:59:39 CST 20052 PrintWriter尽管Java允许用System.out向把握台写数据,但建议仅用在调试程序时或在例题中。对于实际的程序,Java推举的向把握台写数据的方法是用PrintWriter流。

11、PrintWriter是基于字符的类。用基于字符类向把握台写数据使程序更为国际化。PrintWriter定义了多个构造函数,我们所用到的一个如下:PrintWriter(OutputStream outputStream, boolean flushOnNewline)这里,outputStream是OutputStream类的对象,flushOnNewline把握Java是否在println( )方法被调用时刷新输出流。假如flushOnNewline为true,刷新自动发生,若为false,则不发生。PrintWriter支持全部类型(包括Object)的print( )和println(

12、 )方法,这样,我们就可以像用System.out那样用这些方法。假如遇到不同类型的状况,PrintWriter方法调用对象的toString( )方法并打印结果。用PrintWriter向外设写数据,指定输出流为System.out并在每一新行后刷新流。例如这行代码创建了与把握台输出相连的PrintWriter类。PrintWriter pw = new PrintWriter(System.out, true);用PrintWriter向外设写数据,指定输出流为System.out并在每一新行后刷新流。例如这行代码创建了与把握台输出相连的PrintWriter类。PrintWriter p

13、w = new PrintWriter(System.out, true);下面的应用程序说明白用PrintWriter处理把握台输出的方法:import java.io.*;import java.util.Date;public class PrintWriterDemo public static void main(String args) String s = String;int i = 12;Date d = new Date();PrintWriter pw = new PrintWriter(System.out, true);pw.println(This is a + s

14、);pw.println(i);pw.println(d);该程序的输出如下:This is a String12Sat Jul 23 23:03:31 CST 2005在我们学习Java或调试程序时用System.out向把握台写简洁文本输出是没有错的。但是用法PrintWriter使实际的应用程序更简洁国际化。因为在本章所示的例题程序中用PrintWriter并没有多大的优势,所以我们连续用System.out来向把握台输出。1.2 java.io1.2.1 File类File类供应了若干处理文件和猎取它们基本信息的方法。File myFile;myFile = new File(mymo

15、td); myFile = new File(/, mymotd);/ more useful if the directory or filename is/ a variable File myDir = new File(/);myFile = new File(myDir, mymotd);我们所用法的构造函数经常取决于我们所用法的其他文件对象。例如,假如我们在我们的应用程序中只用法一个文件,那么就会用法第一个构造函数。假如我们用法一个公共名目中的若干文件,那么用法其次个或者第三个构造函数可能更简洁。File类供应了独立于平台的方法来操作由本地文件系统维护的文件。然而它不允许我们存取文

16、件的内容。import java.io.*;public class FileOperate public FileOperate() /* * 显示名目下全部文件和名目 * * param folderPath * String 如 c:/fqf */public void showFolder(String folderPath) try File f1 = new File(folderPath);if (f1.isDirectory() String s = f1.list();for (int i = 0; i s.length; i+) File f = new File(fold

17、erPath + / + si);if (f.isDirectory() System.out.println(si + 是名目); else System.out.println(si + 是文件); else System.out.println(folderPath + 参数不是一个名目); catch (Exception e) System.out.println(新建名目操作出错);e.printStackTrace();/* * 显示全部的 html文件 * * param folderPath * String 如 c:/fqf */public void showHtmlFi

18、le(String folderPath) try File f1 = new File(folderPath);FilenameFilter only = new OnlyExt(html);String s = f1.list(only);for (int i = 0; i s.length; i+) System.out.println(si); catch (Exception e) System.out.println(新建名目操作出错);e.printStackTrace();/* * 新建名目 * * param folderPath * String 如 c:/fqf */pu

19、blic void newFolder(String folderPath) try String filePath = folderPath;filePath = filePath.toString();java.io.File myFilePath = new java.io.File(filePath);if (!myFilePath.exists() myFilePath.mkdir(); catch (Exception e) System.out.println(新建名目操作出错);e.printStackTrace();/* * 新建文件 * * param filePathAn

20、dName * String 文件路径及名称 如c:/fqf.txt * param fileContent * String 文件内容 */public void newFile(String filePathAndName, String fileContent) try String filePath = filePathAndName;filePath = filePath.toString();File myFilePath = new File(filePath);if (!myFilePath.exists() myFilePath.createNewFile();FileWri

21、ter resultFile = new FileWriter(myFilePath);PrintWriter myFile = new PrintWriter(resultFile);String strContent = fileContent;myFile.println(strContent);resultFile.close(); catch (Exception e) System.out.println(新建名目操作出错);e.printStackTrace();/* * 删除文件 * * param filePathAndName * String 文件路径及名称 如c:/fq

22、f.txt * param fileContent * String */public void delFile(String filePathAndName) try String filePath = filePathAndName;filePath = filePath.toString();java.io.File myDelFile = new java.io.File(filePath);myDelFile.delete(); catch (Exception e) System.out.println(删除文件操作出错);e.printStackTrace();/* * 删除文件

23、夹 * * param filePathAndName * String 文件夹路径及名称 如c:/fqf * param fileContent * String */public void delFolder(String folderPath) try delAllFile(folderPath); /删除完里面全部内容String filePath = folderPath;filePath = filePath.toString();java.io.File myFilePath = new java.io.File(filePath);myFilePath.delete(); /删

24、除空文件夹 catch (Exception e) System.out.println(删除文件夹操作出错);e.printStackTrace();/* * 删除文件夹里面的全部文件 * * param path * String 文件夹路径 如 c:/fqf */public void delAllFile(String path) File file = new File(path);if (!file.exists() return;if (!file.isDirectory() return;String tempList = file.list();File temp = nul

25、l;for (int i = 0; i tempList.length; i+) if (path.endsWith(File.separator) temp = new File(path + tempListi); else temp = new File(path + File.separator + tempListi);if (temp.isFile() temp.delete();if (temp.isDirectory() /先删除文件夹里面的文件delAllFile(path + / + tempListi); /再删除空文件夹delFolder(path + / + temp

26、Listi); /* * 复制单个文件 * * param oldPath * String 原文件路径 如:c:/fqf.txt * param newPath * String 复制后路径 如:f:/fqf.txt * return boolean */public void copyFile(String oldPath, String newPath) try int bytesum = 0;int byteread = 0;File oldfile = new File(oldPath);if (oldfile.exists() /文件存在时读入原文件InputStream inSt

27、ream = new FileInputStream(oldPath); FileOutputStream fs = new FileOutputStream(newPath);byte buffer = new byte1444;int length;while (byteread = inStream.read(buffer) != -1) bytesum += byteread; /字节数 文件大小System.out.println(bytesum);fs.write(buffer, 0, byteread);inStream.close(); catch (Exception e)

28、System.out.println(复制单个文件操作出错);e.printStackTrace();/* * 复制整个文件夹内容 * * param oldPath * String 原文件路径 如:c:/fqf * param newPath * String 复制后路径 如:f:/fqf/ff * return boolean */public void copyFolder(String oldPath, String newPath) try /假如文件夹不存在 则建立新文件夹(new File(newPath).mkdirs();File a = new File(oldPath)

29、;String file = a.list();File temp = null;for (int i = 0; i file.length; i+) if (oldPath.endsWith(File.separator) temp = new File(oldPath + filei); else temp = new File(oldPath + File.separator + filei);if (temp.isFile() FileInputStream input = new FileInputStream(temp);FileOutputStream output = new

30、FileOutputStream(newPath + / + (temp.getName().toString();byte b = new byte1024 * 5;int len;while (len = input.read(b) != -1) output.write(b, 0, len);output.flush();output.close();input.close();if (temp.isDirectory() /假如是子文件夹copyFolder(oldPath + / + filei, newPath + / + filei); catch (Exception e) S

31、ystem.out.println(复制整个文件夹内容操作出错);e.printStackTrace();class OnlyExt implements FilenameFilter String ext;public OnlyExt(String ext) this.ext = . + ext;public boolean accept(File dir, String name) return name.endsWith(ext);1.2.2 流类Java 的流式输入/输出建立在四个抽象类的基础上:InputStream, OutputStream, Reader和Writer。它们用来

32、创建具体流式子类。尽管程序通过具体子类执行输入/输出操作,顶层的类定义了全部流类的基础通用功能。InputStream 和OutputStream 设计成字节流类。Reader 和Writer 为字符流设计。字节流类和字符流类形成分别的层次结构。一般说来,处理字符或字符串时应用法字符流类,处理字节或二进制对象时应用字节流类。Java的输入输出的流式接口为简单而繁重的任务供应了一个简洁的抽象。过滤流类的组合允许我们动态建立客户端流式接口来协作数据传输要求。继承高级流类InputStream、InputStreamReader、Reader和Writer 类的Java程序在将来(即使创建了新的和改

33、进的具体类)也能得到合理运用。最终,对象序列化有望在将来的Java编程中扮演一个越来越重要的角色。Java的序列化输入/输出类为这些有时显得格外简单的任务供应了便携的解决方法。1.2.3 字节流字节流类为处理字节式输入/输出供应了丰富的环境。一个字节流可以和其他任何类型的对象并用,包括二进制数据。这样的多功能性使得字节流对很多类型的程序都很重要。因为字节流类以InputStream 和OutputStream为顶层,我们就从争辩这两个类开头。1. InputStream(输入流)InputStream 是一个定义了Java流式字节输入模式的抽象类。该类的全部方法在出错条件下引发一个IOExce

34、ption 异样。方法描述int available( ) 返回当前可读的输入字节数void close( ) 关闭输入源。关闭之后的读取会产生IOException 异样void mark(int numBytes) 在输入流的当前点放置一个标记。该流在读取numBytes 个字节前都保持有效boolean markSupported( ) 假如调用的流支持mark( )/reset( ) 就返回true int read( ) 假如下一个字节可读则返回一个整型,遇到文件尾时返回-1 int read(byte buffer ) 试图读取buffer.length 个字节到buffer 中,

35、并返回实际成功读取的字节数。遇到文件尾时返回-1int read(byte buffer , int offset, int numBytes)试图读取buffer 中从bufferoffset 开头的numBytes 个字节,返回实际读取的字节数。遇到文件尾时返回-1void reset( ) 重新设置输入指针到从前设置的标记处long skip(long numBytes) 忽视numBytes 个输入字节,返回实际忽视的字节数表1-12. OutputStream(输出流)OutputStream是定义了流式字节输出模式的抽象类。该类的全部方法返回一个void 值并且在出错状况下引发一个

36、IOException异样。方法描述void close( ) 关闭输出流。关闭后的写操作会产生IOException 异样void flush( ) 定制输出状态以使每个缓冲器都被清除,也就是刷新输出缓冲区void write(int b) 向输出流写入单个字节。留意参数是一个整型数,它允许我们不必把参数转换成字节型就可以调用write()void write(byte buffer ) 向一个输出流写一个完整的字节数组void write(byte buffer , int offset, int numBytes)写数组buffer 以bufferoffset 为起点的numBytes

37、个字节区域内的内容表1-23. FileInputStream(文件输入流)FileInputStream 类创建一个能从文件读取字节的InputStream 类,它的两个常用的构造函数如下:FileInputStream(String filepath)FileInputStream(File fileObj)它们都能引发FileNotFoundException异样。这里,filepath 是文件的全称路径,fileObj是描述该文件的File对象尽管第一个构造函数可能更常用到,其次个构造函数允许在把文件赋给输入流之前用我们在前面介绍过得File方法更进一步检查文件。当一个FileInpu

38、tStream被创建时,它可以被公开读取。FileInputStream重载了抽象类InputStream的六个方法,mark( )和reset( )方法不被重载,任何关于用法FileInputStream的reset()尝试都会生成IOException异样。下面的例题说明白怎样读取单个字节、字节数组以及字节数组的子界。它同样阐述了怎样运用available( )判定剩余的字节个数及怎样用skip( )方法跳过不必要的字节。该程序读取它自己的源文件,该源文件必定在当前名目中。import java.io.*;class FileInputStreamDemo public static vo

39、id main(String args) throws Exception int size;InputStream f = new FileInputStream(FileInputStreamDemo.java);System.out.println(Total Available Bytes: + (size = f.available();int n = size / 40;System.out.println(First + n+ bytes of the file one read() at a time);for (int i = 0; i n; i+) System.out.p

40、rint(char) f.read();System.out.println(nStill Available: + f.available();System.out.println(Reading the next + n + with one read(b);byte b = new byten;if (f.read(b) != n) System.err.println(couldnt read + n + bytes.);System.out.println(new String(b, 0, n);System.out.println(nStill Available: + (size

41、 = f.available();System.out.println(Skipping half of remaining bytes with skip();f.skip(size / 2);System.out.println(Still Available: + f.available();System.out.println(Reading + n / 2 + into the end of array);if (f.read(b, n / 2, n / 2) != n / 2) System.err.println(couldnt read + n / 2 + bytes.);Sy

42、stem.out.println(new String(b, 0, b.length);System.out.println(nStill Available: + f.available();f.close();下面是该程序的输出:Total Available Bytes: 1433First 35 bytes of the file one read() at a time/ Demonstrate FileInputStream.imStill Available: 1398Reading the next 35 with one read(b)port java.io.*;class

43、 FileInputSStill Available: 1363Skipping half of remaining bytes with skip()Still Available: 682Reading 17 into the end of arrayport java.io.*;read(b) != n) SStill Available: 6654. FileOutputStream(文件输出流)FileOutputStream 创建了一个可以向文件写入字节的类OutputStream,它常用的构造函数如下:FileOutputStream(String filePath)FileOu

44、tputStream(File fileObj)FileOutputStream(String filePath, boolean append)它们可以引发IOException或SecurityException异样。这里filePath是文件的全称路径,fileObj是描述该文件的File对象。假如append为true,文件以设置搜寻路径模式打开。FileOutputStream的创建不依靠于文件是否存在。在创建对象时FileOutputStream在打开输出文件之前创建它。这种状况下我们试图打开一个只读文件,会引发一个IOException异样。下面的例子创建一个样本字节缓冲器。先生

45、成一个String对象,接着用getBytes( )方法提取字节数组对等体。然后创建了三个文件。第一个file1.txt将包括样本中的各个字节。其次个文件是file2.txt,它包括全部字节。第三个也是最终一个文件file3.txt,仅包含最终的四分之一。不像FileInputStream类的方法,全部FileOutputStream类的方法都返回一个void类型值。在出错状况下,这些方法将引发IOException异样。import java.io.*;class FileOutputStreamDemo public static void main(String args) throws

46、 Exception String source = Now is the time for all good menn+ to come to the aid of their countryn+ and pay their due taxes.;byte buf = source.getBytes();OutputStream f0 = new FileOutputStream(file1.txt);for (int i = 0; i buf.length; i += 2) f0.write(bufi);f0.close();OutputStream f1 = new FileOutput

47、Stream(file2.txt);f1.write(buf);f1.close();OutputStream f2 = new FileOutputStream(file3.txt);f2.write(buf, buf.length - buf.length / 4, buf.length / 4);f2.close();5. ByteArrayOutputStream(字节数组输出流)ByteArrayOutputStream是一个把字节数组当作输出流的实现。ByteArrayOutputStream有两个构造函数,如下:ByteArrayOutputStream( )ByteArrayO

48、utputStream(int numBytes)在第一种形式里,一个32位字节的缓冲器被生成。其次个构造函数生成一个跟指定numBytes相同位数的缓冲器。缓冲器保存在ByteArrayOutputStream的受爱护的buf 成员里。缓冲器的大小在需要的状况下会自动增加。缓冲器保存的字节数是由ByteArrayOutputStream的受爱护的count域保存的。import java.io.*;class ByteArrayOutputStreamDemo public static void main(String args) throws IOException ByteArrayO

49、utputStream f = new ByteArrayOutputStream();String s = This should end up in the array;byte buf = s.getBytes();f.write(buf);System.out.println(Buffer as a string);System.out.println(f.toString();System.out.println(Into array);byte b = f.toByteArray();for (int i = 0; i b.length; i+) System.out.print(

50、char) bi);System.out.println(nTo an OutputStream();OutputStream f2 = new FileOutputStream(test.txt);f.writeTo(f2);f2.close();System.out.println(Doing a reset);f.reset();for (int i = 0; i 3; i+)f.write(X);System.out.println(f.toString();运行程序后,生成下面的输出。留意在调用reset( )之后,三个X怎样结束。Buffer as a stringThis sho

51、uld end up in the arrayInto arrayThis should end up in the arrayTo an OutputStream()Doing a resetXXX该例用 writeTo( )这一便捷的方法将f 的内容写入test.txt,检查在前面例子中生成的test.txt文件内容,结果如下:This should end up in the array6. 缓冲字节流对于字节流,缓冲流(buffered stream),通过把内存缓冲器连到输入/输出流扩展一个过滤流类。该缓冲器允许Java对多个字节同时进行输入/输出操作,提高了程序性能。因为缓冲器可用

52、,所以可以跳过、标记和重新设置流。缓冲字节流类是BufferedInputStream 和BufferedOutputStream。PushbackInputStream 也可实现缓冲流。BufferedInputStream(缓冲输入流)缓冲输入/输出是一个格外一般的性能优化。Java 的BufferedInputStream 类允许把任何InputStream 类“包装”成缓冲流并使它的性能提高。BufferedInputStream 有两个构造函数:BufferedInputStream(InputStream inputStream)BufferedInputStream(InputS

53、tream inputStream, int bufSize)第一种形式生成了一个默认缓冲长度的缓冲流。其次种形式缓冲器大小是由bufSize传入的。用法内存页或磁盘块等的若干倍的缓冲区大小可以给执行性能带来很大的正面影响。但这是依靠于执行状况的。最抱负的缓冲长度一般与主机操作系统、可用内存空间及机器配置有关。合理利用缓冲不需要特殊简单的操作。一般缓冲大小为8192个字节,给输入/输出流设定一个更小的缓冲器通常是好的方法。用这样的方法,低级系统可以从磁盘或网络读取数据块并在缓冲器中存储结果。因此,即使我们在InputStream外同时读取字节数据时,也可以在超过99.9%的时间里获得快速存储操

54、作。缓冲一个输入流同样供应了在可用缓冲器的流内支持向后移动的必备基础。除了在任何InputStream类中执行的read( )和skip( )方法外,BufferedInputStream 同样支持mark( ) 和reset( )方法。BufferedInputStream.markSupported( )返回true是这一支持的体现。下面的例子设计了一种情形,该情形下,我们可以用法mark( )来记忆我们在输入流中的位置,然后用reset( )方法返回该位置。这个例子分析了HTML实体的引用为版权信息的状况。这个引用以一个&符号开头以分号(;)结束,没有任何空格。例子输入由两个& 符号来说

55、明何处reset( )发生,何处不发生的状况。import java.io.*;class BufferedInputStreamDemo public static void main(String args) throws IOException String s = This is a copyright symbol + but this is © not.n;byte buf = s.getBytes();ByteArrayInputStream in = new ByteArrayInputStream(buf);BufferedInputStream f = new Bu

56、fferedInputStream(in);int c;boolean marked = false;while (c = f.read() != -1) switch (c) case &:if (!marked) f.mark(32);marked = true; else marked = false;break;case ;:if (marked) marked = false;System.out.print(c); elseSystem.out.print(char) c);break;case :if (marked) marked = false;f.reset();Syste

57、m.out.print(&); elseSystem.out.print(char) c);break;default:if (!marked)System.out.print(char) c);break;留意该例运用mark(32),该方法保存接下来所读取的32个字节(这个数量对全部的实体引用都足够)。下面是程序的输出:This is a (c) copyright symbol but this is © not.警告:在缓冲器中用法 mark( )是受限的。其含义为我们只能给mark( )定义一个小于流缓冲大小的参数。BufferedOutputStream(缓冲输出流)Buf

58、feredOutputStream与任何一个OutputStream相同,除了用一个另外的flush( ) 方法来保证数据缓冲器被写入到实际的输出设备。因为BufferedOutputStream是通过减小系统写数据的时间而提高性能的,可以调用flush( )方法生成缓冲器中待写的数据。不像缓冲输入,缓冲输出不供应额外的功能,Java中输出缓冲器是为了提高性能的。下面是两个可用的构造函数:BufferedOutputStream(OutputStream outputStream)BufferedOutputStream(OutputStream outputStream, int bufSi

59、ze)第一种形式创建了一个用法512字节缓冲器的缓冲流。其次种形式,缓冲器的大小由bufSize参数传入。PushbackInputStream(推回输入流)缓冲的一个新颖的用法是实现推回(pushback)。Pushback用于输入流允许字节被读取然后返回(即“推回”)到流。PushbackInputStream类实现了这个想法。它供应了一种机制来“窥视”在没有受到破坏的状况下输入流生成了什么。PushbackInputStream有两个构造函数:PushbackInputStream(InputStream inputStream)PushbackInputStream(InputStre

60、am inputStream, int numBytes)第一种形式创建了一个允许一个字节推回到输入流的流对象。其次种形式创建了一个具有numBytes长度缓冲区的推回缓冲流。它允很多个字节推回到输入流。除了具有与InputStream相同的方法,PushbackInputStream供应了unread( )方法,表示如下:void unread(int ch)void unread(byte buffer )void unread(byte buffer, int offset, int numChars)第一种形式推回ch的低位字节,它将是随后调用read( )方法所返回的下一个字节。其次

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论