File操作类


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

/**
*
* @author
* @since
*/
public class FileUtil {

public static List<File> getAllFile(String root){
List list=new ArrayList();
getAllFile(list,root);
return list;
}

private static void getAllFile(List<File> list, String root) {
File f = new File(root);
if (f.isDirectory()) {
File[] fList = f.listFiles();
for (int j = 0; j < fList.length; j++) {
if (fList[j].isDirectory()) {
getAllFile(list, fList[j].getPath());
}
if (fList[j].isFile()) {
list.add(fList[j]);
}
}
}else{
list.add(f);
}
}

public static String readFile(String filePath, String charsetname) {
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
StringBuffer buffer = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream, charsetname));
line = reader.readLine();
while (line != null) {
buffer.append(line);
buffer.append("\n");
line = reader.readLine();
}
String text= buffer.toString();
reader.close();
return text;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}

public static String readFile(String filePath) {
return readFile(filePath, "UTF-8");
}

/**
* 生成文件
*
* @throws IOException
*/
public static String createClass(String projectPath, String packageName, String className, String content)
throws IOException {
String directory = projectPath + "/src/" + packageName.replaceAll("\\.", "/") + "/";
File directoryFile = new File(directory);
directoryFile.mkdirs();
String fileName = directory + "/" + className + ".java";
return createFile(fileName, content);
}

public static String createFile(String fileName, String content) {
try {
File file = new File(fileName);

String dir=getDir(fileName);
File dirFile=new File(dir);
dirFile.mkdirs();

OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
BufferedWriter bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write(content);
bufferedWriter.flush();
bufferedWriter.close();
return fileName;

} catch (Exception ex) {
throw new RuntimeException(ex);
}

}

public static String getName(String fileName) {
int start = fileName.lastIndexOf('\\');
if (start == -1) {
start = fileName.lastIndexOf('/');
}
int end = fileName.lastIndexOf('.');
return fileName.substring(start + 1, end);
}

public static String getFullName(String fileName) {
int start = fileName.lastIndexOf('\\');
return fileName.substring(start + 1);
}

public static String readClassPathFile(String fileName) {
String path = FileUtil.class.getResource("/").getPath() + "/" + fileName;
return readFile(path);

}

public static String getDir(String fileName){
int index=fileName.lastIndexOf("/");
if(index==-1){
index=fileName.lastIndexOf("\\");
}
return fileName.substring(0,index);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值