Here you can find the source of appendStringToFile(String string, String filename)
Parameter | Description |
---|---|
string | String to be written |
filename | File into which the <tt>string</tt> will be wrote |
public static void appendStringToFile(String string, String filename)
//package com.java2s; //License from project: LGPL import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Main { /**//from w ww . j a v a 2 s. c o m * Writes <tt>string</tt> at the end of the file represented by <tt>filename</tt> * * @param string String to be written * @param filename File into which the <tt>string</tt> will be wrote */ public static void appendStringToFile(String string, String filename) { if (string.equals("")) { return; } File destFile = new File(filename); try { if (!destFile.exists()) { destFile.createNewFile(); } FileOutputStream appendedFile = new FileOutputStream(destFile, true); DataOutputStream outputStream = new DataOutputStream(appendedFile); outputStream.writeBytes("\n"); outputStream.writeBytes(string); appendedFile.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Given a string representing a file determines whether the file exists. * @param filename the string representing the path of the file. * @return tru if the file exists, false otherwise. */ public static boolean exists(String filename) { File f = new File(filename); return f.exists(); } }