java用字节流实现对文件的复制,移动,重命名等 *** 作的程序

java用字节流实现对文件的复制,移动,重命名等 *** 作的程序,第1张

java.io.FileInputStream

java.io.FileOutputStream

复制就是保留原羡旅启文件

移动、重命名就是创建新的、删除旧的 (流兄如关闭后,使用java.io.File可以删除)镇指

最简单的io流问题,不用什么高手,

我给你写个方法,参数是2个字符串,第一个写原文件的全路径,第二个写目标文件的全路进。 你试试吧

public void copy(String fromFilePath, String toFilePath) {

try {

FileInputStream fis = new FileInputStream(fromFilePath)

FileOutputStream fos = new FileOutputStream(toFilePath)

byte[] b = new byte[100]

try {

while (fis.read(b) != (-1)) {

fos.write(b)

}

if (fis != null) {

fis.close()

fis = null

}

if (fos != null) {

fos.flush()

fos.close()

fos = null

}

} catch (IOException e) {

System.out.println("io异常")

}

} catch (FileNotFoundException e) {

System.out.println("源文件不存在")

}

public static void main(String[] args) {

//自己把路径补齐,别忘了!!!!!!!!!!物谨!!!!!!

String fromFilePath=" " // 源文件的全路罩烂基径。 比方"d://myphoto//nihao.mp3"

String toFilePath=" "//目标文件的全路劲。 如果不存在会自动建立,如存在则在文件尾历顷继续添加

new CopyTest().copy(fromFilePath, toFilePath)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12333086.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存