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:com.ibm.bi.dml.runtime.matrix.CleanupMR.java

/**
 * //  ww  w  . j a v a2  s.c  o  m
 * @param path
 * @param numTasks
 * @throws DMLRuntimeException
 * @throws IOException
 */
private static void writeCleanupTasksToFile(Path path, int numTasks) throws DMLRuntimeException, IOException {
    BufferedWriter br = null;
    try {
        FileSystem fs = FileSystem.get(ConfigurationManager.getCachedJobConf());
        br = new BufferedWriter(new OutputStreamWriter(fs.create(path, true)));

        for (int i = 1; i <= numTasks; i++)
            br.write(String.valueOf("CLEANUP TASK " + i) + "\n");
    } catch (Exception ex) {
        throw new DMLRuntimeException("Error writing cleanup tasks to taskfile " + path.toString(), ex);
    } finally {
        if (br != null)
            br.close();
    }
}

From source file:localization.split.java

public static boolean splitFile(String filepath, String passoloPath, boolean myesri) {
    File log = null;//from ww  w . j av  a2  s.c  om
    try {
        String path = filepath.substring(filepath.lastIndexOf("\\") + 1, filepath.length());
        String folder = filepath.substring(0, filepath.lastIndexOf("\\"));
        String[] files = null;
        Vector<String> zFile;
        if (filepath.endsWith(".zip")) {
            zFile = readzipfile(filepath);
            for (String s : zFile) {
                if (s.endsWith(".lpu")) {
                    File unzipFolder = new File(s.substring(0, s.lastIndexOf("\\")));
                    splitFile(s, passoloPath, myesri);
                }
            }
        } else if (!filepath.endsWith(".lpu")) {
            return false;
        } else {
            if (!checkLPU(filepath, passoloPath)) {
                return false;
            }
            File ECI = new File(folder + "\\ECI");
            File AAC = new File(folder + "\\AAC");
            File TOIN = new File(folder + "\\TOIN");
            File LOIX = new File(folder + "\\LIOX");
            if (!ECI.exists()) {
                ECI.mkdir();
            }
            if (!AAC.exists()) {
                AAC.mkdir();
            }
            if (!TOIN.exists()) {
                TOIN.mkdir();
            }
            if (!LOIX.exists()) {
                LOIX.mkdir();
            }
            if (myesri == true) {
                files = new String[4];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[2] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[3] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                files = new String[6];
                files[0] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI.lpu";
                files[1] = folder + "\\ECI\\" + path.substring(0, path.lastIndexOf(".")) + "_ECI_10.lpu";
                files[2] = folder + "\\AAC\\" + path.substring(0, path.lastIndexOf(".")) + "_AAC.lpu";
                files[3] = folder + "\\TOIN\\" + path.substring(0, path.lastIndexOf(".")) + "_TOIN.lpu";
                files[4] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX_10.lpu";
                files[5] = folder + "\\LIOX\\" + path.substring(0, path.lastIndexOf(".")) + "_LIOX.lpu";

                File srcDir = new File(filepath);
                File trgDir1 = new File(files[0]);
                File trgDir2 = new File(files[1]);
                File trgDir3 = new File(files[2]);
                File trgDir4 = new File(files[3]);
                File trgDir5 = new File(files[4]);
                File trgDir6 = new File(files[5]);
                try {
                    FileUtils.copyFile(srcDir, trgDir1);
                    FileUtils.copyFile(srcDir, trgDir2);
                    FileUtils.copyFile(srcDir, trgDir3);
                    FileUtils.copyFile(srcDir, trgDir4);
                    FileUtils.copyFile(srcDir, trgDir5);
                    FileUtils.copyFile(srcDir, trgDir6);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            String logfile = folder + "\\" + path.substring(path.lastIndexOf("\\") + 1, path.lastIndexOf("."))
                    + ".log";
            log = new File(logfile);
            if (!log.exists()) {
                log.createNewFile();
            }

            for (int i = 0; i < files.length; i++) {
                int exitVal = 0;
                try {
                    String osName = System.getProperty("os.name");
                    String cmd = "cmd.exe /c " + passoloPath + " /openproject:" + files[i]
                            + " /runmacro=PslLpuSplitter_v3.bas" + " >> " + logfile;
                    System.out.println(cmd);
                    Runtime rt = Runtime.getRuntime();
                    Process proc = rt.exec(cmd);
                    exitVal = proc.waitFor();
                    System.out.println("Exit value: " + exitVal);
                    if (exitVal == 10) {
                        return false;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                File lpuFile = new File(files[i]);
                File logFile = new File(files[i].substring(0,
                        files[i].substring(0, files[i].lastIndexOf("\\")).lastIndexOf("\\") + 1)
                        + files[i].substring(files[i].lastIndexOf("\\") + 1, files[i].lastIndexOf("."))
                        + ".log");
                if (!lpuFile.exists()) {
                    logFile.delete();
                }
                File lpuFolder = new File(files[i].substring(0, files[i].lastIndexOf("\\")));
                if (lpuFolder.list().length == 0) {
                    lpuFolder.delete();
                }
            } // end for loop  
        }

    } catch (Exception e) {
        try {
            String content = e.getMessage();
            FileWriter fw = new FileWriter(log.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();
        } catch (Exception e1) {

        }

        return false;
    }
    return true;
}

From source file:cgs_lda_multicore.DataModel.DataPreparation.java

/**
 * Simply write the dataset from LDADataset to data file in this lib format.
 * //from w w  w . j a  v a 2 s  .c  o m
 * @param data
 * @param dir
 * @param dfile
 * @return
 * @throws Exception 
 */
public static boolean writeDataset(LDADataset data, String dir, String dfile) throws Exception {
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(dir + File.separator + dfile));
        writer.write(String.valueOf(data.M));
        writer.write("\n");
        for (int i = 0; i < data.M; i++) {
            for (int j = 0; j < data.docs[i].length; j++) {
                writer.write(data.localDict.getWord(data.docs[i].words[j]) + " ");
            }
            writer.write("\n");
        }
        writer.close();
    } catch (Exception e) {
        System.out.println("Error while writing dataset: " + e.getMessage());
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:apps.LuceneQuery.java

/**
 * Save positions, scores, etc information for a single retrieved documents.
 * /*  w w  w . j  a v  a 2 s .c  om*/
 * @param trecFile
 *          an object used to write to the output file.
 * @param topicId
 *          a question ID.
 * @param docId
 *          a document ID of the retrieved document.
 * @param docPos
 *          a position in the result set (the smaller the better).
 * @param score
 *          a score of the document in the result set.
 * @param runId
 *          a run ID.
 * @throws IOException
 */
private static void saveTrecOneEntry(BufferedWriter trecFile, String topicId, String docId, int docPos,
        float score, String runId) throws IOException {
    trecFile.write(String.format("%s\tQ0\t%s\t%d\t%f\t%s%s", topicId, docId, docPos, score, runId, NL));
}

From source file:oneDrive.OneDriveAPI.java

public static void createNewTokenFile(String accessToken) {
    BufferedWriter output = null;
    try {/* ww w .  ja  va  2s  .c  om*/
        File file = new File("SPsCredentials/OneDriveLogin.txt");
        output = new BufferedWriter(new FileWriter(file));
        output.write(accessToken);
        output.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Messenger.TorLib.java

public static void postToURL(String hostname, int port, String postKey, String data) throws IOException {
    Socket socket = TorSocket(hostname, port);
    SSLSocketFactory sslSf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    SSLSocket sslSocket = (SSLSocket) sslSf.createSocket(socket, null, socket.getPort(), false);
    sslSocket.setUseClientMode(true);//  w ww  . j a v a 2 s  . c  o m
    sslSocket.startHandshake();
    String path = "/" + postKey;
    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sslSocket.getOutputStream(), "UTF8"));
    wr.write("POST " + path + " HTTP/1.0\r\n");
    wr.write("Content-Length: " + data.length() + "\r\n");
    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
    wr.write("\r\n");

    wr.write(data);
    wr.flush();

    BufferedReader rd = new BufferedReader(new InputStreamReader(sslSocket.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
    wr.close();
    rd.close();
    sslSocket.close();
}

From source file:uk.ac.bbsrc.tgac.miso.integration.util.IntegrationUtils.java

/**
 * Sends a String message to a given host socket
 * // www  . ja va2s  .  c  o  m
 * @param socket
 *          of type Socket
 * @param query
 *          of type String
 * @return String
 * @throws IntegrationException
 *           when the socket couldn't be created
 */
public static String sendMessage(Socket socket, String query) throws IntegrationException {
    BufferedWriter wr = null;
    BufferedReader rd = null;
    try {
        wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));

        // Send data
        wr.write(query + "\r\n");
        wr.flush();

        // Get response
        rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        wr.close();
        rd.close();

        String dirty = sb.toString();
        StringBuilder response = new StringBuilder();
        int codePoint;
        int i = 0;
        while (i < dirty.length()) {
            codePoint = dirty.codePointAt(i);
            if ((codePoint == 0x9) || (codePoint == 0xA) || (codePoint == 0xD)
                    || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
                    || ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
                    || ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF))) {
                response.append(Character.toChars(codePoint));
            }
            i += Character.charCount(codePoint);
        }

        return response.toString().replace("\\\n", "").replace("\\\t", "");
    } catch (UnknownHostException e) {
        log.error("Cannot resolve host: " + socket.getInetAddress(), e);
        throw new IntegrationException(e.getMessage());
    } catch (IOException e) {
        log.error("Couldn't get I/O for the connection to: " + socket.getInetAddress(), e);
        throw new IntegrationException(e.getMessage());
    } finally {
        try {
            if (wr != null) {
                wr.close();
            }
            if (rd != null) {
                rd.close();
            }
        } catch (Throwable t) {
            log.error("close socket", t);
        }
    }
}

From source file:edu.iu.daal_kmeans.regroupallgather.KMUtil.java

/**
 * Generate centroids and upload to the cDir
 * /*from   ww  w  . ja v  a  2 s .c o  m*/
 * @param numCentroids
 * @param vectorSize
 * @param configuration
 * @param random
 * @param cenDir
 * @param fs
 * @throws IOException
 */
static void generateCentroids(int numCentroids, int vectorSize, Configuration configuration, Path cenDir,
        FileSystem fs) throws IOException {
    Random random = new Random();
    double[] data = null;
    if (fs.exists(cenDir))
        fs.delete(cenDir, true);
    if (!fs.mkdirs(cenDir)) {
        throw new IOException("Mkdirs failed to create " + cenDir.toString());
    }
    data = new double[numCentroids * vectorSize];
    for (int i = 0; i < data.length; i++) {
        data[i] = random.nextDouble() * 1000;
    }
    Path initClustersFile = new Path(cenDir, Constants.CENTROID_FILE_NAME);
    System.out.println("Generate centroid data." + initClustersFile.toString());
    FSDataOutputStream out = fs.create(initClustersFile, true);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
    for (int i = 0; i < data.length; i++) {
        if ((i % vectorSize) == (vectorSize - 1)) {
            bw.write(data[i] + "");
            bw.newLine();
        } else {
            bw.write(data[i] + " ");
        }
    }
    bw.flush();
    bw.close();
    System.out.println("Wrote centroids data to file");
}

From source file:mt.LengthDistribution.java

public static void WriteLengthdistroFile(ArrayList<File> AllMovies, XYSeries counterseries, int framenumber) {

    try {/*ww  w  .  j ava  2  s.  co m*/

        File ratesfile = new File(AllMovies.get(0).getParentFile() + "//" + "Length-Distribution At T " + " = "
                + framenumber + ".txt");

        if (framenumber == 0)
            ratesfile = new File(AllMovies.get(0).getParentFile() + "//" + "Mean Length-Distribution" + ".txt");

        FileWriter fw = new FileWriter(ratesfile);

        BufferedWriter bw = new BufferedWriter(fw);

        bw.write("\tLength(real units) \tCount\n");

        for (int index = 0; index < counterseries.getItems().size(); ++index) {

            double Count = counterseries.getX(index).doubleValue();
            double Length = counterseries.getY(index).doubleValue();

            bw.write("\t" + Length + "\t" + "\t" + Count + "\t" + "\n");

        }

        bw.close();
        fw.close();

    }

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

}

From source file:Main.java

public static void handleClientRequest(Socket socket) {
    try {/*from   w ww  .  j ava  2s  .c o  m*/
        BufferedReader socketReader = null;
        BufferedWriter socketWriter = null;
        socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        socketWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        String inMsg = null;
        while ((inMsg = socketReader.readLine()) != null) {
            System.out.println("Received from  client: " + inMsg);

            String outMsg = inMsg;
            socketWriter.write(outMsg);
            socketWriter.write("\n");
            socketWriter.flush();
        }
        socket.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}