java实现对文件或文件夹的另存为功能
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java实现对文件或文件夹的另存为功能,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1937字,纯文字阅读大概需要3分钟。
内容图文
![java实现对文件或文件夹的另存为功能](/upload/InfoBanner/zyjiaocheng/741/7e78df6f2349414b819b84a76731f617.jpg)
java后台实现对文件或文件夹的另存为功能:
public static void compressFile(String strOldpath,String strNewPath) {
File file = new File(strOldpath);
/* 判断是目录还是文件 */
if (file.isDirectory()) {
//System.out.println("压缩文件夹:" + strOldpath + file.getName());
copyFolder(strOldpath, strNewPath);//对文件夹进行递归另存为
} else {
//System.out.println("压缩文件:" + strOldpath + file.getName());
copyFile(strOldpath, strNewPath); //对单个文件进行另存为
}
}
public static void copyFile(String strOldpath,String strNewPath)
{
try {
File fOldFile = new File(strOldpath);
if (fOldFile.exists())
{
int bytesum = 0;
int byteread = 0;
InputStream inputStream = new FileInputStream(fOldFile);
FileOutputStream fileOutputStream = new FileOutputStream(strNewPath);
byte[] buffer = new byte[1444];
while ( (byteread = inputStream.read(buffer)) != -1)
{
bytesum += byteread; //这一行是记录文件大小的,可以删去
fileOutputStream.write(buffer, 0, byteread);//三个参数,第一个参数是写的内容,
//第二个参数是从什么地方开始写,第三个参数是需要写的大小
}
inputStream.close();
fileOutputStream.close();
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("复制单个文件出错");
e.printStackTrace();
}
}
public static void copyFolder(String strPatientImageOldPath,String strPatientImageNewPath)
{
File fOldFolder = new File(strPatientImageOldPath);//旧文件夹
try {
File fNewFolder = new File(strPatientImageNewPath);//新文件夹
if (!fNewFolder.exists())
{
fNewFolder.mkdirs();//不存在就创建一个文件夹
}
File [] arrFiles = fOldFolder.listFiles();//获取旧文件夹里面所有的文件
for (int i = 0; i < arrFiles.length; i++)
{
//从原来的路径拷贝到现在的路径,拷贝一个文件
if (!arrFiles[i].isDirectory())
{
copyFile(strPatientImageOldPath+"/"+arrFiles[i].getName(), strPatientImageNewPath+"/"+arrFiles[i].getName());
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
内容总结
以上是互联网集市为您收集整理的java实现对文件或文件夹的另存为功能全部内容,希望文章能够帮你解决java实现对文件或文件夹的另存为功能所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。