您的位置:首页 > Web前端

File transfer & File rename & Folder creation in java codes

2012-07-25 11:57 435 查看
public static void tranferFile(String fromPath, String toPath,String file) {

InputStream inStream = null;

OutputStream outStream = null;

String name = "";

if(file.indexOf(".")!=-1)

name = file.substring(0,file.indexOf("."));

File path = new File(toPath);

if (!path.exists()){

path.mkdirs();

}

try {

File afile = new File(fromPath + file);

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHMM");

String destFile = name+"."+formatter.format(new java.util.Date());

File bfile = new File(toPath + destFile);

inStream = new FileInputStream(afile);

outStream = new FileOutputStream(bfile);

byte[] buffer = new byte[1024];

int length;

if (afile.exists())

// copy the file content in bytes

while ((length = inStream.read(buffer)) > 0) {

outStream.write(buffer, 0, length);

}

inStream.close();

outStream.close();

String finalFile = name+".zip";

boolean success = bfile.renameTo(new File(toPath + finalFile) );

} catch (IOException e) {

e.printStackTrace();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