使用FileInputStream和fileOutputStream编写一个程序,接受一个文件的名称并把这个文件的内容写到另外一个文件中
import java.io.*;
public class Test{
public static void main(String[] args) {
byte[] buffer=new byte[1024];
int b = 0;
FileInputStream f=null;
FileOutputStream f1=null;
try {
f=new FileInputStream("E:/java/hibernate.doc");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1=new FileOutputStream("java1");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(f != null){
try {
b=f.read();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
f.read(buffer);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
f1.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
在public static void main(String[] args)后加上throw IOEception就不用try/catch了 |
阅读(718) | 评论(0) | 转发(0) |