List of usage examples for org.apache.mahout.common.iterator FileLineIterable FileLineIterable
public FileLineIterable(InputStream is, boolean skipFirstLine)
From source file:com.msiiplab.recsys.rwr.GroupLensDataModel.java
License:Apache License
private static File convertGLFile(File originalFile) throws IOException { // Now translate the file; remove commas, then convert "::" delimiter to // comma//from w w w.j a v a 2 s .c o m File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "ratings.txt"); if (resultFile.exists()) { resultFile.delete(); } Writer writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(resultFile), Charsets.UTF_8); for (String line : new FileLineIterable(originalFile, false)) { int lastDelimiterStart = line.lastIndexOf(COLON_DELIMTER); if (lastDelimiterStart < 0) { writer.close(); throw new IOException("Unexpected input format on line: " + line); } String subLine = line.substring(0, lastDelimiterStart); String convertedLine = COLON_DELIMITER_PATTERN.matcher(subLine).replaceAll(","); writer.write(convertedLine); writer.write('\n'); } } catch (IOException ioe) { resultFile.delete(); throw ioe; } finally { Closeables.close(writer, false); } return resultFile; }
From source file:rec.sys.examples.GroupLensDataModel.java
License:Apache License
private static File convertGLFile(File originalFile) throws IOException { // Now translate the file; remove commas, then convert "::" delimiter to comma File resultFile = new File(new File(System.getProperty("java.io.tmpdir")), "ratings.txt"); if (resultFile.exists()) { resultFile.delete();/*from ww w .j a v a 2s. c om*/ } try { Writer writer = new OutputStreamWriter(new FileOutputStream(resultFile), Charsets.UTF_8); for (String line : new FileLineIterable(originalFile, false)) { int lastDelimiterStart = line.lastIndexOf(COLON_DELIMTER); if (lastDelimiterStart < 0) { throw new IOException("Unexpected input format on line: " + line); } String subLine = line.substring(0, lastDelimiterStart); String convertedLine = COLON_DELIMITER_PATTERN.matcher(subLine).replaceAll(","); writer.write(convertedLine); writer.write('\n'); } } catch (IOException ioe) { resultFile.delete(); throw ioe; } return resultFile; }