/** Opens a reader to the file called filename. Provided for easy overriding for encoding support etc in * child classes. Called from openNextFile(). * @param filename File to open. * @return BufferedReader of the file */ public static BufferedReader openFileReader(String filename) throws IOException { return openFileReader(filename,null); }
/** Loads the entire file represented by filename into memory * @param filename Location of bit file to read into memory * @throws IOException if anything goes wrong reading the file */ public BitFileInMemory(String filename) throws IOException { this(Files.openFileStream(filename), Files.length(filename)); }
/** Write any dirty properties down to disk */ protected void flushProperties() throws IOException { if (dirtyProperties) { final String propertiesFilename = path + ApplicationSetup.FILE_SEPARATOR + prefix + PROPERTIES_SUFFIX; if ((Files.exists(propertiesFilename) && !Files .canWrite(propertiesFilename)) || (!Files.exists(propertiesFilename) && !Files .canWrite(path))) { logger.warn("Could not write to index properties at " + propertiesFilename + " because you do not have write permission on the index - some changes may be lost"); return; } final OutputStream outputStream = Files .writeFileStream(propertiesFilename); properties.store(outputStream, this.toString()); outputStream.close(); dirtyProperties = false; } }
/** Copy a file from srcFile to destFile. * @return null if OK * @throws IOException if there was a problem copying */ public static Long copyFile(File srcFile, File destFile) throws IOException { return copyFile(openFileStream(srcFile), writeFileStream(destFile)); }
/** * Constructs an instance of the class for a given filename * @param filename java.lang.String the name of the underlying file * @param bufSize how much of the file to buffer */ public BitFileBuffered(String filename, int bufSize) { buffer_size = bufSize; try { fileSize = Files.length(filename); file = Files.openFileRandom(filename); } catch (IOException ioe) { logger.error("Input/Output exception while creating BitFileBuffered object.", ioe); } }
public ByteOutputStream(String filename) throws IOException { OutputStream file; try{ file = Files.writeFileStream(filename); init(file); } catch(FileNotFoundException fnfe) { final String dir = Files.getParent(filename); logger.warn("Could not open new ByteOutputStream because it alleged file could not be found.", fnfe); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir) +" Dir.contentsSize="+Files.list(dir).length); sleep(1000); if (logger.isDebugEnabled()) logger.debug("File.canRead()="+Files.canWrite(filename)+ "Dir.exists()=" +Files.exists(dir) + " Dir.canWrite()="+Files.canWrite(dir)+" Dir.contentsSize="+Files.list(dir).length); logger.warn("Retrying to write BitOutputStream."); init(Files.writeFileStream(filename)); logger.info("Previous warning can be ignored, BitOutputStream "+filename+" has opened successfully"); } }
try { BufferedReader br; if (! Files.exists(queryfilename) || ! Files.canRead(queryfilename)) { logger.error("The topics file " + queryfilename + " does not exist, or it cannot be read."); return false; } else { br = Files.openFileReader(queryfilename,desiredEncoding); TRECFullTokenizer queryTokenizer = new TRECFullTokenizer( new TagSet(TagSet.TREC_QUERY_TAGS),
FileNumber++; if (! Files.exists(filename)) else if (! Files.canRead(filename)) is = Files.openFileStream(filename); //throws an IOException, throw upwards logger.info(this.getClass().getSimpleName() + " "+( (100*(FileNumber-1))/FilesToProcess.size())+"% processing "+filename);
/** Opens an InputStream to a file called file. * @param file File to open. * @return InputStream of the file */ public static InputStream openFileStream(File file) throws IOException { return openFileStream(file.getPath()); }
/** Opens an OutputStream to a file called file. * @param file File to open. * @return OutputStream of the file */ public static OutputStream writeFileStream(File file) throws IOException { return writeFileStream(file.getPath()); }
DataOutputStream dos = new DataOutputStream(Files.writeFileStream(LexiconFilename.concat(".tmp2"))); DataInputStream dis = new DataInputStream(Files.openFileStream(LexiconFilename.concat(".tmp2"))); Files.delete(LexiconFilename.concat(".tmp2")); FSOMapFileLexicon.deleteMapFileLexicon("lexicon", index.getPath(), index.getPrefix()); FSOMapFileLexicon.renameMapFileLexicon("tmplexicon", index.getPath(), index.getPrefix(), "lexicon", index.getPath(), index.getPrefix());
/** Opens an Writer to a file called file. System default encoding will be used. * @param filename File to open. * @return Writer of the file */ public static Writer writeFileWriter(String filename) throws IOException { return writeFileWriter(filename, null); }
protected void loadBlacklist(String BlacklistSpecFilename) { //reads the trec_blacklist_docid file if (BlacklistSpecFilename != null && BlacklistSpecFilename.length() >0) { try { if (Files.exists(BlacklistSpecFilename)) { BufferedReader br =Files.openFileReader(BlacklistSpecFilename); String blackListedDocid = null; while ((blackListedDocid = br.readLine()) != null) { if (!blackListedDocid.startsWith("#") && !blackListedDocid.equals("")) DocIDBlacklist.add(blackListedDocid); } br.close(); } } catch (IOException ioe) { logger.error("Input/Output exception while reading the document black list." + "Stack trace follows", ioe); } } }
/** returns true if all named files exist */ public static boolean allExists(String... files) { for (int i = 0; i < files.length; i++) { if (!Files.exists(files[i])) { logger.debug("Files " + files[i] + " doesn't exist"); return false; } } return true; }
/** Move an index from one location to another */ public static void renameIndex(String srcPath, String srcPrefix, String dstPath, String dstPrefix) throws IOException { final String actualPrefix = srcPrefix +'.'; for (String filename : Files.list(srcPath)) { if (filename.startsWith(actualPrefix)) { final String newFilename = filename.replaceFirst(srcPrefix, dstPrefix); if (! Files.rename(srcPath + "/" + filename, dstPath+"/"+ newFilename)) { final String srcExists = Files.exists(srcPath + "/" + filename) ? "exists" : "notexists"; final String destExists = Files.exists(dstPath+"/"+ newFilename) ? "exists" : "notexists"; throw new IOException("Rename of index structure file '"+srcPath + "/" + filename+"' ("+srcExists+") to " + "'"+ dstPath+"/"+ newFilename +"' ("+destExists+") failed - likely that source file is still open. " + "Possible indexing bug?"); } } } }
if (! Files.exists(thisFilename) || ! Files.canRead(thisFilename) ) if (! Files.exists(thisFilename)) logger.warn("File doesn't exist: "+thisFilename); else if (! Files.canRead(thisFilename) ) logger.warn("File cannot be read: "+thisFilename); rtr = nextDocument(); else if (Files.isDirectory(thisFilename))
@Test public void testWrite() throws Exception { for (String suffix : SUPPORTED_EXTENSIONS) { File f = super.tmpfolder.newFile("test.txt"+suffix); Writer w = Files.writeFileWriter(f.toString()); w.write("hello world\n"); w.close(); assertTrue(Files.exists(f.toString())); assertEquals("hello world", readFirstLineReader(f.toString())); assertEquals("hello world", readFirstLineStream(f.toString())); f.delete(); } }
/** Delete an existing index */ public static void deleteIndex(String path, String prefix) throws IOException { final String actualPrefix = prefix +'.'; String[] files = Files.list(path); if (files == null) return; for (String filename : files) { if (filename.startsWith(actualPrefix)) { Files.delete(path + "/" + filename); } } }
final String actualDestinationPrefix = ((IndexOnDisk) index).getPrefix() +'.' + destinationStructureName + "."; final String path = ((IndexOnDisk) index).getPath(); for (String filename : Files.list(((IndexOnDisk) index).getPath())) Files.delete(path + '/' + filename.replaceFirst(actualSourcePrefix, actualDestinationPrefix)); Files.rename( path + '/' + filename, path + '/' + filename.replaceFirst(actualSourcePrefix, actualDestinationPrefix));