public static void writeFileByLine(String filePath, List<String> linesToWrite) throws IOException { LOG.debug("For CGroups - writing {} to {} ", linesToWrite, filePath); File file = new File(filePath); if (!file.exists()) { LOG.error("{} does not exist", filePath); return; } try (FileWriter writer = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(writer)) { for (String string : linesToWrite) { bw.write(string); bw.newLine(); bw.flush(); } } }
/** * 将Map写到文件 * @param map * @throws IOException */ public static void write(Map map,String file) throws IOException { BufferedWriter bout = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(file), "UTF-8")); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry)iterator.next(); String key = entry.getKey().toString(); String v = entry.getValue().toString(); bout.append(key); bout.append("\t"); bout.append(v); bout.newLine(); } bout.close(); }
String encoding = "UTF-8"; int maxlines = 100; BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new InputStreamReader(new FileInputStream("/bigfile.txt"), encoding)); int count = 0; for (String line; (line = reader.readLine()) != null;) { if (count++ % maxlines == 0) { close(writer); writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/smallfile" + (count / maxlines) + ".txt"), encoding)); } writer.write(line); writer.newLine(); } } finally { close(writer); close(reader); }
public Writer writeTo(Writer out) throws IOException { BufferedWriter bw = new BufferedWriter(out); String line; BooleanClosureWrapper bcw = new BooleanClosureWrapper(closure); while ((line = br.readLine()) != null) { if (bcw.call(line)) { bw.write(line); bw.newLine(); } } bw.flush(); return out; }
/** * 标准化评测分词器 * * @param segment 分词器 * @param outputPath 分词预测输出文件 * @param goldFile 测试集segmented file * @param dictPath 训练集单词列表 * @return 一个储存准确率的结构 * @throws IOException */ public static CWSEvaluator.Result evaluate(Segment segment, String outputPath, String goldFile, String dictPath) throws IOException { IOUtil.LineIterator lineIterator = new IOUtil.LineIterator(goldFile); BufferedWriter bw = IOUtil.newBufferedWriter(outputPath); for (String line : lineIterator) { List<Term> termList = segment.seg(line.replaceAll("\\s+", "")); // 一些testFile与goldFile根本不匹配,比如MSR的testFile有些行缺少单词,所以用goldFile去掉空格代替 int i = 0; for (Term term : termList) { bw.write(term.word); if (++i != termList.size()) bw.write(" "); } bw.newLine(); } bw.close(); CWSEvaluator.Result result = CWSEvaluator.evaluate(goldFile, outputPath, dictPath); return result; }
public void appendLog(String text) { File logFile = new File("sdcard/log.file"); if (!logFile.exists()) { try { logFile.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } try { //BufferedWriter for performance, true to set append to file flag BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); buf.append(text); buf.newLine(); buf.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
BufferedWriter bufOut = new BufferedWriter( new OutputStreamWriter( out ) ); while ( true ) { try { String msg = bufIn.readLine(); System.out.println( "Received: " + msg ); bufOut.write( msg ); bufOut.newLine(); //HERE!!!!!! bufOut.flush(); } catch ( IOException e ) {
private void updateIndex(long txId) { LOG.debug("Starting index update."); final Path tmpPath = tmpFilePath(indexFilePath.toString()); try (FSDataOutputStream out = this.options.fs.create(tmpPath, true); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out))) { TxnRecord txnRecord = new TxnRecord(txId, options.currentFile.toString(), this.options.getCurrentOffset()); bw.write(txnRecord.toString()); bw.newLine(); bw.flush(); out.close(); /* In non error scenarios, for the Azure Data Lake Store File System (adl://), the output stream must be closed before the file associated with it is deleted. For ADLFS deleting the file also removes any handles to the file, hence out.close() will fail. */ /* * Delete the current index file and rename the tmp file to atomically * replace the index file. Orphan .tmp files are handled in getTxnRecord. */ options.fs.delete(this.indexFilePath, false); options.fs.rename(tmpPath, this.indexFilePath); lastSeenTxn = txnRecord; LOG.debug("updateIndex updated lastSeenTxn to [{}]", this.lastSeenTxn); } catch (IOException e) { LOG.warn("Begin commit failed due to IOException. Failing batch", e); throw new FailedException(e); } }
for (int j = 0; j < 20; j++) { double value = rand.nextDouble(); buildData.write("2016010101"); buildData.write('\t'); buildData.write(Integer.toString(sequenceNumber)); // dimension with unique numbers for ingesting raw data buildData.write('\t'); buildData.write(Integer.toString(product)); // product dimension buildData.write('\t'); buildData.write(Double.toString(value)); buildData.newLine(); sketch.update(value); sequenceNumber++; sketchData.write('\t'); sketchData.write(StringUtils.encodeBase64String(sketch.toByteArray(true))); sketchData.newLine(); buildData.close(); sketchData.close();
public static void main(String[] args) throws Exception { String nodeValue = "i am mostafa"; // you want to output to file // BufferedWriter writer = new BufferedWriter(new FileWriter(file3, true)); // but let's print to console while debugging BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); String[] words = nodeValue.split(" "); for (String word: words) { writer.write(word); writer.newLine(); } writer.close(); }
try { Socket s = new Socket("localhost", 60010); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(s.getOutputStream())); out.write("Hello World!"); out.newLine(); out.flush();
private static void writePoint(double[] coordinates, StringBuilder buffer, BufferedWriter out) throws IOException { buffer.setLength(0); // write coordinates for (int j = 0; j < coordinates.length; j++) { buffer.append(FORMAT.format(coordinates[j])); if (j < coordinates.length - 1) { buffer.append(DELIMITER); } } out.write(buffer.toString()); out.newLine(); }
public void stripDuplicatesFromFile(String filename) { BufferedReader reader = new BufferedReader(new FileReader(filename)); Set<String> lines = new HashSet<String>(10000); // maybe should be bigger String line; while ((line = reader.readLine()) != null) { lines.add(line); } reader.close(); BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); for (String unique : lines) { writer.write(unique); writer.newLine(); } writer.close(); }
private static void writePoint(double[] data, StringBuilder buffer, BufferedWriter out) throws IOException { buffer.setLength(0); // write coordinates for (int j = 0; j < data.length; j++) { buffer.append(FORMAT.format(data[j])); if (j < data.length - 1) { buffer.append(DELIMITER); } } out.write(buffer.toString()); out.newLine(); } }
public static void storeClassesAndMethods(List<ClassAndMethods> cams, File file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter out = new BufferedWriter(fw); for (ClassAndMethods entry : cams) { out.append(ClassAndMethodDetails.convertForStoring(entry)); out.newLine(); } out.flush(); out.close(); }
@Override protected void convertCorpus(Sentence sentence, BufferedWriter bw) throws IOException { List<String[]> collector = Utility.convertSentenceToNER(sentence, tagSet); for (String[] tuple : collector) { bw.write(tuple[0]); bw.write('\t'); bw.write(tuple[1]); bw.write('\t'); bw.write(tuple[2]); bw.newLine(); } }
public static void storeClassesAndVariables(List<ClassAndVariables> cams, File file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter out = new BufferedWriter(fw); for (ClassAndVariables entry : cams) { out.append(ClassAndVariableDetails.convertForStoring(entry)); out.newLine(); } out.flush(); out.close(); } }
private static void writeCenter(long id, double[] coordinates, StringBuilder buffer, BufferedWriter out) throws IOException { buffer.setLength(0); // write id buffer.append(id); buffer.append(DELIMITER); // write coordinates for (int j = 0; j < coordinates.length; j++) { buffer.append(FORMAT.format(coordinates[j])); if (j < coordinates.length - 1) { buffer.append(DELIMITER); } } out.write(buffer.toString()); out.newLine(); } }