The Desktop class was added in Java 6 to handle launching associated applications on the native desktop.
Following shows how to launch the associated editor application and open a file for editing.
File file = new File("/tmp/file.txt");
Desktop desktop = Desktop.getDesktop();
desktop.edit(file);
written by objects
\\ tags: desktop, edit, editor, file, native
Many developers use the toURL() method of the File class to convert a File to a URL. This method however does not handle correctly escaping characters that are illegal in URL’s (such as spaces) and has been deprecated.
To convert a File to a URL you should instead first use the toURI() method to convert the file path to a URI. The URI class has a toURL() method that can then be used to convert the URI to a URL.
File file = new File(path);
URI uri = file.toURI();
URL url = uri.toURL();
// or alternatively in one line
URL url = file.toURI().toURL();
written by objects
\\ tags: file, spaces, URI, url, windows
Java doesn’t provide standard support for using tar archives so a 3rd party implementation is required. One such implementation is provided by ICE Engineering and can be found here.
Here is a simple example of its usage to tar the files in a directory.
File tarFile = new File("my.tar");
FileOutputStream out = new FileOutputStream(tarFile);
TarArchive tar = new TarArchive(out);
File[] files = directory.listFiles();
for (File file : files) {
System.out.println("Adding "+file);
TarEntry tarEntry = new TarEntry(file);
tar.writeEntry(tarEntry, false);
}
tar.closeArchive();
out.close();
written by objects
\\ tags: archive, file, tar, TarArchive, trustice
Recent Comments