@Override public void actionPerformed(ActionEvent e) { if (td.getTabCount() > 0) { JFileChooser f = new JFileChooser(); f.setFileFilter(new MyFileFilter()); int choose = f.showSaveDialog(getContentPane()); if (choose == JFileChooser.APPROVE_OPTION) { BufferedWriter brw = null; try { File file = f.getSelectedFile(); brw = new BufferedWriter(new FileWriter(file)); int i = td.getSelectedIndex(); TextDocument ta = (TextDocument) td.getComponentAt(i); ta.write(brw); ta.fileName = file.getName(); // 將檔案名稱更新為存檔的名稱 td.setTitleAt(td.getSelectedIndex(), ta.fileName); ta.file = file; ta.save = true; // 設定已儲存 System.out.println("Save as pass!"); td.setTitleAt(i, ta.fileName); // 更新標題名稱 } catch (Exception exc) { exc.printStackTrace(); } finally { try { brw.close(); } catch (Exception ecx) { ecx.printStackTrace(); } } } } else { JOptionPane.showMessageDialog(null, "沒有檔案可以儲存!"); } }
public void save(int i) { BufferedWriter brw = null; TextDocument ta = (TextDocument) td.getComponentAt(i); try { File file = null; if (ta.file == null) { // 判斷是否為新黨案,是的話使用預設路徑儲存 file = new File(default_auto_save_path + "\\" + ta.fileName); } else { file = ta.file; // 儲存於原檔案 } brw = new BufferedWriter(new FileWriter(file)); ta.write(brw); ta.save = true; td.setTitleAt(i, ta.fileName); // 更新標題名稱 } catch (FileNotFoundException ffe) { // 若預設路徑不存在,則跳出警告 JOptionPane.showMessageDialog( null, ta.fileName + "儲存失敗!\n請檢查Default AutoSave-Path是否存在,或是權限不足.", "Save error", JOptionPane.ERROR_MESSAGE); } catch (Exception exc) { exc.printStackTrace(); } finally { try { brw.close(); } catch (Exception exc) { } } }
public static BufferedReader getBufferedReader(String fileName, String context) throws Throwable { includeNames.add(fileName); if (fileName.startsWith("http://") || fileName.startsWith("https://")) { String hashedFileName = MD5(fileName); File remoteCacheFile = new File(cacheDirectoryName + File.separator + hashedFileName); if (remoteCacheFile.exists()) { return new BufferedReader(new FileReader(remoteCacheFile)); } else { URL input = new URL(fileName); BufferedReader remoteReader = new BufferedReader(new InputStreamReader(input.openStream())); BufferedWriter cacheWriter = new BufferedWriter( new FileWriter(cacheDirectoryName + File.separator + hashedFileName)); String tmpLine = remoteReader.readLine(); while (tmpLine != null) { cacheWriter.write(tmpLine); cacheWriter.newLine(); tmpLine = remoteReader.readLine(); } cacheWriter.close(); return new BufferedReader( new FileReader(cacheDirectoryName + File.separator + hashedFileName)); } } else { if (fileName.startsWith(context)) { return new BufferedReader(new FileReader(fileName)); } else { return new BufferedReader(new FileReader(context + fileName)); } } }
public static void main(String[] args) throws Exception { Reader trainingFile = null; // Process arguments int restArgs = commandOptions.processOptions(args); // Check arguments if (restArgs != args.length) { commandOptions.printUsage(true); throw new IllegalArgumentException("Unexpected arg " + args[restArgs]); } if (trainFileOption.value == null) { commandOptions.printUsage(true); throw new IllegalArgumentException("Expected --train-file FILE"); } if (modelFileOption.value == null) { commandOptions.printUsage(true); throw new IllegalArgumentException("Expected --model-file FILE"); } // Get the CRF structure specification. ZipFile zipFile = new ZipFile(modelFileOption.value); ZipEntry zipEntry = zipFile.getEntry("crf-info.xml"); CRFInfo crfInfo = new CRFInfo(zipFile.getInputStream(zipEntry)); StringBuffer crfInfoBuffer = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(zipEntry))); String line; while ((line = reader.readLine()) != null) { crfInfoBuffer.append(line).append('\n'); } reader.close(); // Create the CRF, and train it. CRF4 crf = createCRF(trainFileOption.value, crfInfo); // Create a new zip file for our output. This will overwrite // the file we used for input. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(modelFileOption.value)); // Copy the CRF info xml to the output zip file. zos.putNextEntry(new ZipEntry("crf-info.xml")); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zos)); writer.write(crfInfoBuffer.toString()); writer.flush(); zos.closeEntry(); // Save the CRF classifier model to the output zip file. zos.putNextEntry(new ZipEntry("crf-model.ser")); ObjectOutputStream oos = new ObjectOutputStream(zos); oos.writeObject(crf); oos.flush(); zos.closeEntry(); zos.close(); }
public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset)); if (outputFile != null) { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile), charset)); } String line; while ((line = br.readLine()) != null) { filePosition += line.length() + 2; line = line.trim(); if (!line.startsWith("#")) { String[] sides = split(line); if ((sides != null) && !sides[0].equals("key")) { if (searchPHI) { // Search the decrypted PHI for the searchText sides[0] = decrypt(sides[0]); if (sides[0].indexOf(searchText) != -1) { output(sides[0] + " = " + sides[1] + "\n"); } } else { // Search the trial ID for the searchText if (sides[1].indexOf(searchText) != -1) { sides[0] = decrypt(sides[0]); output(sides[0] + " = " + sides[1] + "\n"); } } } } } br.close(); if (bw != null) { bw.flush(); bw.close(); } } catch (Exception e) { append("\n\n" + e.getClass().getName() + ": " + e.getMessage() + "\n"); } append("\nDone.\n"); setMessage("Ready..."); }
private void output(String string) throws Exception { if (bw != null) bw.write(string); else append(string); // Set the fraction done. long pct = (filePosition * 100) / fileLength; if (pct != percentDone) { percentDone = pct; setMessage("Working... (" + pct + "%)"); } }
public static void main(String[] args) throws Throwable { long startTime = System.nanoTime(); if (args.length < 2) { System.out.println("Not enough arguments"); System.exit(1); } String inputFileName = args[args.length - 2]; String outputFileName = args[args.length - 1]; includeNames.add(inputFileName); new File(cacheDirectoryName).mkdir(); BufferedWriter out = new BufferedWriter(new FileWriter(outputFileName)); mainContext = contextFromFilename(inputFileName); processFile(getBufferedReader(inputFileName, mainContext), out, mainContext); out.close(); System.out.println("Elapsed time: " + ((System.nanoTime() - startTime) / 1000000.0f) + " ms."); }
public static void processFile(BufferedReader in, BufferedWriter out, String context) throws Throwable { String tmpLine = in.readLine(); while (tmpLine != null) { // check to see if the line matches the import directive Matcher matcher = includePattern.matcher(tmpLine); // if it doesn't match, just output the line, followed by a newline if (!matcher.matches()) { out.write(tmpLine); out.newLine(); // if it matches } else { String fileName = matcher.group(1); // check if it has already been included if (!includeNames.contains(fileName)) { // if it hasn't, process it String newContext = contextFromFilename(fileName); processFile(getBufferedReader(fileName, context), out, context + newContext); } } tmpLine = in.readLine(); } in.close(); }
public static void main(String args[]) { // System.out.println( "Starting Jinan..." ); LineNumberReader in = null; BufferedWriter out = null; try { in = new LineNumberReader(new FileReader("Jinan.txt")); out = new BufferedWriter(new FileWriter("format.txt")); } catch (IOException e) { System.out.println(e); System.exit(-1); } String line; // 下面的表达式为:以K或没有K开头,接着是若干数字,然后是“路”字。 // 注意()表示group,以便提取其中的信息 Pattern p = Pattern.compile("^([Kk]?\\d+路).*$"); Matcher m; boolean flag = true; try { while (true) { line = in.readLine(); if (line == null) break; else if (line.equals("")) continue; m = p.matcher(line); if (flag != m.find()) // 这里必须先用find()触发匹配过程,才能开始使用 throw new RuntimeException("File format error: " + in.getLineNumber()); if (flag == true) { out.write(m.group(1)); // group(0) is the entire string, group(1) is the right one out.write(" "); flag = false; } else { out.write(line); out.newLine(); flag = true; } } in.close(); out.close(); } catch (IOException e) { System.out.println(e); System.exit(-1); } }
/** * Writes the data to the given writer. * * @param aDataSet the project to write the settings for, cannot be <code>null</code> ; * @param aWriter the writer to write the data to, cannot be <code>null</code>. * @throws IOException in case of I/O problems. */ public static void write(final StubDataSet aDataSet, final Writer aWriter) throws IOException { final BufferedWriter bw = new BufferedWriter(aWriter); final AcquisitionResult capturedData = aDataSet.getCapturedData(); final Cursor[] cursors = aDataSet.getCursors(); final boolean cursorsEnabled = aDataSet.isCursorsEnabled(); try { final int[] values = capturedData.getValues(); final long[] timestamps = capturedData.getTimestamps(); bw.write(";Size: "); bw.write(Integer.toString(values.length)); bw.newLine(); bw.write(";Rate: "); bw.write(Integer.toString(capturedData.getSampleRate())); bw.newLine(); bw.write(";Channels: "); bw.write(Integer.toString(capturedData.getChannels())); bw.newLine(); bw.write(";EnabledChannels: "); bw.write(Integer.toString(capturedData.getEnabledChannels())); bw.newLine(); if (capturedData.hasTriggerData()) { bw.write(";TriggerPosition: "); bw.write(Long.toString(capturedData.getTriggerPosition())); bw.newLine(); } bw.write(";Compressed: "); bw.write(Boolean.toString(true)); bw.newLine(); bw.write(";AbsoluteLength: "); bw.write(Long.toString(capturedData.getAbsoluteLength())); bw.newLine(); bw.write(";CursorEnabled: "); bw.write(Boolean.toString(cursorsEnabled)); bw.newLine(); for (int i = 0; cursorsEnabled && (i < cursors.length); i++) { if (cursors[i].isDefined()) { bw.write(String.format(";Cursor%d: ", Integer.valueOf(i))); bw.write(Long.toString(cursors[i].getTimestamp())); bw.newLine(); } } for (int i = 0; i < values.length; i++) { bw.write(formatSample(values[i], timestamps[i])); bw.newLine(); } } finally { bw.flush(); } }