??xml version="1.0" encoding="utf-8" standalone="yes"?>国产一区二区免费在线观看,日韩久久精品网,亚洲精品在线观看网站http://www.aygfsteel.com/cool2009/category/38165.html专注于java相关技?zh-cnSun, 08 Mar 2009 13:33:46 GMTSun, 08 Mar 2009 13:33:46 GMT60d明白Java的IOpȝ (转蝲)http://www.aygfsteel.com/cool2009/articles/258459.html龙华?/dc:creator>龙华?/author>Sun, 08 Mar 2009 06:05:00 GMThttp://www.aygfsteel.com/cool2009/articles/258459.htmlhttp://www.aygfsteel.com/cool2009/comments/258459.htmlhttp://www.aygfsteel.com/cool2009/articles/258459.html#Feedback0http://www.aygfsteel.com/cool2009/comments/commentRss/258459.htmlhttp://www.aygfsteel.com/cool2009/services/trackbacks/258459.html一Q?Input和Output
1. stream代表的是M有能力出数据的数据源,或是M有能力接收数据的接收源。在Java的IO中,所有的streamQ包括Input和Out streamQ都包括两种cdQ?br /> 1.1 以字节ؓ导向的stream
以字节ؓ导向的streamQ表CZ字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种cdQ?br /> 1) input streamQ?br /> 1) ByteArrayInputStreamQ把内存中的一个缓冲区作ؓInputStream使用
2) StringBufferInputStreamQ把一个String对象作ؓInputStream
3) FileInputStreamQ把一个文件作为InputStreamQ实现对文g的读取操?br /> 4) PipedInputStreamQ实Cpipe的概念,主要在线E中使用
5) SequenceInputStreamQ把多个InputStream合ƈZ个InputStream
2) Out stream
1) ByteArrayOutputStreamQ把信息存入内存中的一个缓冲区?br /> 2) FileOutputStreamQ把信息存入文g?br /> 3) PipedOutputStreamQ实Cpipe的概念,主要在线E中使用
4) SequenceOutputStreamQ把多个OutStream合ƈZ个OutStream
1.2 以Unicode字符为导向的stream
以Unicode字符为导向的streamQ表CZUnicode字符为单位从stream中读取或往stream中写入信息。以Unicode字符为导向的stream包括下面几种cdQ?br /> 1) Input Stream
1) CharArrayReaderQ与ByteArrayInputStream对应
2) StringReaderQ与StringBufferInputStream对应
3) FileReaderQ与FileInputStream对应
4) PipedReaderQ与PipedInputStream对应
2) Out Stream
1) CharArrayWriteQ与ByteArrayOutputStream对应
2) StringWriteQ无与之对应的以字节为导向的stream
3) FileWriteQ与FileOutputStream对应
4) PipedWriteQ与PipedOutputStream对应

以字Wؓ导向的stream基本上对有与之相对应的以字节为导向的stream。两个对应类实现的功能相同,字是在操作时的导向不同。如CharArrayReaderQ和ByteArrayInputStream的作用都是把内存中的一个缓冲区作ؓInputStream使用Q所不同的是前者每ơ从内存中读取一个字节的信息Q而后者每ơ从内存中读取一个字W?br /> 1.3 两种不现导向的stream之间的{?br /> InputStreamReader和OutputStreamReaderQ把一个以字节为导向的stream转换成一个以字符为导向的stream?br /> 2. streamd属?br /> 2.1 “为streamd属?#8221;的作?br /> q用上面介绍的Java中操作IO的APIQ我们就可完成我们想完成的Q何操作了。但通过FilterInputStream和FilterOutStream的子c,我们可以为streamd属性?br /> 下面以一个例子来说明q种功能的作用?br /> 如果我们要往一个文件中写入数据Q我们可以这h作:
FileOutStream fs = new FileOutStream(“test.txt”);
然后可以通过产生的fs对象调用write()函数来往test.txt文g中写入数据了。但是,如果我们惛_?#8220;先把要写入文件的数据先缓存到内存中,再把~存中的数据写入文g?#8221;的功能时Q上面的API没有一个能满我们的需求了。但是通过FilterInputStream和FilterOutStream的子c,为FileOutStreamd我们所需要的功能?br /> 2.2 FilterInputStream的各U类?br /> 2.2.1 用于装以字节ؓ导向的InputStream
1) DataInputStreamQ从stream中读取基本类型(int、char{)数据?br /> 2) BufferedInputStreamQ用缓冲区
3) LineNumberInputStreamQ会记录input stream内的行数Q然后可以调用getLineNumber()和setLineNumber(int)
4) PushbackInputStreamQ很用刎ͼ一般用于编译器开?br /> 2.2.2 用于装以字Wؓ导向的InputStream
1) 没有与DataInputStream对应的类。除非在要用readLine()时改用BufferedReaderQ否则用DataInputStream
2) BufferedReaderQ与BufferedInputStream对应
3) LineNumberReaderQ与LineNumberInputStream对应
4) PushBackReaderQ与PushbackInputStream对应
2.3 FilterOutStream的各U类?br /> 2.2.3 用于装以字节ؓ导向的OutputStream
1) DataIOutStreamQ往stream中输出基本类型(int、char{)数据?br /> 2) BufferedOutStreamQ用缓冲区
3) PrintStreamQ生格式化输出
2.2.4 用于装以字Wؓ导向的OutputStream
1) BufferedWriteQ与对应
2) PrintWriteQ与对应
3. RandomAccessFile
1) 可通过RandomAccessFile对象完成Ҏ件的d操作
2) 在生一个对象时Q可指明要打开的文件的性质QrQ只读;wQ只写;rw可读?br /> 3) 可以直接跛_文g中指定的位置
4. I/O应用的一个例?br />
Java codeimportjava.io.*;publicclassTestIO{publicstaticvoidmain(String[] args)throwsIOException{//1.以行为单位从一个文件读取数据BufferedReader in=newBufferedReader(newFileReader("F:\\nepalon\\TestIO.java"));
String s, s2=newString();while((s=in.readLine())!=null)
s2+=s+"\n";
in.close();//1b. 接收键盘的输入BufferedReader stdin=newBufferedReader(newInputStreamReader(System.in));
System.out.println("Enter a line:");
System.out.println(stdin.readLine());//2. 从一个String对象中读取数据StringReader in2=newStringReader(s2);intc;while((c=in2.read())!=-1)
System.out.println((char)c);
in2.close();//3. 从内存取出格式化输入try{
DataInputStream in3=newDataInputStream(newByteArrayInputStream(s2.getBytes()));while(true)
System.out.println((char)in3.readByte());
}catch(EOFException e){
System.out.println("End of stream");
}//4. 输出到文件try{
BufferedReader in4=newBufferedReader(newStringReader(s2));
PrintWriter out1=newPrintWriter(newBufferedWriter(newFileWriter("F:\\nepalon\\ TestIO.out")));intlineCount=1;while((s=in4.readLine())!=null)
out1.println(lineCount+++"Q?+s);
out1.close();
in4.close();
}catch(EOFException ex){
System.out.println("End of stream");
}//5. 数据的存储和恢复try{
DataOutputStream out2=newDataOutputStream(newBufferedOutputStream(newFileOutputStream("F:\\nepalon\\ Data.txt")));
out2.writeDouble(3.1415926);
out2.writeChars("\nThas was pi:writeChars\n");
out2.writeBytes("Thas was pi:writeByte\n");
out2.close();
DataInputStream in5=newDataInputStream(newBufferedInputStream(newFileInputStream("F:\\nepalon\\ Data.txt")));
BufferedReader in5br=newBufferedReader(newInputStreamReader(in5));
System.out.println(in5.readDouble());
System.out.println(in5br.readLine());
System.out.println(in5br.readLine());
}catch(EOFException e){
System.out.println("End of stream");
}//6. 通过RandomAccessFile操作文gRandomAccessFile rf=newRandomAccessFile("F:\\nepalon\\ rtest.dat","rw");for(inti=0; i<10; i++)
rf.writeDouble(i*1.414);
rf.close();

