java 操作 通用类_JAVA实现对文件操作的通用类

这篇博客介绍了Java编程中的FileRobot类,涵盖了文件和目录的创建、写入、读取、检查内容、删除以及文件夹操作。核心方法包括writeFile、createAndDeleteFile、createAndDeleteFolder等,适用于处理文件系统的各种任务。
摘要由CSDN通过智能技术生成

/**

* Script Name : FileRobot

* Description : The operation on file

* Type : Java

* Generated : 2006-5-26 13:49:00

* Original Host : WinNT Version 5.1 Build 2600 (S)

*

* @since 2006/05/26

* @author Jaytse

*/

public class FileRobot {

/**

* Write some info to a file

*

* @param filePath

*            the filepath of the file

* @param fileName

*            the name of the file

* @param contents

*            the information wanted to writed in

* @throws IOException

*/

public static void writeFile(String filePath, String fileName,

String[] contents) throws IOException {

FileWriter fw = new FileWriter(filePath + fileName);

PrintWriter out = new PrintWriter(fw);

for (int i = 0; i < contents.length; i++) {

out.write(contents[i]);

out.println();

out.flush();

}

fw.close();

out.close();

}

/**

* Write some info to a file

*

* @param filePath

*            the filepath of the file

* @param fileName

*            the name of the file

* @param content

*            the information wanted to writed in

* @throws IOException

*/

public static void writeFile(String filePath, String fileName,

String content) throws IOException {

String[] contents = new String[1];

contents[0] = content;

writeFile(filePath, fileName, contents);

}

/**

* Create and Delete a file

*

* @param filePath

*            the path of file

* @param fileName

*            the name of file

* @return if operation success, return true

* @throws IOException

*/

public static boolean createAndDeleteFile(String filePath, String fileName)

throws IOException {

boolean result = false;

File file = new File(filePath, fileName);

if (file.exists()) {

file.delete();

result = true;

System.out.println("File has been Delete!");

} else {

file.createNewFile();

result = true;

System.out.println("File has been Create!");

}

return result;

}

/**

* Create and Delete a folder

*

* @param folderName

*            the folder name

* @param filePath

*            the fold path

* @return if operation success, return true

*/

public static boolean createAndDeleteFolder(String folderName,

String filePath) {

boolean result = false;

try {

File file = new File(filePath + folderName);

if (file.exists()) {

file.delete();

System.out.println("Folder exists and it has been delete!");

result = true;

} else {

file.mkdir();

System.out

.println("Folder does not exist and it has been created!");

result = true;

}

} catch (Exception ex) {

result = false;

System.out.println("CreateAndDeleteFolder is error:" + ex);

}

return result;

}

/**

* Create a file with its name

* @param fileName the file name

*/

public static void makeFile(String fileName) {

String dir = fileName.substring(0, fileName.lastIndexOf("//"));

String name = fileName.substring(fileName.lastIndexOf("//") + 1,

fileName.length());

makeFile(dir, name);

}

/**

* Create a file with its name, and its directory name

* @param dirName the directory name

* @param fileName the file name

*/

public static void makeFile(String dirName, String fileName) {

try {

Stack stack = new Stack();

String old = dirName;

String hhh = old;

while (hhh.indexOf("//") != -1) {

if (!hhh.equals(old)) {

stack.push(hhh);

}

stack.push(hhh.substring(hhh.lastIndexOf("//") + 1));

hhh = hhh.substring(0, hhh.lastIndexOf("//"));

}

stack.push(hhh);

int size = stack.size() / 2;

for (int i = 0; i < size; i++) {

File file = new File((String) stack.pop() + "//"

+ (String) stack.pop());

file.mkdir();

}

File file = new File(dirName + "//" + fileName);

file.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* Print folder and file list at the filepath

*

* @param filePath

*            the destinated filepath

*/

public static void readFolderByFile(String filePath) {

File file = new File(filePath);

File[] tempFile = file.listFiles();

for (int i = 0; i < tempFile.length; i++) {

if (tempFile[i].isFile()) {

System.out.println("File : " + tempFile[i].getName());

}

if (tempFile[i].isDirectory()) {

System.out.println("Directory : " + tempFile[i].getName());

}

}

}

/**

* chech whether the file content is empty

*

* @param filePath

*            the path of the file

* @param fileName

*            the name of the file

* @return when the content is empty, return true

* @throws IOException

*/

public static boolean fileIsNull(String filePath, String fileName)

throws IOException {

boolean result = false;

FileReader fr = new FileReader(filePath + fileName);

if (fr.read() == -1) {

result = true;

System.out.println(fileName + " has no data!");

} else {

System.out.println(fileName + " has some data!");

}

fr.close();

return result;

}

/**

* Get the content of the file

*

* @param filePath

*            the path of the file

* @param fileName

*            the name of the file

* @return the content of the file

* @throws IOException

*/

public static String readAllFile(String filePath, String fileName)

throws IOException {

StringBuffer buffer = new StringBuffer();

FileReader fr = new FileReader(filePath + fileName);

int count = fr.read();

while (count != -1) {

buffer.append((char) count);

count = fr.read();

if (count == 13) {

fr.skip(1);

}

}

fr.close();

return buffer.toString();

}

/**

* Get the content of the file

*

* @param filePath

*            the path of the file

* @param fileName

*            the name of the file

* @return the file content

* @throws IOException

*/

public static String readLineFile(String filePath, String fileName)

throws IOException {

return readLineFile(filePath + fileName);

}

/**

* Get the content of the file

* @param fileFullName the file name

* @return the file content

* @throws IOException

*/

public static String readLineFile(String fileFullName) throws IOException {

StringBuffer buffer = new StringBuffer();

FileReader fr = new FileReader(fileFullName);

BufferedReader br = new BufferedReader(fr);

String line = br.readLine();

while (line != null) {

buffer.append(line);

line = br.readLine();

}

br.close();

fr.close();

return buffer.toString();

}

/**

* Get files for process

* @param file the directory name

* @param type the file type

* @return the sub-directory and the files

*/

public static File[] getFilesWithFilter(File file, String type){

File[] list = file.listFiles();

Vector v = new Vector();

for (int i = 0; i < list.length; i++) {

if(list[i].isDirectory()){

if(checkDirFile(list[i], type)){

v.addElement(list[i]);

}

}else{

if(list[i].getName().endsWith("." + type)){

v.addElement(list[i]);

}

}

}

File[] Result = new File[v.size()];

for (int i = 0; i < Result.length; i++) {

Result[i] = (File) v.elementAt(i);

}

return Result;

}

/**

* Check whether a dir contains a file

* @param file directory name

* @param type file name

* @return true if yes

*/

public static boolean checkDirFile(File file, String type){

boolean bResult = false;

if(file.isDirectory()){

File[] list = file.listFiles();

for(int i = 0 ; i

if(list[i].isDirectory()){

bResult |= checkDirFile(list[i], type);

}else{

bResult |= list[i].getName().endsWith("." + type);

}

}

}

return bResult;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值