本文主要是介绍Java-Work9-文件输入与输出(2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目要求
- 已存在的文件source.txt里有一段文字,现键盘上输入(使用缓冲流实现)一个文件名比如:copy.txt
- 创建这个文件,并将source.txt的内容复制到copy.txt中(使用字节流实现)
题目要点
- FileInputStream 类
- FileOutputStream 类
- BufferedInputStream 类
- BufferedOutputStream 类
- FileInputStream 与 BufferedInputStream 的串联
- FileOutputStream 与 BufferedOutputStream 的串联
题目代码
package work9;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Work9_2
{
public static void main(String[] args)
{
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
int c;
try
{
System.out.println("Input file name:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String fname = in.readLine();
fis = new FileInputStream("D:/ch8/source.txt");
bis = new BufferedInputStream(fis);
fos = new FileOutputStream("D:/ch8/"+fname);
bos = new BufferedOutputStream(fos);
while((c = bis.read() )!= -1)
{
bos.write(c);
}
bos.flush();
System.out.println("File copy done.");
}
catch(IOException e)
{
System.out.println(e);
}
finally
{
try
{
if(fis != null) fis.close();
if(fos != null) fos.close();
if(bis != null) bis.close();
if(bos != null) bos.close();
}
catch(IOException e1)
{
System.out.println(e1);
}
}
}
}
这篇关于Java-Work9-文件输入与输出(2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!