rf=newRandomAccessFile("F:\\nepalon\\ rtest.dat","r");for(inti=0; i<10; i++)
System.out.println("Value"+i+"Q?+rf.readDouble());
rf.close();

rf=newRandomAccessFile("F:\\nepalon\\ rtest.dat","rw");
rf.seek(5*8);
rf.writeDouble(47.0001);
rf.close();

rf=newRandomAccessFile("F:\\nepalon\\ rtest.dat","r");for(inti=0; i<10; i++)
System.out.println("Value"+i+"Q?+rf.readDouble());
rf.close();
}
}



关于代码的解释(以区为单位)Q?br /> 1ZQ当d文gӞ先把文g内容d~存中,当调用in.readLine()Ӟ再从~存中以字符的方式读取数据(以下U?#8220;~存字节d方式”Q?br /> 1bZQ由于想以缓存字节读取方式从标准IOQ键盘)中读取数据,所以要先把标准IOQSystem.inQ{换成字符导向的streamQ再q行BufferedReader装?br /> 2ZQ要以字W的形式从一个String对象中读取数据,所以要产生一个StringReadercd的stream?br /> 4ZQ对String对象s2d数据Ӟ先把对象中的数据存入~存中,再从~冲中进行读取;对TestIO.out文gq行操作Ӟ先把格式化后的信息输出到~存中,再把~存中的信息输出到文件中?br /> 5ZQ对Data.txt文gq行输出Ӟ是先把基本类型的数据输出屋缓存中Q再把缓存中的数据输出到文g中;Ҏ件进行读取操作时Q先把文件中的数据读取到~存中,再从~存中以基本cd的Ş式进行读取。注意in5.readDouble()q一行。因为写入第一个writeDouble()Q所以ؓ了正显C。也要以基本cd的Ş式进行读取?br /> 6区是通过RandomAccessFilecd文gq行操作?br />



]]>
java_N?5道题Q{载)http://www.aygfsteel.com/cool2009/articles/258397.html龙华?/dc:creator>龙华?/author>Sat, 07 Mar 2009 12:37:00 GMThttp://www.aygfsteel.com/cool2009/articles/258397.htmlhttp://www.aygfsteel.com/cool2009/comments/258397.htmlhttp://www.aygfsteel.com/cool2009/articles/258397.html#Feedback0http://www.aygfsteel.com/cool2009/comments/commentRss/258397.htmlhttp://www.aygfsteel.com/cool2009/services/trackbacks/258397.html阅读全文

]]>
java_N?5道题Q{载)http://www.aygfsteel.com/cool2009/articles/258394.html龙华?/dc:creator>龙华?/author>Sat, 07 Mar 2009 12:15:00 GMThttp://www.aygfsteel.com/cool2009/articles/258394.htmlhttp://www.aygfsteel.com/cool2009/comments/258394.htmlhttp://www.aygfsteel.com/cool2009/articles/258394.html#Feedback0http://www.aygfsteel.com/cool2009/comments/commentRss/258394.htmlhttp://www.aygfsteel.com/cool2009/services/trackbacks/258394.html阅读全文

]]>
վ֩ģ壺 | | | γ| ޻| | | ʯ| | Ͽ| | | | | | | | | | ɳ| ƽɽ| | ԭ| Ӣɽ| | ÷ӿ| | | ʡ| | | | γ| | ԰| Ͻ| Դ| | ֿ| ƺ| |