首页 / 软件开发 / JAVA / 用java写的一个文件操作类包
用java写的一个文件操作类包2011-02-05 javaeye wakin2003前几天仔细看了看java的I/O操作,呵呵。就写了一个操作文件的类包,功能有创建文件或目录,删除文件或目录,复制文件或目录,移动文件或目录,设置文件或目录属性,查看文件或目录大小。呵呵,功能比较简单,源代码为:创建:Java代码package fileOperation;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author wakin
*
*/
public class Create
{
/**根据字符串生成文件,如果已存在则抛出异常
*
* @param filePath
*/
public void createFile(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
//这里很奇怪,要是不写这两句在windows下就看不见生成的文件。呵呵,希望大家指点一下。
FileOutputStream fos= new FileOutputStream(file);
fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**根据字符串生成文件夹,如果已存在则抛出异常
*
* @param filePath
*/
public void createDir(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
file.mkdirs();
}
}
package fileOperation;
import java.io.File;
import java.io.FileOutputStream;
/**
* @author wakin
*
*/
public class Create
{
/**根据字符串生成文件,如果已存在则抛出异常
*
* @param filePath
*/
public void createFile(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
try {
//这里很奇怪,要是不写这两句在windows下就看不见生成的文件。呵呵,希望大家指点一下。
FileOutputStream fos= new FileOutputStream(file);
fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**根据字符串生成文件夹,如果已存在则抛出异常
*
* @param filePath
*/
public void createDir(String filePath) {
File file = new File(filePath);
if(file.exists())
throw new RuntimeException("File: "+filePath+" is already exist");
file.mkdirs();
}
}