Copying file from one folder to another in an OS independent way - java

I have tow folders, say folder1 and folder2.
folder1 contains files of different type like .txt, .doc, .png. All file are posted in this folder only.
Now, what i want to do is as soon as file is saved in folder1, a copy of that file goes into subfolder in folder2 and the subfolder name should be the extension of that file. i.e if abc.txt file is saved in folder1 then txt folder should be created inside folder2 (if txt folder does not exist) and abc.txt should be copied inside that subfolder. And if we have xyz.doc file then create doc folder and save inside that. I want to do this in Java.
and this should be platform independent i.e we can copy in Window, Linux or Android.

You can achieve this one by File.list() & Common's FileUtils

You can start a thread that checks frequently for new files in that folder. The thread has a list of all hashes from the files. If the file is not in the list its a new file. Then you can copy the file to the new location. Instead of checking the name of a file you can check the hash.

Can you wait for Java 7? WatchService is perfect for notification of new File objects.
Quote from the top of those JavaDocs:
A watch service that watches registered objects for changes and events. For example a file manager may use a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.
( Emphasis by Oracle, boldness by me. ;)

Related

Java : Zip files in Db within directory

I have stored blobs of multiple files in the database. User should be able to click on a link and should be able to download all the files as zipped. But before that I want to provide a folder structure to group those files. Say, a base folder followed by nested folders and related files. I followed the link below, looks like, I need to first create a physical dir structure for the same and then zip that structure?
http://www.journaldev.com/957/java-zip-example-to-zip-single-file-and-a-directory-recursively
Files in a zip directory are abstracted as ZipEntry objects in Java. Those have names that corresponds to the path relative to the directory where you unpack the zip file. Directories in a zip file are simply entries whose name ends with /. This way, you can also view a zip file as a plain list of binary data entries whose names reflect the directory structure.
So you don't have to create a folder structure on you hard drive and zip it but you can write directly from the database to the ZipOutputStream:
Create each folder as a ZipEntry with no content and a name ending with /.
Instead of using a FileInputStream for reading from files on the hard drive, write the output of resultSet.getBinaryStream("blobcolumn") directly to the entry.

How to make class files into a JAR file

I have a JAR file that I wanted to edit a string inside on of it's classes . So I extracted it using Winrar , done the changes I wanted and saved it . Now I have a folder with subfolders inside that contains class files (about 30 one) .My question here is how can I recreate the Jar file from those folders&files ? Like it's it's doing the reverse action of extracting the Jar file .. ?
You could create a .zip file, rename it (with WinRAR) to something.jar so it becomes a .jar archive and then copy the folders in there.
But what you are doing is not really recommended to be done
You shouldn't extract and re-archive it like that.
And DON'T I repeat DON'T edit .class files!
Jar file is basically a zip file - however the best way to edit jars is to use a file manager (like Total Commander). You can delete, copy, rename, ... whatever, transparently inside the archive.
http://en.wikipedia.org/wiki/JAR_%28file_format%29
May be only one tip for the Total Commander - to enter the archive just use Ctrl-Pagedown. And I don't think that is something wrong on jar editing (in some circumstances you have no other option). However class files editing... it is a different game ...

JAR - Listing files into a folder

I would like to get a list of file contained in a directory which is in a jar package.
I have an "images" folder, within it I have an Images class that should load all images from that directory.
In the past i used the MyClass.class.getResourceAsStream("filename"); to read files, but how do I read a directory?
This is what I tried:
System.out.println(Images.class.getResource("").getPath());
System.out.println(new File(Images.class.getResource("").getPath()).listFiles());
I tried with Images.class.getResource because I have to work with File and there isn't a constructor that accepts an InputStream.
The code produces
file:/home/k55/Java/MyApp/dist/Package.jar!/MyApp/images/
null
So it is finding the folder which I want to list files from, but it is not able to list files.
I've read on other forums that in fact you can't use this method for folders in a jar archive, so how can I accomplish this?
Update: if possible, i would like to read files without having to use the ZipInputStream
You can't do that easily.
What you need to do:
Get the path of the jar file.
Images.class.getResource("/something/that/exists").getPath()
Strip "!/something/that/exists".
Use Zip File System to browse the Jar file.
It's a little bit of hacking.

Java - read file from directory for jar

I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).

How to get rid of Thumbs.db file in Windows NTFS file system?

My web based java application storing files in Local Drive(E.g: D:/AppData). It's scanning a folder for files(String[] nameOfFiles = dirName.list();) and displays all the files in the folder. The Thumbs.db also coming with them. How to omit that file? For now, i am deleting it before scanning the folder.
Is there any other way in java to skip that file from scanning?
Assuming that dirName is a File object then File.list() has an overloaded member that takes a FilenameFilter object which can be used to filter the list of files returned.

Categories

Resources