📄 jahiatools.java
字号:
* * @param decompose The string that you want to convert if <code>isFile</code> * is <code>false</code>. Otherwise, it's the path to * the file you want to read. * @boolean isFile <code>true</code> is the source is a File or * <code>false</code> if the source is a String. * @return Enumeration containing all lines of the string passed in parameter. */ public static Enumeration string2Enumeration( String decompose, boolean isFile ) throws IOException { Vector stringLines = new Vector(); String buffer = ""; BufferedReader buffered; if(isFile) { buffered = new BufferedReader( new FileReader( decompose ) ); } else { buffered = new BufferedReader( new StringReader( decompose ) ); } while((buffer = buffered.readLine()) != null) { if(buffer.trim().length() > 0) { stringLines.add(buffer); } } buffered.close(); return stringLines.elements(); } // end string2Enumeration //------------------------------------------------------------------------- /** * Check if the String passed in parameter is Alpha valid. * * @author FH */ public static boolean isAlphaValid(String name) { if (name == null) { return false; } if (name.length() == 0) { return false; } char[] chars = AUTHORIZED_CHARS; char[] nameBuffer = name.toCharArray(); boolean badCharFound = false; int i = 0; while ((i < nameBuffer.length) && (!badCharFound)) { int j = 0; boolean ok = false; while ((j < chars.length) && (!ok)) { if (chars[j] == nameBuffer[i]) { ok = true; } j++; } badCharFound = (!ok); if (badCharFound) { JahiaConsole.println("JahiaTools.isAlphaValid","--/ Bad character found in ["+name+"] at position "+Integer.toString(i)); } i++; } return (!badCharFound); } // end isAlphaValid //------------------------------------------------------------------------- /** * Write a string in a file. * * @author AK * @param fileName File name. * @param output String output. */ public static void writeStringInFile( String fileName, String output ) { // try to write the file... try { File fileObject = new File( fileName ); FileWriter fileWriter = new FileWriter( fileObject ); fileWriter.write( output ); fileWriter.close(); } catch (IOException ioe) { } } // end writeStringInFile /************************************************************************** * Files Tools * * **************************************************************************/ //------------------------------------------------------------------------- /** * Copy files from String origin to String destination. * * @author AK */ public static void copyFolderContent( String origin, String destination ) throws IOException { File originFolder = new File(origin); File destinationFolder = new File(destination); // create the destination folder if necessary... if(!destinationFolder.exists()) { destinationFolder.mkdirs(); } // copy recursive... if(originFolder.isDirectory()) { File[] filesInThisDirectory = originFolder.listFiles(); StringBuffer destinationFile = null; for(int i=0; i<filesInThisDirectory.length; i++) { String originFile = filesInThisDirectory[i].getPath(); String originFileName = filesInThisDirectory[i].getName(); destinationFile = new StringBuffer(destination); destinationFile.append(File.separator); destinationFile.append(originFileName); if(filesInThisDirectory[i].isFile()) { FileInputStream fileInput = new FileInputStream( originFile ); FileOutputStream fileOutput = new FileOutputStream( destinationFile.toString() ); copyStream( fileInput, fileOutput ); } else { copyFolderContent( originFile, destinationFile.toString() ); } } } } // end copyFiles //------------------------------------------------------------------------- /** * Copy an InputStream to an OutputStream * * @author AK */ public static void copyStream( InputStream inputStream, OutputStream outputStream ) throws IOException { int bufferRead; int bufferSize = 1024; byte[] writeBuffer = new byte[bufferSize]; BufferedOutputStream bufferedStream = new BufferedOutputStream(outputStream, bufferSize); while((bufferRead = inputStream.read(writeBuffer)) != -1) bufferedStream.write(writeBuffer, 0, bufferRead); bufferedStream.flush(); bufferedStream.close(); inputStream.close(); outputStream.flush(); outputStream.close(); } // end copyStream //------------------------------------------------------------------------- public static boolean checkFileExists( String fileName ) { try { File fileObject = new File( fileName ); return fileObject.exists(); } catch (NullPointerException npe) { return false; } } // end checkFileExists //------------------------------------------------------------------------- /** * check if a file or directory exists on disk. The check is case sensitive * * @author NK * @param String the absolute path * @return boolean true if comparison is success */ static public boolean checkFileNameCaseSensitive(String path){ File tmpFile = new File(path); if ( tmpFile != null && ( tmpFile.isFile() || tmpFile.isDirectory() ) ){ String name = tmpFile.getName(); if ( tmpFile.getParentFile() != null ){ File[] files = tmpFile.getParentFile().listFiles(); int nbFiles = files.length; for (int i=0 ; i<nbFiles ; i++){ if ( files[i].getName().equals(name) ){ return true; } } } } return false; } //------------------------------------------------------------------------- /** * delete a file or directory (and all its content) * * @author NK * @param (File) the abstract file object */ public static boolean deleteFile(File f){ return deleteFile(f,false); } //------------------------------------------------------------------------- /** * delete a file or directory ( and all its contains ) * * @author NK * @param (File) the abstract file object * @param (boolean) contentOnly id true, delete only the folder content */ public static boolean deleteFile(File f, boolean contentOnly){ if ( f == null ){ return false; } if ( f.isDirectory() ){ File[] files = f.listFiles(); for ( int i=0 ; i<files.length ; i++ ){ if ( files[i].isFile() ){ files[i].delete(); } else { deleteFile(files[i],false); } } if ( !contentOnly ){ return f.delete(); } } return true; } //------------------------------------------------------------------------- /** * Return the file name but without the extension * * @author NK * @param (String) filename , the complete file name with extension * @param (String) ext , the extension to remove * @return(String) the filename without a gived extension */ public static String removeFileExtension(String filename, String ext){ String name = filename.toLowerCase(); if ( name.endsWith( ext.toLowerCase() ) ){ return ( filename.substring(0,name.lastIndexOf(ext.toLowerCase())) ); } return filename; } //------------------------------------------------------------------------- /** * Method getUniqueDirName * Return a random and unique String that can be used * as directory name * * @return a unique directory name */ public static String getUniqueDirName() { return ( JahiaKeyGen.getKey(10) ); } /************************************************************************** * Sql Tools * * *************************************************************************/ //------------------------------------------------------------------------- /** * Method Quote * Double the quotes in order to be SQL compliant * * @param input String to filter * @return a filtered string */ static public String quote(String input) { if ( input != null ){ StringBuffer sb = new StringBuffer(input); for (int i = 0; i < sb.length(); i++) { char c = sb.charAt(i); if (c == '\'') { sb.insert(i++, '\''); } } return sb.toString(); } return input; } /************************************************************************** * General purposes Tools * * **************************************************************************/ //------------------------------------------------------------------------- /** * Check if a string value is in an array of string * * @param aValue a string value * @param values an array of String * @return true if value found in array * @author NK */ static public boolean inValues(String aValue, String[] values){ if ( values != null ){ for (int i=0 ; i<values.length ; i++){ if (aValue.equals(values[i])){ return true; } } } return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -