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();
}
}
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();
}
}
相关文章推荐
- Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
- blackberry 模拟器 上网设置(Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file)
- Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
- Date&Time in java codes&XML
- Android错误:项目导入时,出现Exception in thread "main" java.lang.NoClassDef、Could not write file 。。。.classpath
- java.lang.ExceptionInInitializerError BeanCreationException: Error creating bean with name 'TestServ
- java.lang.ClassFormatError Duplicate field name&signature in class file XXXXXX【转】
- java.lang.ClassFormatError Duplicate field name&signature in class file XXXXXX
- Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.poifs.filesystem.POIFSFileSys
- Hive连接MYSQL数据OK,查询出现in thread "Thread-18" java.lang.IllegalArgumentException:Does not contain a vali
- 异常备忘:java.lang.UnsupportedClassVersionError: Bad version number in .class file
- dpkg: error: parsing file '/var/lib/dpkg/updates/0002' near line 0:newline in field name `#padding'
- The absolute uri: http://java.sun.com/jstl/core cannot be resolved in either web.xml or the jar file
- Exception in thread "main" java.lang.NoClassDefFoundError错误信息
- Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
- adpatch failed with error: "Missing file format id in file"
- Exception in thread "HouseKeeper" java.lang.NullPointerException
- 《thinking in java》学习笔记18
- Error creating bean with name 'txManager' defined in file
- java.lang.UnsupportedClassVersionError: Bad version number in .class file