📄 filetools.java
字号:
public static long readLong_stdin(String msg, long dft) throws IOException { if (msg == null) { msg = ""; } Print.sysPrintln(msg + " [Long: default='" + dft + "'] "); for (;;) { Print.sysPrint("?"); String line = FileTools.readLine_stdin().trim(); if (line.equals("")) { return dft; } else if (!Character.isDigit(line.charAt(0)) && (line.charAt(0) != '-')) { Print.sysPrint("Long required, please re-enter] "); continue; } return StringTools.parseLong(line, dft); } } /** *** Prints a message, and reads a double value from stdin *** @param msg The message to print *** @param dft The default double value returned, if no value was entered *** @return The double value read from stdin **/ public static double readDouble_stdin(String msg, double dft) throws IOException { if (msg == null) { msg = ""; } Print.sysPrintln(msg + " [Double: default='" + dft + "'] "); for (;;) { Print.sysPrint("?"); String line = FileTools.readLine_stdin().trim(); if (line.equals("")) { return dft; } else if (!Character.isDigit(line.charAt(0)) && (line.charAt(0) != '-') && (line.charAt(0) != '.')) { Print.sysPrint("Double required, please re-enter] "); continue; } return StringTools.parseDouble(line, dft); } } // ------------------------------------------------------------------------ /** *** Writes a byte array to the specified file *** @param data The byte array to write to the file *** @param file The file to which the byte array is written *** @return True if the bytes were successfully written to the file *** @throws IOException if an error occurred. **/ public static boolean writeFile(byte data[], File file) throws IOException { return FileTools.writeFile(data, file, false); } /** *** Writes a byte array to the specified file *** @param data The byte array to write to the file *** @param file The file to which the byte array is written *** @param append True to append the bytes to the file, false to overwrite. *** @return True if the bytes were successfully written to the file *** @throws IOException if an error occurred. **/ public static boolean writeFile(byte data[], File file, boolean append) throws IOException { if ((data != null) && (file != null)) { FileOutputStream fos = null; try { fos = new FileOutputStream(file, append); fos.write(data, 0, data.length); return true; } finally { try { fos.close(); } catch (Throwable t) {/* ignore */} } } return false; } /** *** Writes a String to the specified file in "ISO-8859-1" character encoding.<br> *** Unicode characters are escaped using the '\u0000' format. *** @param dataStr The String to write to the file *** @param file The file to which the byte array is written *** @return True if the String was successfully written to the file *** @throws IOException if an error occurred. **/ public static boolean writeEscapedUnicode(String dataStr, File file) throws IOException { boolean append = false; if ((dataStr != null) && (file != null)) { FileOutputStream fos = new FileOutputStream(file, append); BufferedWriter fbw = null; try { fbw = new BufferedWriter(new OutputStreamWriter(fos, "8859_1")); int len = dataStr.length(); for (int i = 0; i < len; i++) { char ch = dataStr.charAt(i); if ((ch == '\n') || (ch == '\r')) { fbw.write(ch); } else if ((ch == '\t') || (ch == '\f')) { fbw.write(ch); } else if ((ch < 0x0020) || (ch > 0x007e)) { fbw.write('\\'); fbw.write('u'); fbw.write(StringTools.hexNybble((ch >> 12) & 0xF)); fbw.write(StringTools.hexNybble((ch >> 8) & 0xF)); fbw.write(StringTools.hexNybble((ch >> 4) & 0xF)); fbw.write(StringTools.hexNybble( ch & 0xF)); } else { fbw.write(ch); } } return true; } finally { try { fbw.close(); } catch (Throwable t) {/* ignore */} } } return false; } // ------------------------------------------------------------------------ /** *** Gets the extension characters from the specified file name *** @param filePath The file name *** @return The extension characters **/ public static String getExtension(String filePath) { if (filePath != null) { return getExtension(new File(filePath)); } return ""; } /** *** Gets the extension characters from the specified file *** @param file The file *** @return The extension characters **/ public static String getExtension(File file) { if (file != null) { String fileName = file.getName(); int p = fileName.indexOf("."); if ((p >= 0) && (p < (fileName.length() - 1))) { return fileName.substring(p + 1); } } return ""; } /** *** Returns true if the specified file path has an extension which matches one of the *** extensions listed in the specified String array *** @param filePath The file path/name *** @param extn An array of file extensions *** @return True if teh specified file path has a matching exention **/ public static boolean hasExtension(String filePath, String extn[]) { if (filePath != null) { return hasExtension(new File(filePath), extn); } return false; } /** *** Returns true if the specified file has an extension which matches one of the *** extensions listed in the specified String array *** @param file The file *** @param extn An array of file extensions *** @return True if teh specified file has a matching exention **/ public static boolean hasExtension(File file, String extn[]) { if ((file != null) && (extn != null)) { String e = getExtension(file); for (int i = 0; i < extn.length; i++) { if (e.equalsIgnoreCase(extn[i])) { return true; } } } return false; } /** *** Removes the extension from the specified file path *** @param filePath The file path from which the extension will be removed *** @return The file path with the extension removed **/ public static String removeExtension(String filePath) { if (filePath != null) { return removeExtension(new File(filePath)); } return filePath; } /** *** Removes the extension from the specified file *** @param file The file from which the extension will be removed *** @return The file path with the extension removed **/ public static String removeExtension(File file) { if (file != null) { String fileName = file.getName(); int p = fileName.indexOf("."); if (p > 0) { // '.' in column 0 not allowed file = new File(file.getParentFile(), fileName.substring(0, p)); } return file.getPath(); } else { return null; } } // ------------------------------------------------------------------------ /** *** Returns true if the specified character is a file separator *** @param ch The character to test *** @return True if the specified character is a file separator **/ public static boolean isFileSeparatorChar(char ch) { if (ch == File.separatorChar) { // simple test, matches Java's understanding of a file path separator return true; } else if (OSTools.isWindows() && (ch == '/')) { // '/' can be used as a file path separator on Windows return true; } else { // not a file path separator character return false; } } /** *** Returns true if the specified String contains a file separator *** @param fn The String file path *** @return True if the file String contains a file separator **/ public static boolean hasFileSeparator(String fn) { if (fn == null) { // no string, no file separator return false; } else if (fn.indexOf(File.separator) >= 0) { // simple test, matches Java's understanding of a file path separator return true; } else if (OSTools.isWindows() && (fn.indexOf('/') >= 0)) { // '/' can be used as a file path separator on Windows return true; } else { // no file path separator found return false; } } // ------------------------------------------------------------------------ private static final String ARG_FILE[] = new String[] { "file" }; // file path private static final String ARG_HEX[] = new String[] { "hex" }; // boolean private static final String ARG_WIDTH[] = new String[] { "width", "w" }; // int private static final String ARG_DUMP[] = new String[] { "dump" }; // boolean private static final String ARG_PACK[] = new String[] { "pack" }; // boolean private static final String ARG_UNI_ENCODE[] = new String[] { "ue", "uniencode" }; // boolean private static final String ARG_UNI_DECODE[] = new String[] { "ud", "unidecode" }; // boolean /** *** Display usage **/ private static void usage() { Print.sysPrintln("Usage:"); Print.sysPrintln(" java ... " + FileTools.class.getName() + " {options}"); Print.sysPrintln("Options:"); Print.sysPrintln(" -file=<file> File path"); Print.sysPrintln(" -dump Print hex dump"); Print.sysPrintln(" -unidecode Decode unicode escaped file (output to stdout)"); System.exit(1); } /** *** Debug/Testing entry point *** @param argv The COmmand-line args **/ public static void main(String argv[]) throws Throwable { RTConfig.setCommandLineArgs(argv); File file = RTConfig.getFile(ARG_FILE,null); if (file == null) { usage(); } byte data[] = FileTools.readFile(file); /* parse hex */ if (RTConfig.getBoolean(ARG_HEX,false)) { String hexStr = new String(data); data = StringTools.parseHex(hexStr, data); } /* hex dump */ if (RTConfig.getBoolean(ARG_DUMP,false)) { System.out.println("Size " + ((data!=null)?data.length:-1)); int width = RTConfig.getInt(ARG_WIDTH,16); System.out.println(StringTools.formatHexString(data,width)); System.exit(0); } /* hex pack */ if (RTConfig.getBoolean(ARG_PACK,false)) { System.out.println("Size " + ((data!=null)?data.length:-1)); System.out.println(StringTools.toHexString(data)); System.exit(0); } /* unicode encode */ if (RTConfig.getBoolean(ARG_UNI_DECODE,false)) { String dataStr = StringTools.unescapeUnicode(StringTools.toStringValue(data)); Print.sysPrintln(dataStr); System.exit(0); } /* done */ usage(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -