public TextFile openDBforFile(String dbName, String fileName, String tablePrefix) { perFile = new perFileStruc(); cliDB = new sqlSolver(); initialScript(dbName, tablePrefix); if (checkMisprog(cliDB == null, "cliDB")) return null; if (checkMisprog(cached == null, "cached")) return null; cliDB.openScript(); out( "INSERT INTO " + cached.tabPrefix + "_files VALUES (" + cached.fileID + ", '" + cliDB.escapeString(DateFormat.getTodayStr()) + "', '" + fileName + "');"); log.dbg(2, "processOneFile", "start parsing [" + fileName + "]"); TextFile tf = new TextFile(); if (!tf.fopen(fileName, "rb")) // mode "rb" to be able to get the InputStream! { log.err("processOneFile", "file to parse [" + fileName + "] cannot be openned!"); return null; } return tf; }
public void testRemove() throws IOException { int i = 0; TextFile tf = new TextFile(TEST_FILE_NAME); for (String name : tf) { System.out.println("::: " + name); assertEquals(names.get(i++), name); } // testing remove() for (Iterator<String> it = tf.iterator(); it.hasNext(); ) { String name = it.next(); if ("Dirk".equals(name)) { it.remove(); System.out.println("removing dirk"); } } names.remove("Dirk"); i = 0; for (String name : tf) { System.out.println("::: " + name); assertEquals(names.get(i++), name); } }
public void testBadRemove() throws IOException { TextFile tf = new TextFile(TEST_FILE_NAME); Iterator<String> it = tf.iterator(); try { it.remove(); fail("Expected an IllegalStateException"); } catch (IllegalStateException success) { } }
public void testAllRemove() throws IOException { TextFile tf = new TextFile(TEST_FILE_NAME); // testing remove() on first element Iterator<String> it = tf.iterator(); while (it.hasNext()) { it.next(); it.remove(); } it = tf.iterator(); assertFalse(it.hasNext()); }
public void addFileSource(String path, String cnt, boolean omitIfExists) throws Exception { File tmp = File.createTempFile("tmp", ".tmp"); tmp.deleteOnExit(); TextFile.stringToFile(cnt, tmp.getAbsolutePath()); addFileName(path, tmp.getAbsolutePath(), omitIfExists); tmp.delete(); }
public void testFirstRemove() throws IOException { int i = 0; TextFile tf = new TextFile(TEST_FILE_NAME); // testing remove() on first element Iterator<String> it = tf.iterator(); it.next(); it.remove(); names.remove("John"); i = 0; for (String name : tf) { System.out.println("::: " + name); assertEquals(names.get(i++), name); } }
public void testConcurrentModificationException() throws IOException { TextFile tf = new TextFile(TEST_FILE_NAME); Iterator<String> broken = tf.iterator(); for (Iterator<String> it = tf.iterator(); it.hasNext(); ) { String name = it.next(); if ("Dirk".equals(name)) { it.remove(); } } try { broken.next(); fail("Expected a ConcurrentModificationException"); } catch (ConcurrentModificationException success) { } }
public static int calcHighestId() { // goes through each line if the csv file, keeping track of the highest // id it finds. returns the highest id found in the csv file. int id; int highestId = 0; TextFile file; file = new TextFile(filepath, "r"); file.openFile(); do { if (file.readChar() == ',') { do { id = file.readInt(); } while (id == -1); if (highestId < id) { highestId = id; } file.clearRestOfLine(); } } while(file.endOfFile() == false); file.closeFile(); return highestId; }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("Usage: java E17_JCommentExtractor file"); System.exit(0); } String src = TextFile.read(args[0]); Pattern p = Pattern.compile(CMNT_EXT_REGEX); Matcher m = p.matcher(src); while (m.find()) System.out.println(m.group(1) != null ? m.group(1) : m.group(2)); }
@Test public void parseTextFileUsingBufferedReader( @Mocked final BufferedReader reader, @Mocked FileReader fileReader) throws Exception { new NonStrictExpectations() { { reader.readLine(); returns("line1", "another,line", null); } }; TextFile textFile = new TextFile("file"); List<String[]> result = textFile.parse(); assertResultFromTextFileParsing(result); new Verifications() { { reader.close(); } }; }
@Test public void parseTextFileUsingInterface(@Mocked final TextReader reader) throws Exception { new NonStrictExpectations() { { reader.readLine(); returns("line1", "another,line", null); } }; TextFile textFile = new TextFile(reader, 100); List<String[]> result = textFile.parse(); assertResultFromTextFileParsing(result); new VerificationsInOrder() { { reader.skip(100); reader.close(); } }; }
private static void doStatistics(String filePath, Map<String, Integer> posMap) { ArrayList<Map.Entry<String, Integer>> posFrequencyList = new ArrayList<Map.Entry<String, Integer>>(posMap.entrySet()); Collections.sort( posFrequencyList, new Comparator<Map.Entry<String, Integer>>() { public int compare(Entry<String, Integer> entry1, Entry<String, Integer> entry2) { return entry1.getValue().compareTo(entry2.getValue()); } }); StringBuilder buffer = new StringBuilder(); for (Map.Entry<String, Integer> entry : posFrequencyList) { System.out.println(entry.getKey() + ": " + entry.getValue()); buffer.append(entry.getKey() + ": " + entry.getValue() + "\n"); } TextFile.write("pos_frequency_" + filePath.replace(".xml", ".txt"), buffer.toString()); }
private static void log(String tag, String outStr) { // seqNumber++; // nur für Testzwecke // aktuelle Zeit long timeS = (System.currentTimeMillis() - startTimeMs) / 1000; String strTimeS = Long.toString(timeS); // Ausgabestrings formatieren // String strDebTag = String.format("%d %s %s %s\n", seqNumber, strTimeS, tag, outStr); // String strDeb = String.format("%d %s %s\n", seqNumber, strTimeS, outStr); String strDebTag = String.format("%s %s %s\n", strTimeS, tag, outStr); String strDeb = String.format("%s %s\n", strTimeS, outStr); String strView; // Tag anzeigen ? if (mLogFrag.isShowingTag) { strView = strDebTag; } else { strView = strDeb; } // abspeichern if (mLogFrag.isSaving2File) { if (textFile == null) { newLogFile(); } textFile.appendText(strView); } else { textFile = null; } // buffern mStringBuilder.append(strView); // ausgeben falls Fragment sichtbar if (mLogFrag.isVisible()) { mLogFrag.setTextView(mStringBuilder.toString()); mStringBuilder.delete(0, mStringBuilder.length()); } }