如果我有以下寫入檔案的邏輯,在 Groovy 上完成
def file = new File(FILE_PATH FILE_NAME)
file.append("hello again!")
它(append(String s)方法)是否會在出現例外或其他情況時自動關閉流?
uj5u.com熱心網友回復:
在查看了 groovy 源代碼之后,是的。
詳情:
// ResourceGroovyMethods.java line 870
/**
* Append the text at the end of the File without writing a BOM.
*
* @param file a File
* @param text the text to append at the end of the File
* @throws IOException if an IOException occurs.
* @since 1.0
*/
public static void append(File file, Object text) throws IOException {
append(file, text, false);
}
// ResourceGroovyMethods.java line 882
/**
* Append the text at the end of the File. If the default
* charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias) and
* <code>writeBom</code> is <code>true</code>, the requisite byte order
* mark is written to the file before the text.
*
* @param file a File
* @param text the text to append at the end of the File
* @param writeBom whether to write a BOM
* @throws IOException if an IOException occurs.
* @since 2.5.0
*/
public static void append(File file, Object text, boolean writeBom) throws IOException {
append(file, text, Charset.defaultCharset().name(), writeBom);
}
// ResourceGroovyMethods.java line 1027
/**
* Append the text at the end of the File, using a specified encoding. If
* the given charset is "UTF-16BE" or "UTF-16LE" (or an equivalent alias),
* <code>writeBom</code> is <code>true</code>, and the file doesn't already
* exist, the requisite byte order mark is written to the file before the
* text is appended.
*
* @param file a File
* @param text the text to append at the end of the File
* @param charset the charset used
* @param writeBom whether to write a BOM
* @throws IOException if an IOException occurs.
* @since 2.5.0
*/
public static void append(File file, Object text, String charset, boolean writeBom) throws IOException {
Writer writer = null;
try {
boolean shouldWriteBom = writeBom && !file.exists();
FileOutputStream out = new FileOutputStream(file, true);
if (shouldWriteBom) {
IOGroovyMethods.writeUTF16BomIfRequired(out, charset);
}
writer = new OutputStreamWriter(out, charset);
InvokerHelper.write(writer, text);
writer.flush();
Writer temp = writer;
writer = null;
temp.close();
} finally {
closeWithWarning(writer);
}
}
// DefaultGroovyMethodsSupport.java line 119
/**
* Close the Closeable. Logging a warning if any problems occur.
*
* @param closeable the thing to close
*/
public static void closeWithWarning(Closeable closeable) {
tryClose(closeable, true); // ignore result
}
// DefaultGroovyMethodsSupport.java line 128
/**
* Attempts to close the closeable returning rather than throwing
* any Exception that may occur.
*
* @param closeable the thing to close
* @param logWarning if true will log a warning if an exception occurs
* @return throwable Exception from the close method, else null
*/
static Throwable tryClose(AutoCloseable closeable, boolean logWarning) {
Throwable thrown = null;
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
thrown = e;
if (logWarning) {
LOG.warning("Caught exception during close(): " e);
}
}
}
return thrown;
}
您的file.append(string)呼叫將由該public static void append(File file, Object text)方法處理。
在代碼之后,我們可以看到撰寫器在一個finally塊中關閉,這意味著關閉操作將始終運行,無論是否有例外。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/418327.html
標籤:
上一篇:如何將物件分配給繼承的函式
