/** * Delete a directory or file and all subdirectories and files. * * @param path the path * @param tryOnly whether errors should be ignored */ public static void deleteRecursive(String path, boolean tryOnly) { if (exists(path)) { if (isDirectory(path)) { for (String s : newDirectoryStream(path)) { deleteRecursive(s, tryOnly); } } if (tryOnly) { tryDelete(path); } else { delete(path); } } }
private boolean openWriter() { if (printWriter == null) { try { FileUtils.createDirectories(FileUtils.getParent(fileName)); if (FileUtils.exists(fileName) && !FileUtils.canWrite(fileName)) { // read only database: don't log error if the trace file // can't be opened return false; } fileWriter = IOUtils.getBufferedWriter(FileUtils.newOutputStream(fileName, true)); printWriter = new PrintWriter(fileWriter, true); } catch (Exception e) { logWritingError(e); return false; } } return true; }
private static String createTempLobFileName(DataHandler handler) throws IOException { String path = handler.getDatabasePath(); if (path.isEmpty()) { path = SysProperties.PREFIX_TEMP_FILE; } return FileUtils.createTempFile(path, Constants.SUFFIX_TEMP_FILE, true, true); }
/** * Create the directory and all required parent directories. * * @param dir the directory name */ public static void createDirectories(String dir) { if (dir != null) { if (exists(dir)) { if (!isDirectory(dir)) { // this will fail createDirectory(dir); } } else { String parent = getParent(dir); createDirectories(parent); createDirectory(dir); } } }
boolean exists = FileUtils.exists(name); if (exists && !FileUtils.canWrite(name)) { mode = "r"; } else { FileUtils.createDirectories(FileUtils.getParent(name)); file = FileUtils.open(name, mode); if (exists) { fileLength = file.size();
RedoLog(Map<String, String> config) { this.config = config; if (config.containsKey("log_chunk_size")) logChunkSize = Long.parseLong(config.get("log_chunk_size")); else logChunkSize = DEFAULT_LOG_CHUNK_SIZE; String baseDir = config.get("base_dir"); String logDir = config.get("redo_log_dir"); String storagePath = baseDir + File.separator + logDir; config.put("storagePath", storagePath); if (!FileUtils.exists(storagePath)) FileUtils.createDirectories(storagePath); List<Integer> ids = getAllChunkIds(); int lastId; if (!ids.isEmpty()) lastId = ids.get(ids.size() - 1); else lastId = 0; currentChunk = new RedoLogChunk(lastId, config); }
/** * Load a properties object from a file. * * @param fileName the name of the properties file * @return the properties object */ public static synchronized SortedProperties loadProperties(String fileName) throws IOException { SortedProperties prop = new SortedProperties(); if (FileUtils.exists(fileName)) { InputStream in = null; try { in = FileUtils.newInputStream(fileName); prop.load(in); } finally { if (in != null) { in.close(); } } } return prop; }
/** * Delete the given file now. This will remove the reference from the list. * * @param ref the reference as returned by addFile * @param fileName the file name */ public synchronized void deleteFile(Reference<?> ref, String fileName) { if (ref != null) { String f2 = refMap.remove(ref); if (f2 != null) { if (SysProperties.CHECK) { if (fileName != null && !f2.equals(fileName)) { DbException.throwInternalError("f2:" + f2 + " f:" + fileName); } } fileName = f2; } } if (fileName != null && FileUtils.exists(fileName)) { try { IOUtils.trace("TempFileDeleter.deleteFile", fileName, null); FileUtils.tryDelete(fileName); } catch (Exception e) { // TODO log such errors? } } }
if (packageName != null) { dir = new File(dir, packageName.replace('.', '/')); FileUtils.createDirectories(dir.getAbsolutePath()); OutputStream f = FileUtils.newOutputStream(javaFile.getAbsolutePath(), false); PrintWriter out = new PrintWriter(IOUtils.getBufferedWriter(f)); classFile.delete();
/** * Copy a file from one directory to another, or to another file. * * @param original the original file name * @param copy the file name of the copy */ public static void copyFiles(String original, String copy) throws IOException { InputStream in = FileUtils.newInputStream(original); OutputStream out = FileUtils.newOutputStream(copy, false); copyAndClose(in, out); }
private void deleteOldTempFiles() { String path = FileUtils.getParent(getStoragePath()); for (String name : FileUtils.newDirectoryStream(path)) { if (name.endsWith(Constants.SUFFIX_TEMP_FILE) && name.startsWith(getStoragePath())) { // can't always delete the files, they may still be open FileUtils.tryDelete(name); } } }
@Override public FileStorage openFile(String name, String openMode, boolean mustExist) { if (mustExist && !FileUtils.exists(name)) { throw DbException.get(ErrorCode.FILE_NOT_FOUND_1, name); } FileStorage fileStorage = FileStorage.open(this, name, openMode, dbSettings.cipher, dbSettings.filePasswordHash); try { fileStorage.init(); } catch (DbException e) { fileStorage.closeSilently(); throw e; } return fileStorage; }
private void initWrite() throws IOException { if (output == null) { try { OutputStream out = FileUtils.newOutputStream(fileName, false); out = new BufferedOutputStream(out, Constants.IO_BUFFER_SIZE); output = new BufferedWriter(new OutputStreamWriter(out, characterSet)); } catch (Exception e) { close(); throw DbException.convertToIOException(e); } } }
@Override public OutputStream newOutputStream(boolean append) throws IOException { try { File file = new File(name); File parent = file.getParentFile(); if (parent != null) { FileUtils.createDirectories(parent.getAbsolutePath()); } FileOutputStream out = new FileOutputStream(name, append); IOUtils.trace("openFileOutputStream", name, out); return out; } catch (IOException e) { freeMemoryAndFinalize(); return new FileOutputStream(name); } }
public void delete() { FileUtils.delete(fileName); } }
private void initRead() throws IOException { if (input == null) { try { InputStream in = FileUtils.newInputStream(fileName); in = new BufferedInputStream(in, Constants.IO_BUFFER_SIZE); input = new InputStreamReader(in, characterSet); } catch (IOException e) { close(); throw e; } } if (!input.markSupported()) { input = new BufferedReader(input); } input.mark(1); int bom = input.read(); if (bom != 0xfeff) { // Microsoft Excel compatibility // ignore pseudo-BOM input.reset(); } inputBuffer = new char[Constants.IO_BUFFER_SIZE * 2]; if (columnNames == null) { readHeader(); } }
/** * Remove this storage. */ synchronized void remove() { checkOpen(); closeImmediately(); FileUtils.deleteRecursive(btreeStoragePath, true); }
long getDiskSpaceUsed() { return org.lealone.storage.fs.FileUtils.folderSize(new File(btreeStoragePath)); }