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);}}