本文整理匯總了Java中java.io.Writer.close方法的典型用法代碼示例。如果您正苦於以下問題:Java Writer.close方法的具體用法?Java Writer.close怎麽用?Java Writer.close使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.io.Writer
的用法示例。
在下文中一共展示了Writer.close方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createFiles
import java.io.Writer; //導入方法依賴的package包/類
private static void createFiles(int length, int numFiles, Random random)
throws IOException {
Range[] ranges = createRanges(length, numFiles, random);
for (int i = 0; i < numFiles; i++) {
Path file = new Path(workDir, "test_" + i + ".txt");
Writer writer = new OutputStreamWriter(localFs.create(file));
Range range = ranges[i];
try {
for (int j = range.start; j < range.end; j++) {
writer.write(Integer.toString(j));
writer.write("\n");
}
} finally {
writer.close();
}
}
}
示例2: write
import java.io.Writer; //導入方法依賴的package包/類
public static boolean write() {
FileHandle fileHandle = Compatibility.get().getBaseFolder().child("settings.json");
JsonObject json = new JsonObject();
for (Map.Entry<String, Setting> entry : settings.entrySet()) {
json.set(entry.getKey(), entry.getValue().toJson());
}
try {
Writer writer = fileHandle.writer(false);
json.writeTo(writer, WriterConfig.PRETTY_PRINT);
writer.close();
} catch (Exception e) {
Log.error("Failed to write settings", e);
fileHandle.delete();
return false;
}
return true;
}
示例3: testCloseFlush
import java.io.Writer; //導入方法依賴的package包/類
public void testCloseFlush() throws IOException {
SpyAppendable spy = new SpyAppendable();
Writer writer = new AppendableWriter(spy);
writer.write("Hello");
assertFalse(spy.flushed);
assertFalse(spy.closed);
writer.flush();
assertTrue(spy.flushed);
assertFalse(spy.closed);
writer.close();
assertTrue(spy.flushed);
assertTrue(spy.closed);
}
示例4: process
import java.io.Writer; //導入方法依賴的package包/類
@Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver() || written++ > 0) {
return false;
}
messager.printMessage(Diagnostic.Kind.NOTE, "writing Generated.java");
try {
Writer w = processingEnv.getFiler().createSourceFile("p.Generated").openWriter();
try {
w.write("package p; public class Generated { public static void m() { } }");
} finally {
w.close();
}
} catch (IOException x) {
messager.printMessage(Diagnostic.Kind.ERROR, x.toString());
}
return true;
}
示例5: testNoManifest
import java.io.Writer; //導入方法依賴的package包/類
@Test
public void testNoManifest() throws Exception {
File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
TestJarFinder.class.getName() + "-testNoManifest");
delete(dir);
dir.mkdirs();
File propsFile = new File(dir, "props.properties");
Writer writer = new FileWriter(propsFile);
new Properties().store(writer, "");
writer.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream zos = new JarOutputStream(baos);
JarFinder.jarDir(dir, "", zos);
JarInputStream jis =
new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
Assert.assertNotNull(jis.getManifest());
jis.close();
}
示例6: main
import java.io.Writer; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
File src = new File("C.java");
Writer w = new FileWriter(src);
try {
w.write("import static p.Generated.m;\nclass C { {m(); } }\n");
w.flush();
} finally {
w.close();
}
JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
try (StandardJavaFileManager fm = jc.getStandardFileManager(null, null, null)) {
JavaCompiler.CompilationTask task = jc.getTask(null, fm, null, null, null,
fm.getJavaFileObjects(src));
task.setProcessors(Collections.singleton(new Proc()));
if (!task.call()) {
throw new Error("Test failed");
}
}
}
示例7: writeDown
import java.io.Writer; //導入方法依賴的package包/類
/** store the setting object even if was not changed */
private void writeDown() throws IOException {
Object inst = instance.get();
if (inst == null) return ;
ByteArrayOutputStream b = new ByteArrayOutputStream(1024);
Writer w = new OutputStreamWriter(b, "UTF-8"); // NOI18N
try {
isWriting = true;
Convertor conv = getConvertor();
if (conv != null) {
conv.write(w, inst);
} else {
write(w, inst);
}
} finally {
w.close();
isWriting = false;
}
isChanged = false;
buf = b;
file.getFileSystem().runAtomicAction(this);
buf = null;
if (!isChanged) firePropertyChange(PROP_SAVE);
}
示例8: writeNonEffectivePom
import java.io.Writer; //導入方法依賴的package包/類
private void writeNonEffectivePom(final Writer pomWriter) throws IOException {
try {
withXmlActions.transform(pomWriter, POM_FILE_ENCODING, new ErroringAction<Writer>() {
protected void doExecute(Writer writer) throws IOException {
new MavenXpp3Writer().write(writer, getModel());
}
});
} finally {
pomWriter.close();
}
}
示例9: testExistingManifest
import java.io.Writer; //導入方法依賴的package包/類
@Test
public void testExistingManifest() throws Exception {
File dir = new File(System.getProperty("test.build.dir", "target/test-dir"),
TestJarFinder.class.getName() + "-testExistingManifest");
delete(dir);
dir.mkdirs();
File metaInfDir = new File(dir, "META-INF");
metaInfDir.mkdirs();
File manifestFile = new File(metaInfDir, "MANIFEST.MF");
Manifest manifest = new Manifest();
OutputStream os = new FileOutputStream(manifestFile);
manifest.write(os);
os.close();
File propsFile = new File(dir, "props.properties");
Writer writer = new FileWriter(propsFile);
new Properties().store(writer, "");
writer.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream zos = new JarOutputStream(baos);
JarFinder.jarDir(dir, "", zos);
JarInputStream jis =
new JarInputStream(new ByteArrayInputStream(baos.toByteArray()));
Assert.assertNotNull(jis.getManifest());
jis.close();
}
示例10: _testAuthentication
import java.io.Writer; //導入方法依賴的package包/類
protected void _testAuthentication(Authenticator authenticator, boolean doPost) throws Exception {
start();
try {
URL url = new URL(getBaseURL());
AuthenticatedURL.Token token = new AuthenticatedURL.Token();
Assert.assertFalse(token.isSet());
TestConnectionConfigurator connConf = new TestConnectionConfigurator();
AuthenticatedURL aUrl = new AuthenticatedURL(authenticator, connConf);
HttpURLConnection conn = aUrl.openConnection(url, token);
Assert.assertTrue(connConf.invoked);
String tokenStr = token.toString();
if (doPost) {
conn.setRequestMethod("POST");
conn.setDoOutput(true);
}
conn.connect();
if (doPost) {
Writer writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(POST);
writer.close();
}
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
if (doPost) {
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String echo = reader.readLine();
Assert.assertEquals(POST, echo);
Assert.assertNull(reader.readLine());
}
aUrl = new AuthenticatedURL();
conn = aUrl.openConnection(url, token);
conn.connect();
Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode());
Assert.assertEquals(tokenStr, token.toString());
} finally {
stop();
}
}
示例11: writedoc
import java.io.Writer; //導入方法依賴的package包/類
/**
* Writes the given element's subtree to the specified file.
*
* @param fname The output file name
* @param ele The xml Element subtree to write to file
* @param doc The Document
* @return True if content previously existed in the given file and the content is the same
* as the new content provided
* @exception Hexception If exception
*/
private boolean writedoc(
String fname,
Element ele,
Document doc)
throws Hexception {
try {
boolean contentEquals = false;
String s1 = null;
File f = new File(fname);
if (f.exists())
s1 = Files.readFileToEncoding(f, "UTF-8").toString();
FileOutputStream fos = new FileOutputStream(f);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
Writer wtr = new BufferedWriter(osw);
OutputFormat format = new OutputFormat(doc, "UTF-8", true);
// Indenting true
format.setMethod("xml");
// May not ne necessary to call this
format.setLineWidth(0);
// No line wrapping
XMLSerializer ser = new XMLSerializer(wtr, format);
ser.serialize(ele);
fos.close();
osw.close();
wtr.close();
if (s1 != null)
contentEquals = s1.contentEquals(Files.readFileToEncoding(f, "UTF-8"));
return contentEquals;
} catch (IOException ioe) {
throw new Hexception("cannot write file \"" + fname
+ "\" reason: " + ioe);
}
}
示例12: performTestDoNotPerform
import java.io.Writer; //導入方法依賴的package包/類
protected void performTestDoNotPerform(String className, int line, int column) throws Exception {
prepareTest(className);
DataObject od = DataObject.find(testSource);
EditorCookie ec = od.getCookie(EditorCookie.class);
Document doc = ec.openDocument();
List<ErrorDescription> errors = new ErrorHintsProvider().computeErrors(info, doc, Utilities.JAVA_MIME_TYPE);
List<Fix> fixes = new ArrayList<Fix>();
for (ErrorDescription d : errors) {
if (getStartLine(d) + 1 == line)
fixes.addAll(getFixes(d));
}
fixes = sortFixes(fixes);
File fixesDump = new File(getWorkDir(), getName() + "-hints.out");
File diff = new File(getWorkDir(), getName() + "-hints.diff");
Writer hintsWriter = new FileWriter(fixesDump);
for (Fix f : fixes) {
hintsWriter.write(f.getText());
hintsWriter.write("\n");
}
hintsWriter.close();
File hintsGolden = getGoldenFile(getName() + "-hints.pass");
assertFile(fixesDump, hintsGolden, diff);
}
示例13: createFile
import java.io.Writer; //導入方法依賴的package包/類
protected Path createFile(String fileName) throws IOException {
Path file = new Path(workDir, fileName);
Writer writer = new OutputStreamWriter(localFs.create(file));
writer.write("");
writer.close();
return localFs.makeQualified(file);
}
示例14: writeResource
import java.io.Writer; //導入方法依賴的package包/類
/**
* Write <query>.sql to resources folder
*
* @param queryDesc information on query
*/
private void writeResource(QueryDesc queryDesc) {
Filer filer = processingEnv.getFiler();
try {
FileObject o = filer.createResource(StandardLocation.CLASS_OUTPUT,
queryDesc.getPackageName() + ".sql", queryDesc.getMethodNameFirstUpper() + ".sql");
Writer w = o.openWriter();
w.append(queryDesc.getQuery());
w.flush();
w.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例15: testExtractPassword
import java.io.Writer; //導入方法依賴的package包/類
@Test
public void testExtractPassword() throws IOException {
File testDir = new File(System.getProperty("test.build.data",
"target/test-dir"));
testDir.mkdirs();
File secretFile = new File(testDir, "secret.txt");
Writer writer = new FileWriter(secretFile);
writer.write("hadoop");
writer.close();
LdapGroupsMapping mapping = new LdapGroupsMapping();
Assert.assertEquals("hadoop",
mapping.extractPassword(secretFile.getPath()));
}