And if the method is static, then refer to it with the class name.
ADifferentClass.staticMethod();
//or
ADifferentClass obj = new ADifferentClass();
obj.nonStaticMethod();
Note however that if you're defining and calling too many static methods, your design may be poor.
import java.io.File;
public class FindLatestFile {
public static File getLatest(File thisDir) throws Exception {
long latestModDate = -1;
File latestFile = null;
if (!thisDir.isDirectory()) {
throw new Exception("Please pass in a directory.");
}
File[] fileList = thisDir.listFiles();
for(int i=0; i < fileList.length; i++){
File file = fileList[i];
if (file.lastModified() > latestModDate) {
latestModDate = file.lastModified();
latestFile = file;
}
}
return latestFile;
}
}
Above is my class which gets the last file that has been modified. If I wanted my other class to use the last modified method I should have something along the lines of:
FindLatestFile.getLatest();
> FindLatestFile.getLatest(thisDir);
> I tried that but its not working.
"not working" tells us squat. If you get an error message, please post the message.
You can use a variable called thisDir as the argument for the method, but realize that it is totally unrelated to the "thisDir" in the code where you define your method. So unless you have previously declared thisDir to be a Path variable that is within the scope of this call and you have previously initialized it you will have problems.
/p
Message was edited by:
petes1234
Yeah sorry. Im just playing with code I have here. Mainly changing things to see if I can get it to do stuff. I think this is what I was looking for:
try {
FindLatestFile.getLatest( new File(System.getProperty("user.dir")));
} catch (Exception e) {
e.printStackTrace();
}
When I changed it to the following it printed the text and the full path to the correct file:
try {
System.out.println("The latest file in the current directory is " +
getLatest( new File(System.getProperty("user.dir") ) ) + ".");
} catch (Exception e) {
e.printStackTrace();
}
Now what I wish to do is to is(well if its possible) change my code so that the last file that has been edited is selected. At the min I specify the file. Heres what I got so far:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
public class XML_builder {
public XML_builder() { }
public static void main(String[] args){
testBuildXML();
}
private static void testBuildXML(){
try{
String textFile = "P://Useful Docs/Programming/Java/Automated Texting/Automated Texting/phoneNumbers.txt";
String XMLFile = "P://Useful Docs/Programming/Java/Automated Texting/Automated Texting/phoneNumbers.xml";
File text = new File(textFile);
FileReader reader = new FileReader(text);
BufferedReader buffer = new BufferedReader(reader);
String temp = "";
int x = 0;
Element element = new Element("phone_numbers");
while ((temp = buffer.readLine())!= null){
Element child_node = new Element("number_"+x);
System.out.println(temp);
child_node.addContent(temp);
x++;
element.addContent(child_node);
}
FileWriter writer = new java.io.FileWriter(XMLFile);
XMLOutputter xmloutputter = new XMLOutputter();
Document doc = new Document(element);
xmloutputter.output(doc, writer);
}
catch (IOException exception){
System.out.println(exception);
}
}
}
Is it possible to set the getLatest to the value textFile in a similar sort of way that I have:
File text = new File(textFile);
Or am I way off the mark?
Message was edited by:
paul_carron
null
> public static File getLatest(File thisDir)
> throws Exception {
...
>if (!thisDir.isDirectory()) {
> throw new Exception("Please pass in a
> directory.");
>}
That's pretty poor, to throw a bare Exception like that. If the problem is that a bad argument was passed in, then throw an IllegalArgumentException.
> if (file.lastModified() > latestModDate) {
You might want to explicitly exclude directories from that file listing, before you check the last modified dates. Dunno if that's causing the problem you're having, because I'm not quite sure what your problem is yet.
I took out the exception altogether. Does that look better now?
import java.io.File;
public class FindLatestFile {
public static File getLatest(File thisDir){
long latestModDate = -1;
File latestFile = null;
File[] fileList = thisDir.listFiles();
for(int i=0; i < fileList.length; i++){
File file = fileList[i];
if (file.lastModified() > latestModDate) {
latestModDate = file.lastModified();
latestFile = file;
}
}
return latestFile;
}
}
> if (file.lastModified() > latestModDate) {
You might want to explicitly exclude directories from that file listing, before you check the last modified dates.
how is this done?
Dunno if that's causing the problem you're having, because I'm not quite sure what your problem is yet.
Do you mean my explanation is not very clear or you just havnt figured it out yet?
If its a case that im not being clear I'll have another go. What I want is for my XML_builder class to use my FindLatestFile class to select the file I will use rather than have it coded into the XML_builder.
> I took out the exception altogether. Does that look
> better now?
It's not a matter of how it looks, it's the functionality and maintainability.
Your code may now throw a null pointer exception if the argument is not a directory. I'd argue that's much worse than an illegal argument exception.
So I'd suggest either:
1) put in the check to confirm that the file argument is a directory, and throw an IllegalArgumentException if it is not, or
2) put the whole for loop in an if block, and the if's boolean expression will check to see if fileList is null.
>> You might want to explicitly exclude directories from
>> that file listing, before you check the last modified
>> dates.
>
> how is this done?
I'd suggest calling listFiles() instead of list(), and pass it a FileFilter. The FileFilter would check to see if the file is a plain file (plus whatever else you're checking for).
>> Do you mean my explanation is not very clear or you
>> just havnt figured it out yet?
I mean I don't know what your problem is. Does the code compile? Does it run? Does it give the right results?
The program just terminates. Sorry that I havnt been very clear. I think I'm getting a bit ahead of myself. Let me just take a step back. Do you know what this means?
FindLatestFile.getLatest( new File(System.getProperty("user.dir")));
I take it that this is using the getLatest method to find the last modified file. Does ("user.dir") say to look in the same file as my class file? If so I will either need to put my class in a different directory and specify which directory I want the last modified file to be found in or will need to put in a condition as to what type of file I am looking for. I think I would rather have my class file in a different directory. No sure how to do this though. Any suggestions?