Example usage for java.io BufferedWriter write

List of usage examples for java.io BufferedWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:it.iit.genomics.cru.bridges.liftover.ws.LiftOverRun.java

public static Mapping runLiftOver(String genome, String fromAssembly, String toAssembly, String chromosome,
        int start, int end) {

    String liftOverCommand = RESOURCE_BUNDLE.getString("liftOverCommand");

    String liftOverPath = RESOURCE_BUNDLE.getString("liftOverPath");

    String mapChainDir = RESOURCE_BUNDLE.getString("mapChainDir");

    String tmpDir = RESOURCE_BUNDLE.getString("tmpDir");

    Mapping mapping = null;//  ww  w  . j  a v  a  2  s  . co m

    Runtime r = Runtime.getRuntime();

    String rootFilename = String.format("%s", RandomStringUtils.randomAlphanumeric(8));

    String inputFilename = rootFilename + "-" + fromAssembly + ".bed";
    String outputFilename = rootFilename + "-" + toAssembly + ".bed";
    String unmappedFilename = rootFilename + "-" + "unmapped.bed";

    String mapChain = fromAssembly.toLowerCase() + "To" + toAssembly.toUpperCase().charAt(0)
            + toAssembly.toLowerCase().substring(1) + ".over.chain.gz";

    try {

        File tmpDirFile = new File(tmpDir);

        // if the directory does not exist, create it
        if (false == tmpDirFile.exists()) {
            System.out.println("creating directory: " + tmpDir);
            boolean result = tmpDirFile.mkdir();

            if (result) {
                System.out.println("DIR created");
            }
        }

        // Write input bed file
        File inputFile = new File(tmpDir + inputFilename);
        // if file doesnt exists, then create it
        if (!inputFile.exists()) {
            inputFile.createNewFile();
        }

        FileWriter fw = new FileWriter(inputFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(chromosome + "\t" + start + "\t" + end + "\n");
        bw.close();

        String commandArgs = String.format("%s %s %s %s %s", liftOverPath + "/" + liftOverCommand,
                tmpDir + inputFilename, mapChainDir + mapChain, tmpDir + outputFilename,
                tmpDir + unmappedFilename);
        System.out.println(commandArgs);

        Process p = r.exec(commandArgs);

        p.waitFor();

        BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";

        while ((line = b.readLine()) != null) {
            System.out.println(line);
        }
        b.close();

        b = new BufferedReader(new FileReader(tmpDir + outputFilename));

        while ((line = b.readLine()) != null) {
            String[] cells = line.split("\t");
            String newChromosome = cells[0];
            int newStart = Integer.parseInt(cells[1]);
            int newEnd = Integer.parseInt(cells[2]);
            mapping = new Mapping(genome, toAssembly, newChromosome, newStart, newEnd);
        }
        b.close();

        // delete
        File delete = new File(tmpDir + inputFilename);
        delete.delete();

        delete = new File(tmpDir + outputFilename);
        delete.delete();

        delete = new File(tmpDir + unmappedFilename);
        delete.delete();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    return mapping;
}

From source file:Main.java

/**
 * Print out the spike trains so it can be analyzed by plotting software
 * @param file/*  www  .  j av  a2  s.c  o  m*/
 * @param array
 */
public static void print(File file, double[] array) {
    FileWriter fstream = null;
    BufferedWriter out = null;

    try {
        fstream = new FileWriter(file);
        out = new BufferedWriter(fstream);
        for (int i = 0; i < array.length; i++) {
            out.write(array[i] + "\t");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fstream != null) {
            try {
                fstream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:com.beginner.core.utils.Tools.java

/**
 * ?txt?/*from ww  w. ja  v  a2s. c  om*/
 * @param fileP    
 * @param content    
 * @since          1.0.0
 */
public static void writeFile(String fileP, String content) {
    String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "../../"; //
    filePath = (filePath.trim() + fileP.trim()).substring(6).trim();
    if (filePath.indexOf(":") != 1) {
        filePath = File.separator + filePath;
    }
    try {
        OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(filePath), "utf-8");
        BufferedWriter writer = new BufferedWriter(write);
        writer.write(content);
        writer.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:mainserver.Server.java

public static void sendMessage(String text, Socket clientSocket) {
    try {/* www .  ja  v a2 s  .c o m*/
        BufferedWriter outputToServer = new BufferedWriter(
                new OutputStreamWriter(clientSocket.getOutputStream()));
        outputToServer.write(text + "\r\n");
        outputToServer.flush();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:forge.error.BugReporter.java

public static void saveToFile(final String text) {
    File f;//from w  w  w  . j av a 2  s .  c om
    final long curTime = System.currentTimeMillis();
    for (int i = 0;; i++) {
        final String name = String.format("%TF-%02d.txt", curTime, i);
        f = new File(name);
        if (!f.exists()) {
            break;
        }
    }

    f = GuiBase.getInterface().getSaveFile(f);

    try {
        final BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        bw.write(text);
        bw.close();
    } catch (final IOException ex) {
        SOptionPane.showMessageDialog("There was an error during saving. Sorry!\n" + ex, "Error saving file",
                SOptionPane.ERROR_ICON);
    }
}

From source file:ca.uqac.info.trace.conversion.TraceConverter.java

/**
 * Writes some string to a file/*  w w w .jav  a 2s  . c o m*/
 * @param filename The filename to write to
 * @param contents The file's contents
 */
private static void writeToFile(String filename, String contents) {
    try {
        // Create file 
        FileWriter fstream = new FileWriter(filename);
        BufferedWriter out = new BufferedWriter(fstream);
        out.write(contents);
        //Close the output stream
        out.close();
    } catch (Exception e) {
        // Catch exception if any
        System.err.println("ERROR: " + e.getMessage());
    }
}

From source file:com.dahl.brendan.wordsearch.view.IOService.java

private static void writeFile(Context context, File file, boolean overwrite) {
    if (!file.exists() || overwrite) {
        Cursor cursor = context.getContentResolver().query(Word.CONTENT_URI, new String[] { Word.WORD }, null,
                null, null);/*from  ww w .  j av a2  s. c om*/
        JSONArray array = new JSONArray();
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                array.put(cursor.getString(0));
                cursor.moveToNext();
            }
        }
        try {
            file.getParentFile().mkdirs();
            file.createNewFile();
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            out.write(array.toString());
            out.close();
        } catch (IOException e) {
            Log.e(LOGTAG, "write failed", e);
        }
    }
}

From source file:Main.java

public static BufferedWriter getWriter(File file, final String group, String comment) throws IOException {
    BufferedWriter bw = new BufferedWriter(new FileWriter(file)) {

        @Override/*from ww  w  .  ja v a  2s .  c  om*/
        public void close() throws IOException {
            write("</" + group + ">");
            super.close();
        }
    };
    bw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    bw.newLine();
    bw.newLine();
    bw.write("<!--" + comment + "-->");
    bw.newLine();
    bw.newLine();
    bw.write("<" + group + ">");
    bw.newLine();

    return bw;
}

From source file:Main.java

/**
 * Print out the spike trains so it can be analyzed by plotting software
 * @param file//  ww  w . j  a  v a 2  s  .c  o  m
 * @param array
 */
public static void printArray(File file, double[] x, double[] y) {
    FileWriter fstream = null;
    BufferedWriter out = null;

    try {
        fstream = new FileWriter(file);
        out = new BufferedWriter(fstream);
        for (int i = 0; i < x.length; i++) {
            out.write(x[i] + "\t");
        }
        out.write("\r\n");
        for (int i = 0; i < y.length; i++) {
            out.write(y[i] + "\t");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fstream != null) {
            try {
                fstream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:com.netflix.config.DynamicFileConfigurationTest.java

static File createConfigFile(String prefix) throws Exception {
    configFile = File.createTempFile(prefix, ".properties");
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8"));
    writer.write("dprops1=123456789");
    writer.newLine();/* w  w w  .  j  a va  2 s. c o m*/
    writer.write("dprops2=79.98");
    writer.newLine();
    writer.close();
    System.err.println(configFile.getPath() + " created");
    return configFile;
}