Files in java


 
Thread Tools Search this Thread
Top Forums Programming Files in java
# 1  
Old 05-16-2012
Files in java

Hello,

I have a directory 'dir' which contains a set of files 'file1, file2, file3, ...',
Please, how to retrieve the file that have the greatest number of late?

Thank you.
This User Gave Thanks to chercheur857 For This Post:
# 2  
Old 05-16-2012
What do you mean by 'greatest number of late'? I don't think that translated.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 05-16-2012
I assume you mean the file with the oldest last modified date

You would use the File and Date classes I guess
Code:
import java.io.File;
import java.util.Date;

class LastFile{
    File directory;

    LastFile(String directoryPath){
        File directory=new File(directoryPath);
        this.directory=directory;
    }

    public String getLast(){
        File[] fileList=this.directory.listFiles();
        Date now= new Date(); //asumes current locale in use in file store
        long last_mod_date=now.getTime();
        String oldestFile = "";
        for (int i = 0; i < fileList.length; i++){
            if (fileList[i].lastModified() < last_mod_date){
                oldestFile = fileList[i].getPath();
                last_mod_date = fileList[i].lastModified();
            }
        }
        return(oldestFile);
    }

    /***
    * Quick and dirty test of class if called directly rather than imported
    * This is no substitute for proper testing, just a handy way to test 
    * one off projects.
    ***/
    public static void main(String[] args){
        if (args.length > 0){
            for (int i = 0;i<args.length;i++){
                LastFile last = new LastFile(args[i]);
                System.out.println( "The last modified file in the directory "
                + args[i] + " is " + last.getLast());
            }
        }
        else{
            LastFile last = new LastFile(".");
            System.out.println( "The last modified file in the directory \".\" is " 
            + last.getLast());
        }
    }
}


Last edited by Skrynesaver; 05-16-2012 at 01:01 PM.. Reason: Added caveat about testing to comment
This User Gave Thanks to Skrynesaver For This Post:
# 4  
Old 05-16-2012
I'm very sorry for the translation.
in fact, i would like to find the file that has the biggest number in his end
Example: file1, file2, file3
Response: file3

Thank you so much
# 5  
Old 05-17-2012
OK, take the code above, remove the Date calls and instead use java.util.regex to capture all the digits at the end of the filename and change the comparison loop above to return the highest number (or raise an exception if there are no files ending in digits?) Something like this.

WARNING untested pre-coffee code
Code:
    public String getLargestId(){
        File[] fileList=this.directory.listFiles();
        int largestFileId=0;
        String largestFile="";
         for (int i =0; i<fileList.length; i++){
            String filename=fileList[i].getPath();
            int fileId=getFinalDigits(filename);
            if ( fileId > largestFileId){
                largestFileId=fileId;
                largestFile=filename;
            }
        }
        return largestFile;
    }
    /**
    * You should of course catch any possible runtime exceptions
    * Check out the JavaDocs on the regex library
    **/
    private static int getFinalDigits(String filename){
        Pattern p = Pattern.compile("\\d+$");
        Matcher m = p.matcher(filename);
        return Integer.parseInt(m.group(0));
    }

This User Gave Thanks to Skrynesaver For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get CRC check sum of files in java EAR file without extracting .jar/.war files to disk.?

unzip -v gives CRC info of each file in a zip(in my case .EAR) file. # unzip -v my-application.ear Archive: my-application.ear Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 197981 Defl:N 183708 7%... (1 Reply)
Discussion started by: kchinnam
1 Replies

2. Homework & Coursework Questions

java project to sync 2 files

excuse me i have an question, school projcet about writing a java programme to syncronise 2 file in network using fuse with java plz i need help (1 Reply)
Discussion started by: yohana
1 Replies

3. Shell Programming and Scripting

how to delete certain java script from html files using sed

I am cleaning forum posts to convert them in offline reading version with clean html text. All files are with html extension and reside in one folder. There is some java script i would like to remove, which looks like <script LANGUAGE="JavaScript1.1"> <!-- function mMz() { var mPz = "";... (2 Replies)
Discussion started by: georgi58
2 Replies

4. Shell Programming and Scripting

Remove java code from multiple files

Hello, We have a client who has had an FTP injection attack on their account. Over 600 files have this code added to the files: <script>var t="";var... (10 Replies)
Discussion started by: dhasbro
10 Replies

5. Shell Programming and Scripting

AWK script for programatically modifying java files

Hi, I want to add a String variable to all java classes in my project. Assuming a class like public class Random { String var="Constant string"; ... ... ... } The text in bold is what I want to add to all java files in my workspace. I am an absolute newbie to AWK, and read somewhere that... (5 Replies)
Discussion started by: rocker86
5 Replies

6. Programming

Need an c,c++,or java code for parsing the log files

need the code for c,c++,java for parsing the log file (5 Replies)
Discussion started by: raghuraipur
5 Replies

7. Shell Programming and Scripting

Script for adding few methods to bunch of Java files

Hi I have around 1000+ java file under different folder in /home/raxit/source and in each file i want to add a fix method. -------- /* Some comment for few lines like header block etc.. */ package import class A { method1 () { } method2 () (3 Replies)
Discussion started by: raxitsheth
3 Replies

8. Shell Programming and Scripting

Compiling all modified Java files in a folder on Unix

Hi all, I have a Unix script that will compile all Java files in a sub folder as follows: find . -name "*.java" -print -exec $JAVA_HOME/bin/javac -cp .:$CLASSPATH '{}' \; I would like to enhance it to only compile those Java files who: 1.) Have no class file 2.) Have a class file... (1 Reply)
Discussion started by: Annorax
1 Replies

9. Programming

JAVA - External JAR files in UNIX

Hi, I have to run my JAVA programs in UNIX server. The java program uses some external jar files for compiling. I have set the classpath to the folder where all the jar files are present using EXPORT classpath command. But when i compile, it shows errors.. saying that the classes relating... (8 Replies)
Discussion started by: satish2712
8 Replies

10. UNIX for Dummies Questions & Answers

Possible to use a Java app with dll files on Unix-systems

Hi... I have build a program for the Velleman K8000 interface card, in java, which works just fine in windows, but now I want to use the program on a Unix-System. Is it possible to somehow convert the dll file to a format that Unix supports, or do I have to find another way? Dll-file:... (3 Replies)
Discussion started by: Scorp-D
3 Replies
Login or Register to Ask a Question