📄 filetools.java
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Description:// This class provides many File based utilities// ----------------------------------------------------------------------------// Change History:// 2006/03/26 Martin D. Flynn// Initial release// 2006/06/30 Martin D. Flynn// -Repackaged// 2008/05/14 Martin D. Flynn// -Added method 'writeEscapedUnicode'// ----------------------------------------------------------------------------package org.opengts.util;import java.io.*;/***** File handling tools**/public class FileTools{ // ------------------------------------------------------------------------ /** *** Copies bytes from one stream to another *** @param input The InputStream *** @param output The OutputStream *** @return The number of bytes copied **/ public static int copyStreams(InputStream input, OutputStream output) throws IOException { return FileTools.copyStreams(input, output, -1); } /** *** Copies bytes from one stream to another *** @param input The InputStream *** @param output The OutputStream *** @param maxLen The maximum number of bytes to copy *** @return The number of bytes copied **/ public static int copyStreams(InputStream input, OutputStream output, int maxLen) throws IOException { /* copy nothing? */ if (maxLen == 0) { return 0; } /* copy bytes */ int length = 0; // count of bytes copied byte tmpBuff[] = new byte[10 * 1024]; // 10K blocks while (true) { /* read length */ int readLen; if (maxLen >= 0) { readLen = maxLen - length; if (readLen == 0) { break; // done reading } else if (readLen > tmpBuff.length) { readLen = tmpBuff.length; // max block size } } else { readLen = tmpBuff.length; } /* read input stream */ int cnt = input.read(tmpBuff, 0, readLen); /* copy to output stream */ if (cnt < 0) { if ((maxLen >= 0) && (length != maxLen)) { Print.logError("Copy size mismatch: " + maxLen + " => " + length); } break; } else if (cnt > 0) { output.write(tmpBuff, 0, cnt); length += cnt; if ((maxLen >= 0) && (length >= maxLen)) { break; // per 'maxLen', done copying } } else { //Print.logDebug("Read 0 bytes ... continuing"); } } output.flush(); /* return number of bytes copied */ return length; } // ------------------------------------------------------------------------ /** *** Opens the specified file for reading *** @param file The path of the file to open *** @return The opened InputStream **/ public static InputStream openInputFile(String file) { if ((file != null) && !file.equals("")) { return FileTools.openInputFile(new File(file)); } else { return null; } } /** *** Opens the specified file for reading *** @param file The file to open *** @return The opened InputStream **/ public static InputStream openInputFile(File file) { try { return new FileInputStream(file); } catch (IOException ioe) { Print.logError("Unable to open file: " + file + " [" + ioe + "]"); return null; } } // ------------------------------------------------------------------------ /** *** Closes the specified InputStream *** @param in The InputStream to close **/ public static void closeStream(InputStream in) { if (in != null) { try { in.close(); } catch (IOException ioe) { //Print.logError("Unable to close stream: " + ioe); } } } /** *** Closes the specified OutputStream *** @param out The OutputStream to close **/ public static void closeStream(OutputStream out) { if (out != null) { try { out.close(); } catch (IOException ioe) { //Print.logError("Unable to close stream: " + ioe); } } } // ------------------------------------------------------------------------ /** *** Returns an array of bytes read from the specified InputStream *** @param input The InputStream *** @return The array of bytes read from the InputStream **/ public static byte[] readStream(InputStream input) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); copyStreams(input, output); return output.toByteArray(); } // ------------------------------------------------------------------------ /** *** Writes a String to the specified OutputStream *** @param output The OutputStream *** @param dataStr The String to write to the OutputStream **/ public static void writeStream(OutputStream output, String dataStr) throws IOException { byte data[] = dataStr.getBytes(); output.write(data, 0, data.length); } // ------------------------------------------------------------------------ /** *** Returns an array of bytes read from the specified file *** @param file The file path from which the byte array is read *** @return The byte array read from the specified file **/ public static byte[] readFile(String file) { if ((file != null) && !file.equals("")) { return FileTools.readFile(new File(file)); } else { return null; } } /** *** Returns an array of bytes read from the specified file *** @param file The file from which the byte array is read *** @return The byte array read from the specified file **/ public static byte[] readFile(File file) { if (file == null) { return null; } else if (!file.exists()) { Print.logError("File does not exist: " + file); return null; } else { FileInputStream fis = null; try { fis = new FileInputStream(file); return readStream(fis); } catch (IOException ioe) { Print.logError("Unable to read file: " + file + " [" + ioe + "]"); } finally { if (fis != null) { try { fis.close(); } catch (IOException ioe) {/*ignore*/} } } return null; } } // ------------------------------------------------------------------------ /** *** Reads a single line of characters from the specified InputStream, terminated by *** either a newline (\n) or carriage-return (\r) *** @param input The InputStream *** @return The line read from the InputStream **/ public static String readLine(InputStream input) throws IOException { StringBuffer sb = new StringBuffer(); while (true) { int ch = input.read(); if (ch < 0) { // eof throw new EOFException("End of InputStream"); } else if ((ch == '\r') || (ch == '\n')) { return sb.toString(); } sb.append((char)ch); } } /** *** Reads a single line of characters from stdin, terminated by *** either a newline (\n) or carriage-return (\r) *** @return The line read from stdin **/ public static String readLine_stdin() throws IOException { while (System.in.available() > 0) { System.in.read(); } return FileTools.readLine(System.in); } /** *** Prints a message, and reads a line of text from stdin *** @param msg The message to print *** @param dft The default String returned, if no text was entered *** @return The line of text read from stdin **/ public static String readString_stdin(String msg, String dft) throws IOException { if (msg == null) { msg = ""; } Print.sysPrintln(msg + " [String: default='" + dft + "'] "); for (;;) { Print.sysPrint("?"); String line = FileTools.readLine_stdin(); if (line.equals("")) { if (dft != null) { return dft; } else { // if there is no default, a non-empty String is required Print.sysPrint("String required, please re-enter] "); continue; } } return line; } } /** *** Prints a message, and reads a boolean value from stdin *** @param msg The message to print *** @param dft The default boolean value returned, if no value was entered *** @return The boolean value read from stdin **/ public static boolean readBoolean_stdin(String msg, boolean dft) throws IOException { if (msg == null) { msg = ""; } Print.sysPrintln(msg + " [Boolean: default='" + dft + "'] "); for (;;) { Print.sysPrint("?"); String line = FileTools.readLine_stdin().trim(); if (line.equals("")) { return dft; } else if (!StringTools.isBoolean(line,true)) { Print.sysPrint("Boolean required, please re-enter] "); continue; } return StringTools.parseBoolean(line, dft); } } /** *** Prints a message, and reads a long value from stdin *** @param msg The message to print *** @param dft The default long value returned, if no value was entered *** @return The long value read from stdin **/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -