import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedInputOutputStreamExample
{
public static void main(String[] args) {
try{
BufferedInputStreamExample bisx=new BufferedInputStreamExample();
BufferedInputStream bis=bisx.inputMethod();
BufferedOutputStreamExample bosx=new BufferedOutputStreamExample();
bosx.outputMethod(bis);
}
catch(FileNotFoundException fnf)
{
System.out.println("Sorry--------File not exists");
}
catch(IOException io)
{
System.out.println("IOException ---:"+io.getMessage());
}
}
}
class BufferedInputStreamExample
{
BufferedInputStream bis=null;
BufferedInputStream inputMethod()throws FileNotFoundException,IOException
{
FileInputStream fin=new FileInputStream("C:/e-SDK-4.1-win32-x86_64 (1)/RahulExample/src/Test.java");
bis=new BufferedInputStream(fin);
int c;
while((c=bis.read())!=-1)
System.out.print((char)c);
System.out.println();
return bis;
}
}
class BufferedOutputStreamExample
{
BufferedOutputStream bos=null;
int outputMethod(BufferedInputStream bis)throws IOException,FileNotFoundException
{
bos=new BufferedOutputStream(new FileOutputStream("C:/varun.txt"));
int c;
while((c=bis.read())!=-1){
bos.write(c);
}
bis.close();
bos.close();
System.out.println("File created.............");
return 1;
}
}
在这个程序中,我们使用 bufferedinputstream 从文件中读取内容,当我想使用 bufferedoutputstream 将 Test.java 文件的内容写入 varun.txt 文件时,它会创建文件但不在 varun.txt 中写入任何内容。如果我们将 Test.java 中的内容写入 varun.txt 而不读取它,创建文件并写入两者。为什么这样做。