📄 extfile.java
字号:
} } } finally { try { in1.close(); in2.close(); } catch(NullPointerException npe) { //don't care } } return result; }/*****************************************************************************//* *//* Method: compareFile *//* Description: Optionally performs a lexicographic sort, then compares this *//* file to another file, optionally saving this file if the *//* comaparison fails (overloaded) *//* Parameters: model - File to compare this file against *//* sort - if true first perform sort, if false do not sort *//* Returns: true if files compared OK, false otherwise *//* *//*****************************************************************************/public boolean compareFile(File model, boolean sort) throws IOException { if (!sort) { // do not sort return compareFile(model); } ExtFile sortedSource = this.sort(); ExtFile sortedModel = new ExtFile(model).sort(); boolean result = sortedSource.compareFile(sortedModel); if (!result && saveFailures) { File saveName = new File(model.getParent(), this.getName()+".fail"); this.captureFile(saveName); } sortedSource.delete(); sortedModel.delete(); return result; }/*****************************************************************************//* *//* Method: compareFile *//* Description: Compares this file to another file, optionally saving this *//* file if the comaparison fails (overloaded) *//* Parameters: model - filename of file to compare this file against *//* Returns: true if files compared OK, false otherwise *//* *//*****************************************************************************/public boolean compareFile(String model) throws IOException { File modelFile = new File(model); return compareFile(modelFile); }/*****************************************************************************//* *//* Method: compareFile *//* Description: Optionally performs a lexicographic sort, then compares this *//* file to another file, optionally saving this file if the *//* comaparison fails (overloaded) *//* Parameters: model - filename of file to compare this file against *//* sort - if true first perform sort, if false do not sort *//* Returns: true if files compared OK, false otherwise *//* *//*****************************************************************************/public boolean compareFile(String model, boolean sort) throws IOException { File modelFile = new File(model); return compareFile(modelFile, sort); }/*****************************************************************************//* *//* Method: isSaveFailures *//* Description: Access method (get) for saveFailures field. saveFailures *//* specifies if a file should be saved when a compare fails *//* *//*****************************************************************************/public boolean isSaveFailures() { return saveFailures;}/*****************************************************************************//* *//* Method: isSaveFailures *//* Description: Access method (set) for saveFailures field. saveFailures *//* specifies if a file should be saved when a compare fails *//* *//*****************************************************************************/public void setSaveFailures(boolean newSaveFailures) { saveFailures = newSaveFailures;}/*****************************************************************************//* *//* Method: sort *//* Description: Performs a lexicographic sort on this file. The sort is *//* performed on a line by line basis *//* Parameters: none *//* Returns: an ExtFile which represents the sorted version of this ExtFile *//* *//*****************************************************************************/public ExtFile sort() throws IOException { /* Read each line of file and store as String Array */ Vector unsorted = new Vector(); BufferedReader in = null; try { in = new BufferedReader(new FileReader(this)); while (true) { String line = in.readLine(); if (line == null) { break; } unsorted.add(line); } } finally { try { in.close(); } catch(NullPointerException npe) {} //don't care } Object[] foo = new String[0]; String[] sort = (String[]) unsorted.toArray(foo); /* Lexicographically sort array */ for (int i=sort.length; --i >= 0; ) { for (int j=0; j < i; j++) { if (sort[j].compareTo(sort[j+1]) >= 0) { String temp = sort[j]; sort[j] = sort[j+1]; sort[j+1] = temp; } } } /* Write sorted file back out */ BufferedWriter out = null; ExtFile sortedFile = new ExtFile(this.getPath()+".sorted"); try { out = new BufferedWriter(new FileWriter(sortedFile)); for (int i=0; i < sort.length; i++) { out.write(sort[i]); out.newLine(); } out.flush(); } finally { try { out.close(); } catch(NullPointerException npe) {} // don't care } return sortedFile; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -