Example usage for java.io BufferedWriter newLine

List of usage examples for java.io BufferedWriter newLine

Introduction

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

Prototype

public void newLine() throws IOException 

Source Link

Document

Writes a line separator.

Usage

From source file:su90.etl.controller.SlaveController.java

private void _do_write_result() {

    String outputfilename = FILENAME.substring(0, FILENAME.indexOf(".csv")) + ".json";

    ObjectMapper mapper = new ObjectMapper();
    BufferedWriter writer = null;

    try {/* w  w w . j  a v a 2  s  . c  om*/
        writer = new BufferedWriter(
                new FileWriter(OUTPUTPATHSTR + System.getProperty("file.separator") + outputfilename));
        for (Map.Entry<Integer, Result> entry : results.entrySet()) {
            try {
                Result tmpresult = entry.getValue();
                if (tmpresult.getImps() == 0)
                    tmpresult.setImps(null);
                if (tmpresult.getClicks() == 0)
                    tmpresult.setClicks(null);
                String tmpstr = mapper.writeValueAsString(entry.getValue());
                writer.write(tmpstr);
                writer.newLine();
            } catch (Exception ex) {
                Logger.getLogger(SlaveController.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    } catch (IOException e) {
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException e) {
        }
    }
}

From source file:analytics.storage.store2csv.java

private void createHeaders(BufferedWriter writer, String metricName, String element) {

    try {/*from   ww w  .  j a  va 2s . c o  m*/
        writer.append(element);
        writer.append(',');
        writer.append(metricName);
        writer.newLine();
        // writer.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    // finally {
    // try {
    // if (writer != null)
    // writer.close();
    // } catch (IOException ex) {
    // ex.printStackTrace();
    // }
    // }

}

From source file:com.google.android.apps.location.gps.gnsslogger.FileLogger.java

/**
 * Start a new file logging process.// w  w w .  ja  v a  2s .c o m
 */
public void startNewLog() {
    synchronized (mFileLock) {
        File baseDirectory;
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            baseDirectory = new File(Environment.getExternalStorageDirectory(), FILE_PREFIX);
            baseDirectory.mkdirs();
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            logError("Cannot write to external storage.");
            return;
        } else {
            logError("Cannot read external storage.");
            return;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyy_MM_dd_HH_mm_ss");
        Date now = new Date();
        String fileName = String.format("%s_%s.txt", FILE_PREFIX, formatter.format(now));
        File currentFile = new File(baseDirectory, fileName);
        String currentFilePath = currentFile.getAbsolutePath();
        BufferedWriter currentFileWriter;
        try {
            currentFileWriter = new BufferedWriter(new FileWriter(currentFile));
        } catch (IOException e) {
            logException("Could not open file: " + currentFilePath, e);
            return;
        }

        // initialize the contents of the file
        try {
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.write("Header Description:");
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.write(VERSION_TAG);
            String manufacturer = Build.MANUFACTURER;
            String model = Build.MODEL;
            String fileVersion = mContext.getString(R.string.app_version) + " Platform: "
                    + Build.VERSION.RELEASE + " " + "Manufacturer: " + manufacturer + " " + "Model: " + model;
            currentFileWriter.write(fileVersion);
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter
                    .write("Raw,ElapsedRealtimeMillis,TimeNanos,LeapSecond,TimeUncertaintyNanos,FullBiasNanos,"
                            + "BiasNanos,BiasUncertaintyNanos,DriftNanosPerSecond,DriftUncertaintyNanosPerSecond,"
                            + "HardwareClockDiscontinuityCount,Svid,TimeOffsetNanos,State,ReceivedSvTimeNanos,"
                            + "ReceivedSvTimeUncertaintyNanos,Cn0DbHz,PseudorangeRateMetersPerSecond,"
                            + "PseudorangeRateUncertaintyMetersPerSecond,"
                            + "AccumulatedDeltaRangeState,AccumulatedDeltaRangeMeters,"
                            + "AccumulatedDeltaRangeUncertaintyMeters,CarrierFrequencyHz,CarrierCycles,"
                            + "CarrierPhase,CarrierPhaseUncertainty,MultipathIndicator,SnrInDb,"
                            + "ConstellationType,AgcDb,CarrierFrequencyHz");
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.write("Fix,Provider,Latitude,Longitude,Altitude,Speed,Accuracy,(UTC)TimeInMs");
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.write("Nav,Svid,Type,Status,MessageId,Sub-messageId,Data(Bytes)");
            currentFileWriter.newLine();
            currentFileWriter.write(COMMENT_START);
            currentFileWriter.newLine();
        } catch (IOException e) {
            logException("Count not initialize file: " + currentFilePath, e);
            return;
        }

        if (mFileWriter != null) {
            try {
                mFileWriter.close();
            } catch (IOException e) {
                logException("Unable to close all file streams.", e);
                return;
            }
        }

        mFile = currentFile;
        mFileWriter = currentFileWriter;
        Toast.makeText(mContext, "File opened: " + currentFilePath, Toast.LENGTH_SHORT).show();

        // To make sure that files do not fill up the external storage:
        // - Remove all empty files
        FileFilter filter = new FileToDeleteFilter(mFile);
        for (File existingFile : baseDirectory.listFiles(filter)) {
            existingFile.delete();
        }
        // - Trim the number of files with data
        File[] existingFiles = baseDirectory.listFiles();
        int filesToDeleteCount = existingFiles.length - MAX_FILES_STORED;
        if (filesToDeleteCount > 0) {
            Arrays.sort(existingFiles);
            for (int i = 0; i < filesToDeleteCount; ++i) {
                existingFiles[i].delete();
            }
        }
    }
}

From source file:gate.creole.gazetteer.LinearDefinition.java

/**
 * Stores this to a definition file.//from  w w w . ja v  a 2 s .c o  m
 */
public void store() throws ResourceInstantiationException {
    if (null == url) {
        throw new ResourceInstantiationException("URL not set.(null)");
    }
    try {

        File fileo = Files.fileFromURL(url);
        fileo.delete();
        BufferedWriter defWriter = new BufferedWriter(new FileWriter(fileo));
        Iterator<LinearNode> inodes = nodes.iterator();
        while (inodes.hasNext()) {
            defWriter.write(inodes.next().toString());
            defWriter.newLine();
        }
        defWriter.close();
        isModified = false;
    } catch (Exception x) {
        throw new ResourceInstantiationException(x);
    }

}

From source file:com.taobao.diamond.client.processor.ServerAddressProcessor.java

void storeServerAddressesToLocal() {
    List<String> domainNameList = new ArrayList<String>(diamondConfigure.getDomainNameList());
    PrintWriter printWriter = null;
    BufferedWriter bufferedWriter = null;
    try {//from   www  .j  a v a2  s  .co m
        File serverAddressFile = new File(
                generateLocalFilePath(this.diamondConfigure.getFilePath(), "ServerAddress"));
        if (!serverAddressFile.exists()) {
            serverAddressFile.createNewFile();
        }
        printWriter = new PrintWriter(serverAddressFile);
        bufferedWriter = new BufferedWriter(printWriter);
        for (String serveraddress : domainNameList) {
            bufferedWriter.write(serveraddress);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
    } catch (Exception e) {
        log.error("", e);
    } finally {
        if (bufferedWriter != null) {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                // ignore
            }
        }
        if (printWriter != null) {
            printWriter.close();
        }
    }
}

From source file:com.ruforhire.utils.Stopwords.java

/**
 * Writes the current stopwords to the given writer. The writer is closed
 * automatically.//from ww w .ja  v a 2 s.com
 *
 * @param writer the writer to get the stopwords from
 * @throws Exception if writing fails
 */
public void write(BufferedWriter writer) throws Exception {
    Enumeration enm;

    // header
    writer.write("# generated " + new Date());
    writer.newLine();

    enm = elements();

    while (enm.hasMoreElements()) {
        writer.write(enm.nextElement().toString());
        writer.newLine();
    }

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

From source file:cn.leancloud.diamond.client.processor.ServerAddressProcessor.java

void storeServerAddressesToLocal() {
    List<String> domainNameList = new ArrayList<String>(diamondConfigure.getDomainNameList());
    PrintWriter printWriter = null;
    BufferedWriter bufferedWriter = null;
    try {/*ww w  .j a va  2 s .com*/
        File serverAddressFile = new File(
                generateLocalFilePath(this.diamondConfigure.getFilePath(), "ServerAddress"));
        if (!serverAddressFile.exists()) {
            serverAddressFile.createNewFile();
        }
        printWriter = new PrintWriter(serverAddressFile);
        bufferedWriter = new BufferedWriter(printWriter);
        for (String serveraddress : domainNameList) {
            bufferedWriter.write(serveraddress);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
    } catch (Exception e) {
        log.error("??", e);
    } finally {
        if (bufferedWriter != null) {
            try {
                bufferedWriter.close();
            } catch (IOException e) {
                // ignore
            }
        }
        if (printWriter != null) {
            printWriter.close();
        }
    }
}

From source file:com.ichi2.libanki.Utils.java

private static void printJSONObject(JSONObject jsonObject, String indentation, BufferedWriter buff) {
    try {//from   ww  w.j  a  v a  2s  .c  o m
        @SuppressWarnings("unchecked")
        Iterator<String> keys = (Iterator<String>) jsonObject.keys();
        TreeSet<String> orderedKeysSet = new TreeSet<String>();
        while (keys.hasNext()) {
            orderedKeysSet.add(keys.next());
        }

        Iterator<String> orderedKeys = orderedKeysSet.iterator();
        while (orderedKeys.hasNext()) {
            String key = orderedKeys.next();

            try {
                Object value = jsonObject.get(key);
                if (value instanceof JSONObject) {
                    if (buff != null) {
                        buff.write(indentation + " " + key + " : ");
                        buff.newLine();
                    }
                    Timber.i("  " + indentation + key + " : ");
                    printJSONObject((JSONObject) value, indentation + "-", buff);
                } else {
                    if (buff != null) {
                        buff.write(indentation + " " + key + " = " + jsonObject.get(key).toString());
                        buff.newLine();
                    }
                    Timber.i("  " + indentation + key + " = " + jsonObject.get(key).toString());
                }
            } catch (JSONException e) {
                Timber.e(e, "printJSONObject : JSONException");
            }
        }
    } catch (IOException e1) {
        Timber.e(e1, "printJSONObject : IOException");
    }
}

From source file:matrix.CreateUrlMatrix.java

public void urlMatrix()
        throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException {

    CosSim cossim = new CosSim();
    JSONParser jParser = new JSONParser();
    BufferedReader in = new BufferedReader(new InputStreamReader(
            new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweetsUrls.json"), "ISO-8859-9"));
    JSONArray a = (JSONArray) jParser.parse(in);
    File fout = new File("/Users/nSabri/Desktop/urlMatris.csv");
    FileOutputStream fos = new FileOutputStream(fout);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    for (int i = 0; i < a.size(); i++) {

        for (int j = 0; j < a.size(); j++) {

            JSONObject tweet1 = (JSONObject) a.get(i);
            JSONObject tweet2 = (JSONObject) a.get(j);
            String tweetUrl1 = tweet1.get("title").toString() + tweet1.get("meta").toString();
            System.out.println(tweetUrl1);
            String tweetUrl2 = tweet2.get("title").toString() + tweet1.get("meta").toString();
            System.out.println(tweetUrl2);

            double CosSimValue = cossim.Cosine_Similarity_Score(tweetUrl1, tweetUrl2);
            CosSimValue = Double.parseDouble(new DecimalFormat("##.###").format(CosSimValue));
            bw.write(Double.toString(CosSimValue) + ", ");

        }//  w w w. j  a  v  a 2 s  .c o  m
        bw.newLine();
    }
    bw.close();

}