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:
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()));
}
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:
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