Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

i really in need of an answer to my question. As i am a novice in java and of course in the programming.I want to know the java code for retrieving the information of a file and storing that information(info such as filename,filesize,creation date,creation time,creators name,moified date,modified time,location information)in our system's database.

Guys please bless me with this code, i am in great need of this code and i hope you guys will really help me out from this problem.

Regards
Pavan.
Posted
Comments
Marc A. Brown 24-Mar-11 10:00am    
What have you tried? What research have you done? What problems have you encountered? You'll get better results here by telling us these things, rather than asking for code to be provided.
pavanparcha 24-Mar-11 10:07am    
i know but i have only few hours to finish my work. What all i have done to achieve this has been rejected saying my approach to the problem is wrong...what can i do at this peak moment!!

1 solution

To do what you're asking for is actually quite complicated in Java as not all OSes provide the information you required (such as created date of a file for example).

For the simple case where you do not need all the fields you can get them by doing something like this:

Java
package com.test;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
public class Program {
	
	
	public static void SimpleExample()	{
		File file = new File("C:\\Temp\\ClientTrace.svclog");
		
		System.out.println("Name: " + file.getName());
		System.out.println("Absolute path: " + file.getAbsolutePath());
		System.out.println("Size: " + file.length());
		System.out.println("Last modified: " + new Date(file.lastModified()));
	}
	
        /* Ignore this one for now*/
	public static void ComplexExample() throws IOException, ParseException	{
		FileInfo file = new FileInfo(new File("C:\\Temp\\ClientTrace.svclog"));
		
		System.out.println("Name: " + file.getName());
		System.out.println("Absolute path: " + file.getAbsolutePath());
		System.out.println("Size: " + file.getSize());
		System.out.println("Last modified: " + file.getLastModified());
		System.out.println("Owner: " + file.getOwner());
		System.out.println("Created: " + file.getCreated());
		System.out.println("Accessed: " + file.getAccessed());
		System.out.println("Written: " + file.getWritten());
	}
	
	
	public static void main(String[] args) throws Exception, ParseException {
		
		SimpleExample();
		ComplexExample();
	}
}


To get more information on a windows machine you can shell out to the cmd and run dir (or really any other command that'll give you the information you need).

In the complexExample in the I'm making use of a FileInfo class that can be implemented like this:

Java
package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class FileInfo {
	public enum Timefield {
		CREATED, ACCESSED, WRITTEN
	}
	private final static DateFormat FORMATTER = new SimpleDateFormat(
			"dd/MM/yyyy  hh:mm");
	private File file;
	private boolean hasLoaded = false;
	private String owner;
	private Map<Timefield, Date> timefields = new HashMap<Timefield, Date>();
	public FileInfo(File file) {
		this.file = file;
	}
	private String getTimefieldSwitch(Timefield field) {
		switch (field) {
		case CREATED:
			return "C";
		case ACCESSED:
			return "A";
		default:
			return "W";
		}
	}
	private void shellToDir(Timefield timefield) throws IOException,
			ParseException {
		Runtime systemShell = Runtime.getRuntime();
		Process output = systemShell.exec(String.format("cmd /c dir /Q /R /T%s %s ", getTimefieldSwitch(timefield), file.getAbsolutePath()));
		BufferedReader reader = new BufferedReader(new InputStreamReader(output.getInputStream()));
		String outputLine = null;
		while ((outputLine = reader.readLine()) != null) {
			if (outputLine.contains(file.getName())) {
				timefields.put(timefield,
						FORMATTER.parse(outputLine.substring(0, 17)));
				owner = outputLine.substring(36, 59);
			}
		}
	}
	private void load() throws IOException, ParseException {
		if (hasLoaded)
			return;
		shellToDir(Timefield.CREATED);
		shellToDir(Timefield.ACCESSED);
		shellToDir(Timefield.WRITTEN);
	}
	public String getName() {
		return file.getName();
	}
	public String getAbsolutePath() {
		return file.getAbsolutePath();
	}
	public long getSize() {
		return file.length();
	}
	public Date getLastModified() {
		return new Date(file.lastModified());
	}
	public String getOwner() throws IOException, ParseException {
		load();
		return owner;
	}
	public Date getCreated() throws IOException, ParseException {
		load();
		return timefields.get(Timefield.CREATED);
	}
	public Date getAccessed() throws IOException, ParseException {
		load();
		return timefields.get(Timefield.ACCESSED);
	}
	public Date getWritten() throws IOException, ParseException {
		load();
		return timefields.get(Timefield.WRITTEN);
	}
}



As for actually getting that stuff into a database, well it really depends on what your DB is and looks like. This article[^] is a good starting point.

Hope this helps,
Fredrik Bornander
 
Share this answer
 
Comments
pavanparcha 24-Mar-11 10:55am    
Thank You Mr.Fredrik Bornander.
Nuri Ismail 24-Mar-11 12:10pm    
Excellent answer! My 5. :)
aj250037 10-May-13 9:02am    
Outstanding!!!!! My vote of 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900