Web项目: Java在安置项目的WebRoot下成立文件夹(附上文件操作类)[Java编程]
本文“Web项目: Java在安置项目的WebRoot下成立文件夹(附上文件操作类)[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
public boolean doTest(){
String path="../webapps/FileTest/reportFiles/aa.jsp";//FileTest为自己的项目名 reportFiles为自己成立的文件夹 aa.jsp为自己成立的文件
boolean isDone = false;
File file = new File(path);
if(file.exists())
throw new RuntimeException("File: "+path+" is already exist");
try {
isDone = file.createNewFile();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return isDone;
}
附文件操作类
package fileOperation;
import java.io.File;
/**
* 查看,改正文件或目录的属性
* @author wakin
*
*/
public class Attribute {
/**
* 查看途径名所表示文件或目录的属性.
* @param fileName 途径名
*/
public void lookAttribute(String fileName) {
boolean canRead;
boolean canWrite;
boolean canExecute;
File file = new File(fileName);
if(!file.exists())
throw new RuntimeException("File:"+fileName+"is not exist");
canRead = file.canRead();
canWrite = file.canWrite();
canExecute = file.canExecute();
System.out.println("Can read:"+canRead+" Can write:"+canWrite+" Can Execute:"+canExecute);
}
/**
* 设置途径名所表示的文件或目录的的属性.?部份功效大概在windows下无效.
* @param fileName 途径名
* @param readable 能否可读
* @param writable 能否可写
* @param executable 能否可履行
* @param ownerOnly 能否用户独享
* @return 属性设置成功,返回true,不然返回false
*/
public boolean setAttribute(
String fileName,
boolean readable,
boolean writable,
boolean executable,
boolean ownerOnly)
{
boolean isDone = false;
File file = new File(fileName);
isDone = file.setReadable(readable, ownerOnly)
&& file.setWritable(writable, ownerOnly)
&& file.setExecutable(executable, ownerOnly);
return isDone;
}
}
package fileOperation;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 复制文件和文件夹工具类,能判断源文件不存在,源文件不可读,目标文件已经存在,
* 目标途径不存在,目标途径不可写等情形
* @author wakin
*
*/
public class Copy
{
/**按照源途径名和目标途径名复制文件.
*
* @param source_name 源途径名
* @param dest_name 目标途径名
* @param type 值为判断假如目标途径存在能否覆盖,1为覆盖旧的文件,2为不覆盖,操作撤消.
* @return 当复制成功时返回1, 当目标文件存在且type值为2时返回2,其他情形返回0
* @throws IOException 发生I/O错误
*/
public int copyFile(
String source_name,
String dest_name,
int type) throws IOException {
int result = 0;
int byte_read;
byte [] buffer;
File source_file = new File(source_name);
File dest_file = new File(dest_name);
FileInputStream source = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
FileOutputStream dest = null;
try {
if(!source_file.exists() || !source_file.isFile()) //不存在
throw new RuntimeException("FileCopy: no such source file:"+source_name);
if(!source_file.canRead()) //不可读
throw new RuntimeException("FileCopy: source file"+source_name
+"is unreadable");
if(dest_file.exists()) {
if(dest_file.isFile()) {
if(type==1) {
dest_file.delete(); //覆盖
result = 1 ;
}
else if(type==2) {
result = 2;
return result; //不覆盖 ,程序返回.
}
}
else
throw new RuntimeException("FileCopy: destination"+dest_name
+"is not a file.");
}
else {
File parentDir = new File(dest_file.getParent()); //得到目录信息
if(!parentDir.exists()) {
throw new RuntimeException("FileCopy: destination"+dest_name
+"directory doesn't exist."); //目录不存在
}
if(!parentDir.canWrite())
throw new RuntimeException("FileCopy: destination"+dest_name
+"is unwriteable."); //目录不可写
}
//开始复制文件
//输入流
source = new FileInputStream(source_file);
bis = new BufferedInputStream(source);
//输出流
dest = new FileOutputStream(dest_file);
bos = new BufferedOutputStream(dest);
buffer = new byte[1024*5];
while((byte_read=bis.read(buffer))!=-1) {
bos.write(buffer, 0, byte_read);
}
result = 1;
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if(source!=null){
bis.close();
source.close();
}
if(dest!=null) {
bos.flush();
bos.close();
dest.flush();
dest.close();
}
}
return result;
}
/**按照源途径名和目标途径名复制目录.
* 复制目录以复制文件为底子,通过递归复制子目录实现.
* @param source_name 源途径名
* @param dest_name 目标途径名
* @param type 值为判断假如目标途径存在能否覆盖,1为覆盖旧的目录,2为不覆盖,操作撤消.
* @return 当复制成功时返回1, 当目标目录存在且type值为2时返回2,其他情形返回0
* @throws IOException 发生I/O错误
*/
public int copyDirectory(
String source_name,
String dest_name,
int type
) throws IOException {
File source_file = new File(source_name);
File dest_file = new File(dest_name);
int result = 0;
Delete del = new Delete(); //用于删除目录文件
if(!source_file.exists()||source_file.isFile()) //不存在
throw new RuntimeException("DirCopy: no such dir"+source_name);
if(!source_file.canRead()) //不可读
throw new RuntimeException("DirCopy: source file"+source_name
+"is unreadable");
if(dest_file.exists()) {
if(type==1) {
del.deleteDir(dest_name); //覆盖
result = 1;
}
if(type==2) {
result = 2; //不覆盖
return result;
}
}
if(!dest_file.exists()) {
new File(dest_name).mkdirs(); //成立目标目录
File[] fileList = source_file.listFiles();
for(int i=0;i<fileList.length;i++){
System.out.println(fileList[i].getName());
if(fileList[i].isFile()){
//用copyFile函数复制文件
this.copyFile(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(),
type);
}
else if(fileList[i].isDirectory()){
//递归
copyDirectory(
source_name+"/"+fileList[i].getName(),
dest_name+"/"+fileList[i].getName(), type);
}
}
result = 1;
}
return result;
}
}
package fileOperation;
import java.io.File;
/**成立一个新的文件或目录
* @author wakin
*
*/
public class Create
{
/**
* 按照途径名成立文件
* @param filePath 途径名
* @return 当成立文件成功后,返回true,不然返回false.
*
*/
public boolean createFile(String filePath) {
boolean isDone = false;
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
isDone = file.createNewFile();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return isDone;
}
/**
* 按照途径名成立目录
* @param filePath 途径名
* @return 当成立目录成功后,返回true,不然返回false.
*/
public boolean createDir(String filePath) {
boolean isDone = false;
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("FileDirectory: "+filePath+" is already exist");
isDone = file.mkdirs();
return isDone;
}
}
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
* 删除一个文件或目录
* @author wakin
*
*/
public class Delete
{
/**
* 删除途径名所代表的文件.
* @param filePath 途径名
* @return 当文件成功删除,返回true,不然返回false.
*/
public boolean deleteFile(String filePath) throws IOException{
File file = new File(filePath);
if(file.exists()) {
file.delete();
//System.out.println(filePath+"文件已删除.");
return true;
}
else {
//System.out.println("逻辑错误:"+filePath+"文件不存在.");
return false;
}
}
/**
* 删除途径名所代表的目录
* @param filePath 途径名
* @return 当目录成功删除,返回true,不然返回false.
*/
public boolean deleteDir(String filePath) throws IOException{
boolean isDone = false;
File file = new File(filePath);
//判断是文件还是目录
if(file.exists()&&file.isDirectory()){
if(file.listFiles().length==0){
file.delete();
isDone = true;
}
else {
File [] delFiles = file.listFiles();
for(int i=0;i<delFiles.length;i++){
if(delFiles[i].isDirectory()){
deleteDir(delFiles[i].getAbsolutePath()); //递归调用deleteDir函数
}
else {
delFiles[i].delete();
}
}
}
//删除最后剩下的目录名.
deleteDir(filePath);
isDone = true;
}
else
return false;
return isDone;
}
}
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
* 移动文件/目录,操纵Delete类和Copy类来实现.
* @author wakin
*
*/
public class Move {
/**
* 操纵Copy类的函数和Delete类来完成移动文件/目录的操作.
* @param source_name 源途径名
* @param dest_name 目标途径名
* @param type type 值为判断假如目标途径存在能否覆盖,1为覆盖旧的文件/目录,2为不覆盖操作撤消.
* @return 当移动成功后返回1,当目标目录存在且type值为2时返回2,其他情形返回0
* @throws IOException 发生I/O错误
*/
public int move(String source_name,String dest_name,int type) throws IOException{
int result = 0;
Copy copy = new Copy();
Delete delete = new Delete();
File source_file = new File(source_name);
//File dest_file = new File(dest_name);
if(!source_file.exists())
throw new RuntimeException("FileMove: no such source file:"+source_name);
if(source_file.isFile()){
result = copy.copyFile(source_name, dest_name, type); //调用Copy类的copyFile函数
if(result ==1)
delete.deleteFile(source_name); //调用Delete类的deleteFile函数删除源文件
}
else {
result = copy.copyDirectory(source_name, dest_name, type);//调用Copy类的copyDirectory函数
if(result == 1)
delete.deleteDir(source_name); //调用Delete类的deleteDir函数删除源目录
}
return result;
}
}
package fileOperation;
import java.io.File;
import java.io.IOException;
/**
* 计算文件或目录的空间大小.
* @author wakin
*
*/
public class Size {
/**
* 计算途径名代表的文件或目录所占空间的大小.
* @param fileName 途径名
* @return 所占字节数
* @throws IOException 发生I/O错误
*/
public static long getSize(String fileName) throws IOException {
long result = 0;
File file = new File(fileName);
if(!file.exists())
throw new RuntimeException("No such source file:"+fileName);
if(file.isFile()){
return file.length();
}
else {
String [] FileList = file.list();
for(int i =0;i<FileList.length;i++) {
result += getSize(fileName+"/"+FileList[i]); //迭代
}
}
return result;
}
}
以上是“Web项目: Java在安置项目的WebRoot下成立文件夹(附上文件操作类)[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |