欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java简介以及了解java(二三)

程序员文章站 2022-02-19 06:34:30
...

基本输入输出所使用的类的介绍:
FileInputStream和FileOutputStream (文件输入输出流)
以上两个是字节流
1) 结点流,可以对磁盘文件进行操作。
2) 要构造一个FileInputStream, 所关联的文件必须存在而且是可读的。
3) 要构造一个FileOutputStream而输出文件已经存在,则它将被覆盖。
FileInputStream infile = new FileInputStream("myfile.dat");
FIleOutputStream outfile = new FileOutputStream("results.dat");
FileOutputStream outfile = new FileOutputStream(“results.dat”,true);
参数为true时输出为添加,为false时为覆盖。

import java.io.*;
public class FileCopy {
public static void main(String[] args) {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(args[0]);
fo = new FileOutputStream("copy_"+args[0]);
byte[] bs=new byte[1024];
int i;
while((i=fi.read(bs))!=-1){
fo.write(bs,0,i);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(fi!=null)
try {
fi.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(fo!=null)
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

 

相关标签: Java