/** * Delete a URI. Currently this only works for file: URIs, however the * interface should not need to change to handle more complex URIs * * @param orig * The URI to delete * @return whether the deleted happened * @throws IOException a problem with I/O happened */ public static boolean delete(URI orig) throws IOException { checkFileURI(orig); return new File(orig.getPath()).delete(); }
/** * Move a URI from one place to another. Currently this only works for file: * URIs, however the interface should not need to change to handle more * complex URIs * * @param oldUri * The URI to move * @param newUri * The destination URI * @return whether the move happened * @throws IOException a problem with I/O happened */ public static boolean move(URI oldUri, URI newUri) throws IOException { checkFileURI(oldUri); checkFileURI(newUri); File oldFile = new File(oldUri.getPath()); File newFile = new File(newUri.getPath()); return oldFile.renameTo(newFile); }
/** * If the directory does not exist, create it. Note this currently only * works with file: type URIs * * @param orig * The directory URI to create * @throws MalformedURLException * If the URI is not valid */ public static void makeDirectory(URI orig) throws MalformedURLException { checkFileURI(orig); File file = new File(orig.getPath()); // If it is a file, except if (file.isFile()) { // TRANSLATOR: Error condition: A directory was expected, but a file was found. {0} is a placeholder for the file. throw new MalformedURLException(JSMsg.gettext("The URL {0} is a file.", orig)); } // Is it already a directory ? if (!file.isDirectory()) { // Create the directory and make sure it worked. if (!file.mkdirs()) { // TRANSLATOR: Error condition: A directory could not be created. {0} is a placeholder for the directory throw new MalformedURLException(JSMsg.gettext("The URL {0} could not be created as a directory.", orig)); } } }
/** * If the file does not exist, create it. Note this currently only works * with file: type URIs * * @param orig * The file URI to create * @throws MalformedURLException * If the URI is not valid * @throws IOException a problem with I/O happened */ public static void makeFile(URI orig) throws MalformedURLException, IOException { checkFileURI(orig); File file = new File(orig.getPath()); // If it is a file, except if (file.isDirectory()) { // TRANSLATOR: Error condition: A file was expected, but a directory was found. {0} is a placeholder for the directory. throw new MalformedURLException(JSMsg.gettext("The URL {0} is a directory.", orig)); } // Is it already a directory ? if (!file.isFile()) { FileOutputStream fout = new FileOutputStream(file); fout.close(); // Did that work? if (!file.isFile()) { // TRANSLATOR: Error condition: A file could not be created. {0} is a placeholder for the file throw new MalformedURLException(JSMsg.gettext("The URL {0} could not be created as a file.", orig)); } } }