java实现对文件或文件夹的另存为功能

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实现文件或文件夹另存为)