public static void main(String[] args) {
//将变量定义在外部方便后面关闭资源
InputStream fis=null;
OutputStream fos=null;
try {
fis = new FileInputStream("D:\\a.txt");
fos = new FileOutputStream("E:\\a.txt"); //如果e盘没有这个文件夹会自动创建
//循环读取
int n;
while ((n=fis.read())!=-1){ //文件读取完毕,read返回-1
fos.write((char)n); //读取一个字节就往d盘新建的a.txt文件中写入
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally { //关闭资源
if (fis !=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos !=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
把d盘的a.txt文件复制到E盘跟目录下(java的io实现案例)
最新推荐文章于 2023-08-05 22:09:22 发布