List of usage examples for java.io FileWriter FileWriter
public FileWriter(FileDescriptor fd)
From source file:Main.java
public static File saveText(String text) throws IOException { File f;/* w ww . ja v a2 s . c o m*/ String filename; if (!isExternalStorageWritable()) throw new IOException("Storage not available"); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); filename = dateFormat.format(new Date()); f = new File(getAlbumStorageDir(FOLDER_NAME), "Message " + filename + ".txt"); FileWriter fw = new FileWriter(f); fw.write(text); fw.close(); return f; }
From source file:Main.java
/** * Create a script file to run one command * @param cmmd Command for the script to run * @param fileName The file name/* www .ja v a2 s . c om*/ * @return The file * @throws IOException */ public static File createScriptFile(String cmmd, String fileName) throws IOException { String f = fileName == null ? "script_" + System.currentTimeMillis() : fileName; FileWriter w = new FileWriter(f); w.write(cmmd + "\n"); w.close(); File file = new File(f); file.setExecutable(true, false); return file; }
From source file:Main.java
/** * Writes all characters from a <tt>Reader</tt> to a file using the default * character encoding./*from www . j a va2s . co m*/ * * @param reader The <tt>Reader</tt> containing the data to write to the * file. * @param file The file to write the data to. * @return The total number of characters written. * @throws IOException If an I/O error occured while trying to write the * data to the file. * @see java.io.FileWriter */ public static final long writeToFile(Reader reader, File file) throws IOException { FileWriter writer = new FileWriter(file); try { return transfer(reader, writer); } finally { writer.close(); } }
From source file:Main.java
public static synchronized boolean update(String sourceXML, Document doc) { try {//from ww w . j ava2s. c o m Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, ENCODING); tf.setOutputProperty(OutputKeys.INDENT, INDENT); Writer out = new FileWriter(new File(sourceXML)); tf.transform(new DOMSource(doc), new StreamResult(out)); return true; } catch (TransformerException | IOException e) { e.printStackTrace(); } return false; }
From source file:Main.java
public static void writeTextFile(String[] text, String filename) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); for (int i = 0; i < text.length; i++) { writer.write(text[i]);// ww w . j av a 2 s . c o m writer.newLine(); } writer.close(); }
From source file:Main.java
public static void delete(String filename, int startline, int numlines) { try {/*from ww w .j a v a 2 s . c o m*/ BufferedReader br = new BufferedReader(new FileReader(filename)); //String buffer to store contents of the file StringBuffer sb = new StringBuffer(""); //Keep track of the line number int linenumber = 1; String line; while ((line = br.readLine()) != null) { //Store each valid line in the string buffer if (linenumber < startline || linenumber >= startline + numlines) sb.append(line + "\n"); linenumber++; } if (startline + numlines > linenumber) System.out.println("End of file reached."); br.close(); FileWriter fw = new FileWriter(new File(filename)); //Write entire string buffer into the file fw.write(sb.toString()); fw.close(); } catch (Exception e) { System.out.println("Something went horribly wrong: " + e.getMessage()); } }
From source file:Main.java
public static void indentXmlFile(File file) throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//w w w.j a v a2 s.c om DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); Document doc = documentBuilder.parse(file); OutputFormat format = new OutputFormat(doc); format.setLineWidth(80); format.setIndenting(true); format.setIndent(4); XMLSerializer serializer = new XMLSerializer(new FileWriter(file), format); serializer.serialize(doc); }
From source file:Main.java
public static void writeToFile(Iterator<?> iter, File file) throws IOException { if (iter != null && file != null) { file.getParentFile().mkdirs();/*from w w w . j ava 2s .com*/ File tempFile = new File(file.getAbsolutePath() + ".tmp"); FileWriter fw = new FileWriter(tempFile); for (; iter.hasNext();) { fw.write(new StringBuilder(iter.next().toString()).append("\r\n").toString()); } fw.flush(); fw.close(); file.delete(); tempFile.renameTo(file); } }
From source file:Main.java
public static void startLogger(String file) { if (writer != null) { stopLogger();/* ww w . j ava 2 s . co m*/ } try { writer = new PrintWriter(new FileWriter(file)); } catch (Exception e) { e.printStackTrace(); if (writer != null) { writer.close(); } } }
From source file:WriteFile.java
private static PrintWriter openWriter(String name) { try {/* ww w. j a v a2 s . c om*/ File file = new File(name); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)), true); return out; } catch (IOException e) { System.out.println("I/O Error"); System.exit(0); } return null; }