Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("filename.txt"), "utf-8")); writer.write("Something"); } catch (IOException ex) { // report } finally { try {writer.close();} catch (Exception ex) {/*ignore*/} }
/** * Copy the contents of the given String to the given output OutputStream. * Leaves the stream open when done. * @param in the String to copy from * @param charset the Charset * @param out the OutputStream to copy to * @throws IOException in case of I/O errors */ public static void copy(String in, Charset charset, OutputStream out) throws IOException { Assert.notNull(in, "No input String specified"); Assert.notNull(charset, "No charset specified"); Assert.notNull(out, "No OutputStream specified"); Writer writer = new OutputStreamWriter(out, charset); writer.write(in); writer.flush(); }
import java.io.FileWriter; FileWriter writer = new FileWriter("output.txt"); for(String str: arr) { writer.write(str); } writer.close();
try { // Create temp file. File temp = File.createTempFile("pattern", ".suffix"); // Delete temp file when program exits. temp.deleteOnExit(); // Write to temp file BufferedWriter out = new BufferedWriter(new FileWriter(temp)); out.write("aString"); out.close(); } catch (IOException e) { }
DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(wr, "UTF-8")); writer.write(content); writer.close(); wr.close();
@Override public String invoke(File dir, VirtualChannel channel) throws IOException { if(!inThisDirectory) dir = new File(System.getProperty("java.io.tmpdir")); else mkdirs(dir); File f; try { f = creating(File.createTempFile(prefix, suffix, dir)); } catch (IOException e) { throw new IOException("Failed to create a temporary directory in "+dir,e); } try (Writer w = new FileWriter(writing(f))) { w.write(contents); } return f.getAbsolutePath(); } }
public static void writeStringAsFile(final String fileContents, String fileName) { Context context = App.instance.getApplicationContext(); try { FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName)); out.write(fileContents); out.close(); } catch (IOException e) { Logger.logError(TAG, e); } } public static String readFileAsString(String fileName) { Context context = App.instance.getApplicationContext(); StringBuilder stringBuilder = new StringBuilder(); String line; BufferedReader in = null; try { in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName))); while ((line = in.readLine()) != null) stringBuilder.append(line); } catch (FileNotFoundException e) { Logger.logError(TAG, e); } catch (IOException e) { Logger.logError(TAG, e); } return stringBuilder.toString(); }
@Override public void saveClassFile(String path, String qualifiedName, String entryName, String content, int[] mapping) { File file = new File(getAbsolutePath(path), entryName); try (Writer out = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) { out.write(content); } catch (IOException ex) { DecompilerContext.getLogger().writeMessage("Cannot write class file " + file, ex); } }
@Test public void openFileInput_shouldReturnAFileInputStream() throws Exception { String fileContents = "blah"; File file = new File(context.getFilesDir(), "__test__"); try (Writer fileWriter = Files.newBufferedWriter(file.toPath(), UTF_8)) { fileWriter.write(fileContents); } try (FileInputStream fileInputStream = context.openFileInput("__test__")) { byte[] bytes = new byte[fileContents.length()]; fileInputStream.read(bytes); assertThat(bytes).isEqualTo(fileContents.getBytes(UTF_8)); } }
try ( FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw) ) { bw.write(text); }
BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("test.txt")); bw.write("test"); bw.flush(); // you can omit this if you don't care about errors while flushing bw.close(); // you can omit this if you don't care about errors while closing } catch (IOException e) { // error handling (e.g. on flushing) } finally { IOUtils.closeQuietly(bw); }
public static void main(String[] args) throws Exception { String nodeValue = "i am mostafa"; // you want to output to file // BufferedWriter writer = new BufferedWriter(new FileWriter(file3, true)); // but let's print to console while debugging BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); String[] words = nodeValue.split(" "); for (String word: words) { writer.write(word); writer.newLine(); } writer.close(); }
File myFoo = new File("foo.log"); FileWriter fooWriter = new FileWriter(myFoo, false); // true to append // false to overwrite. fooWriter.write("New Contents\n"); fooWriter.close();
private void writePackFile (File outputDir, String scaledPackFileName, Array<Page> pages) throws IOException { File packFile = new File(outputDir, scaledPackFileName + settings.atlasExtension); File packDir = packFile.getParentFile(); packDir.mkdirs(); Writer writer = new OutputStreamWriter(new FileOutputStream(packFile, true), "UTF-8"); for (Page page : pages) { writer.write("\n" + page.imageName + "\n"); writer.write("size: " + page.imageWidth + "," + page.imageHeight + "\n"); writer.write("format: " + settings.format + "\n"); writer.write("filter: " + settings.filterMin + "," + settings.filterMag + "\n"); writer.write("repeat: " + getRepeatValue() + "\n");
try (Writer writer = new BufferedWriter(new OutputStreamWriter( new FileOutputStream("filename.txt"), "utf-8"))) { writer.write("something"); }
@Before public void setup() throws IOException { inputDir = temporaryFolder.newFolder("data"); // set up data for (int i = 0; i < 5; i++) { try (final Writer writer = Files.newBufferedWriter(new File(inputDir, "test_" + i).toPath(), StandardCharsets.UTF_8)) { writer.write(StringUtils.format("2017-12-%d,%d th test file\n", 24 + i, i)); writer.write(StringUtils.format("2017-12-%d,%d th test file\n", 25 + i, i)); } } for (int i = 0; i < 5; i++) { try (final Writer writer = Files.newBufferedWriter(new File(inputDir, "filtered_" + i).toPath(), StandardCharsets.UTF_8)) { writer.write(StringUtils.format("2017-12-%d,%d th test file\n", 25 + i, i)); } } indexingServiceClient = new LocalIndexingServiceClient(); localDeepStorage = temporaryFolder.newFolder("localStorage"); }
/** * Closes the underlying writer. * * @throws IOException if an error occurs closing the underlying writer */ @Override public void close() throws IOException { if (writer == null) { encoding = defaultEncoding; writer = new OutputStreamWriter(out, encoding); writer.write(xmlPrologWriter.toString()); } writer.close(); }
try (FileWriter fw = new FileWriter(file)) { BufferedWriter bw = new BufferedWriter(fw); bw.write(text); }
try { String filename= "MyFile.txt"; FileWriter fw = new FileWriter(filename,true); //the true will append the new data fw.write("add a line\n");//appends the string to the file fw.close(); } catch(IOException ioe) { System.err.println("IOException: " + ioe.getMessage()); }
Date date = new Date() ; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ; File file = new File(dateFormat.format(date) + ".tsv") ; BufferedWriter out = new BufferedWriter(new FileWriter(file)); out.write("Writing to file"); out.close();