Example usage for java.io BufferedWriter append

List of usage examples for java.io BufferedWriter append

Introduction

In this page you can find the example usage for java.io BufferedWriter append.

Prototype

public Writer append(CharSequence csq) throws IOException 

Source Link

Document

Appends the specified character sequence to this writer.

Usage

From source file:Main.java

public static void saveBoardToExternal(Context context, String fileName, JSONObject jsonObject) {
    File file = new File(getDirectoryBoards(), fileName);
    if (file.exists()) {
        file.delete();//from w  ww.ja  va  2 s  .  c  o m
    }

    try {
        file.createNewFile();
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.append(jsonObject.toString());
        buf.close();
        addTomMediaScanner(context, file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

static void writeToFollower(String name) {
    try {/*from  ww  w. j av  a 2 s.  c o  m*/
        File root = new File(Environment.getExternalStorageDirectory().toString(), ".Instagram");
        if (!root.exists()) {
            root.mkdirs();

        }
        File file = new File(root, "Following.txt");
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.newLine();
        buf.append(name);
        buf.close();
    } catch (Throwable t) {
    }
}

From source file:com.sludev.commons.vfs2.provider.azure.AzTestUtils.java

public static File createTempFile(String prefix, String ext, String content) throws IOException {
    File res = File.createTempFile(prefix, ext);
    try (FileWriter fw = new FileWriter(res)) {
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(content);
        bw.flush();//from   w w w.  j  a  v a2s. co m
    }

    return res;
}

From source file:Main.java

public static boolean writeErrorLogToSDCard(String path, String errorLog) {
    if (!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) // check if SD card is mounted
        return false;

    File file;/*from   w  w  w  . j  a  v a  2 s .co  m*/
    file = new File(path);
    if (!file.exists()) {
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
    }

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); //true means append to file
        writer.append(errorLog);
        writer.newLine();
        writer.append("-------------------------------"); // seperator
        writer.newLine();
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
    return true;
}

From source file:de.unisb.cs.st.javalanche.mutation.util.DBPerformanceTest.java

private static void testWrite() {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/* w  ww .j  av a2  s  . c  o m*/
    try {
        File file = new File("test.txt");
        file.deleteOnExit();
        FileWriter fw = new FileWriter(file);
        BufferedWriter w = new BufferedWriter(fw);
        for (int i = 0; i < 50 * 1024 * 1024; i++) {
            w.append('x');
        }
        w.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    stopWatch.stop();
    System.out.printf("Writing file took %s\n", DurationFormatUtils.formatDurationHMS(stopWatch.getTime()));

}

From source file:au.org.ala.delta.intkey.model.StartupUtils.java

/**
 * Save a copy of a dataset that was opened from a remote location
 * //w  w  w  .j  a v  a 2s . com
 * @param context
 *            Intkey context
 * @param saveDir
 *            Directory in which to save a copy of the dataset
 * @return A copy of the JNLP-style dataset startup file to use to open the
 *         saved copy of the dataset.
 * @throws IOException
 *             If saving to disk failed.
 */
public static File saveRemoteDataset(IntkeyContext context, File saveDir) throws IOException {
    StartupFileData startupFileData = context.getStartupFileData();
    File datasetZip = startupFileData.getDataFileLocalCopy();

    // Copy the zipped dataset as downloaded from the web
    // FileUtils.copyFileToDirectory(datasetZip, saveDir);

    // Copy the zipped dataset as downloaded from the web
    // Use utility method to avoid overwriting existing files with the same
    // name
    File copyZipFile = Utils.getSaveFileForDirectory(saveDir, datasetZip.getName());
    FileUtils.copyFile(datasetZip, copyZipFile);

    // Write a new .ink file
    // Use utility method to avoid overwriting existing files with the same
    // name
    File newInkFile = Utils.getSaveFileForDirectory(saveDir,
            FilenameUtils.getName(startupFileData.getInkFileLocation().getFile()));

    FileWriter fw = new FileWriter(newInkFile);
    BufferedWriter bufFW = new BufferedWriter(fw);

    bufFW.append(INIT_FILE_INK_FILE_KEYWORD);
    bufFW.append("=");
    bufFW.append(newInkFile.toURI().toURL().toString());
    bufFW.append("\n");

    bufFW.append(INIT_FILE_DATA_FILE_KEYWORD);
    bufFW.append("=");
    bufFW.append(copyZipFile.toURI().toURL().toString());
    bufFW.append("\n");

    bufFW.append(INIT_FILE_INITIALIZATION_FILE_KEYWORD);
    bufFW.append("=");
    bufFW.append(startupFileData.getInitializationFileLocation());
    bufFW.append("\n");

    String imagePath = startupFileData.getImagePath();
    if (imagePath != null) {
        bufFW.append(INIT_FILE_IMAGE_PATH_KEYWORD);
        bufFW.append("=");
        bufFW.append(imagePath);
        bufFW.append("\n");
    }

    String infoPath = startupFileData.getInfoPath();
    if (infoPath != null) {
        bufFW.append(INIT_FILE_INFO_PATH_KEYWORD);
        bufFW.append("=");
        bufFW.append(infoPath);
        bufFW.append("\n");
    }

    bufFW.flush();
    bufFW.close();

    return newInkFile;
}

From source file:uk.ac.ncl.aries.entanglement.cli.export.MongoGraphToAscii.java

private static void exportAscii(Mongo m, DB db, String graphName, String graphBranch, File outputFile)
        throws IOException, GraphModelException, RevisionLogException {
    /*//from   w  w  w .j av a2  s  .c  o m
     * Database access
     */
    GraphCheckoutNamingScheme collectionNamer = new GraphCheckoutNamingScheme(graphName, graphBranch);
    DBCollection nodeCol = db.getCollection(collectionNamer.getNodeCollectionName());
    DBCollection edgeCol = db.getCollection(collectionNamer.getEdgeCollectionName());

    NodeDAO nodeDao = GraphDAOFactory.createDefaultNodeDAO(classLoader, m, db, nodeCol, edgeCol);
    EdgeDAO edgeDao = GraphDAOFactory.createDefaultEdgeDAO(classLoader, m, db, nodeCol, edgeCol);

    RevisionLog log = new RevisionLogDirectToMongoDbImpl(classLoader, m, db);

    /*
     * File writer
     */
    BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));

    writer.append("Nodes:\n");
    for (DBObject node : nodeDao.iterateAll()) {
        writer.append(node.toString()).append("\n");
    }

    writer.append("Edges:\n");
    for (DBObject edge : edgeDao.iterateAll()) {
        writer.append(edge.toString()).append("\n");
    }

    writer.flush();
    writer.close();
}

From source file:org.mskcc.cbio.portal.scripts.FilterCases.java

private static void cutRows(String inFile, String outFile, String concatRegex, int idIx, int startColumn)
        throws IOException {

    BufferedReader in = new BufferedReader(new FileReader(inFile));
    BufferedWriter out = new BufferedWriter(new FileWriter(outFile));
    for (int i = 0; i < startColumn; i++) {
        out.append(in.readLine());
        out.newLine();//  ww w .j a  v a 2 s.  com
    }

    Pattern p = Pattern.compile(concatRegex);

    for (String line = in.readLine(); line != null; line = in.readLine()) {
        String[] parts = line.split("\t");
        Matcher m = p.matcher(parts[idIx]);
        if (m.find()) {
            out.append(line);
            out.newLine();
        }
    }
    in.close();
    out.close();
}

From source file:Main.java

public static void appendLog(String text) {
    Log.d("LOGFILE", text);
    SimpleDateFormat sTime = new SimpleDateFormat("dd/MMM/yyyy - hh:mm:ss");
    String timeFormat = sTime.format(new Date());

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/Cura/Logs/");
    dir.mkdirs();//www.  j  a  v a2  s.  c  om
    File logFile = new File(dir, "Cura_Logs_DEBUG.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        if (text.compareTo("wipe") == 0)
            logFile.delete();
        else {
            buf.append("[" + timeFormat + "] - ");
            buf.append(text);
            buf.newLine();
            buf.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");
    if (!logFile.exists()) {
        try {//from w ww .j  a v a 2 s .  c o m
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        BufferedReader buf = new BufferedReader(new FileReader(logFile));

        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        while ((line = buf.readLine()) != null) {
            lines.add(line);
        }

        int size = lines.size();

        //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum
        int i = lines.size() - 500;
        if (i < 0) {
            i = 0;
        }

        buf.close();

        BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true));

        if (size > 1000) {
            bufW.write("");
            for (; i < lines.size(); i++) {
                bufW.append(line);
            }
        }

        bufW.append(text + "\n");
        bufW.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}