代码如下:
package 作业;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class 复制txt文本 {
public static void main(String[] args) {
makeTxt();
removeTxt();
}
//复制文本文档
public static void removeTxt() {
// TODO Auto-generated method stub
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fos=new FileOutputStream("D:\\b.txt");
fis=new FileInputStream("E:\\a.txt");
int x=0;
while((x=fis.read())!=-1){
fos.write(x);
System.out.println((char)x);
}
fis.close();
} catch (IOException ex) {
// TODO: handle exception
ex.printStackTrace();
throw new RuntimeException("空指针异常");
}finally{
if(fis==null)
try {
fis.close();
} catch (IOException ex) {
// TODO: handle exception
ex.printStackTrace();
throw new RuntimeException("空指针异常");
}
}
}
//创建文本文档
public static void makeTxt() {
// TODO Auto-generated method stub
FileOutputStream fos=null;
try {
fos=new FileOutputStream("E:\\a.txt");
fos.write("xiaomudaisdou".getBytes());
fos.close();
} catch (IOException ex) {
// TODO: handle exception
ex.printStackTrace();
throw new RuntimeException("空指针异常");
}finally{
if(fos==null)
try {
fos.close();
} catch (IOException ex) {
// TODO: handle exception
ex.printStackTrace();
throw new RuntimeException("空指针异常");
}
}
}
}
|
|