
public class Copy {
public static void main(String args[]){
if(args.length!=2){
System.out.println("参数不正确,无法袭森弯完成复制!正确用法春禅:")
System.out.println("java Copy 源文件名 目的文件名")
System.exit(0)
}
copyFile(args[0],args[1])
}
public static void copyFile(String src,String obj){
FileInputStream fis=null
FileOutputStream fos=null
try{
fis=new FileInputStream(src)
fos=new FileOutputStream(obj)
}catch(FileNotFoundException e){
System.out.println("文件不存在,请检查拍闷您的输入:")
}catch(IOException e){
e.printStackTrace()
}
try{
int b
while((b=fis.read())!=-1){
fos.write(b)
}
fos.flush()
System.out.println("文件复制成功!")
}catch(IOException e){
System.out.println("文件写入错误!")
}
}
}
最简单的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)
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)