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) { } } }
@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 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[]) { // 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); } }
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..."); }
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."); }