Java tutorial
/* * @(#)FileUtils.java 1.0 2013-03-26 * * Copyright (c) 2007-2013 Shanghai BSOFT IT, Co., Ltd. * All rights reserved. * * This software is the confidential and proprietary information of * Shanghai BSFOT IT Co., Ltd. ("Confidential Information"). * You shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement you * entered into with BSOFT. */ package com.bsoft.baseframe.baseframe_utils.beanUtils; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; /** * * ? * * @version 1.0 2013-3-26 * @author caie * @history * 2013-4-7 caie writeDataboolean * getFilePath?tablename * */ public class FileUtils { /** * ??bean? * @param tableName ?? * @param pack * @return ? */ public static String getFilePath(String pack) { String proPath = System.getProperty("user.dir").replaceAll("\\\\", "/"); String spack = pack.replaceAll("\\.", "/"); return proPath + "/src/main/java/" + spack + "/"; } /** * ?? * 2013-04-07 caie * @param src * @param pack ?? * @return */ public static String getFilePath(String src, String pack) { String proPath = System.getProperty("user.dir").replaceAll("\\\\", "/"); String spack = pack.replaceAll("\\.", "/"); return proPath + "/" + src + "/main/java" + spack + "/"; } /** * ?XMLPath * 2013-04-07 caie * @param src * @param pack ?? * @return */ public static String getXMLFilePath(String src, String pack) { String proPath = System.getProperty("user.dir").replaceAll("\\\\", "/"); String spack = pack.replaceAll("\\.", "/"); return proPath + "/" + src + "/" + spack + "/"; } /** * ?? * @param tableName ?? * @param pack * @return */ public static String getFilePathName(String tableName, String pack) { return getFilePath(pack) + suffixClassName(tableName); } /** * ???.java * @param tableName ?? * @return .java?? */ public static String suffixClassName(String tableName) { String suffixClassName = getClassName(tableName); return suffixClassName + ".java"; } /** * ?bean?? * @param tableName * @deprecated * @return */ public static String getBeanName(String tableName) { int index = tableName.indexOf("_"); String className = tableName; if (index >= 0) { className = tableName.substring(index + 1); } className = className.substring(0, 1).toUpperCase() + className.substring(1); return className; } /** * * @param tn ?? * @return ?? */ public static String getClassName(String tn) { int index = tn.indexOf("_"); String temp = ""; if (index < 0) { return tn.substring(0, 1).toUpperCase() + tn.substring(1); } else { temp = tn.substring(0, index) + tn.substring(index + 1, index + 2).toUpperCase() + tn.substring(index + 2); return getClassName(temp); } } /** * bean * @param filePath * @return ?truefalse */ private static boolean createFile(String filePath) { File f = new File(filePath); int index = filePath.indexOf("."); if (index >= 0) { String dirPath = filePath.substring(0, filePath.lastIndexOf("/")); File dirFile = new File(dirPath); if (!dirFile.exists()) { dirFile.mkdirs(); } if (f.exists()) { f.delete(); } try { f.createNewFile(); } catch (IOException e) { e.printStackTrace(); return false; } } else { if (!f.exists()) { f.mkdirs(); } } return true; } /** * * @param beanCode * @param filePath */ public static boolean writeData(String beanCode, String filePath) { OutputStreamWriter osw = null; BufferedWriter bw = null; FileOutputStream fos = null; createFile(filePath); File f = new File(filePath); try { fos = new FileOutputStream(f); osw = new OutputStreamWriter(fos, "UTF-8"); bw = new BufferedWriter(osw); bw.write(beanCode); bw.flush(); fos.close(); osw.close(); bw.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * ?? * @param str * @return true ?false */ public static boolean isEmpty(String str) { if ("".equals(str)) { return true; } return false; } /** * ?XML? * @param document * @param file * @throws IOException */ public static boolean wrieteXML2Doc(Document document, File file) { boolean isCreate = false; try { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();// } if (!file.exists()) { isCreate = true; file.createNewFile();// java testData.java } else { isCreate = false; } //?XML?? if (isCreate) { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); // //document XMLWriter writer = new XMLWriter(new FileWriter(file), format); writer.write(document); writer.close(); } return true; } catch (Exception e) { return false; } } /** * ?ServiceServiceImpl * @param path * @param isNextWriter */ public static boolean writerJavaFile(String contentStr, String path, boolean isNextWriter) { boolean isCreate = false; try { File file = new File(path); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs();// } if (!file.exists()) { isCreate = true; file.createNewFile();// java testData.java } else { isCreate = false; } if (isCreate) { // Java FileWriter fileWriter = new FileWriter(path, isNextWriter); BufferedWriter bw = new BufferedWriter(fileWriter); bw.newLine(); bw.write(contentStr); fileWriter.flush(); bw.close(); fileWriter.close(); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * * @param args */ public static void main(String[] args) { String path = "E:/java/es/Test.java"; FileUtils.createFile(path); } }