File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes how to get the last modified time of a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Reply locked New topic
Author

how to get the last modified time of a text file

Sonal Sharma
Ranch Hand

Joined: May 13, 2006
Posts: 62
How to read the last modified time of a text file or a directory in hh:mm:ss format.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20



SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sonal Sharma
Ranch Hand

Joined: May 13, 2006
Posts: 62
Dear Rob,
I am asking about the ftp environment.
Here is my program.It is throwing exceptions when I retrieve lastmodifiedtime of a file from an ftp server.
/*
* Main.java
*
* Created on April 23, 2008, 4:41 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/



package javaapplication1;
import com.jscape.inet.ftp.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;


/**
*
* @author Sonal
*/
public class Main {

private static Date lastmodifiedtime;

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Ftp ftp = new Ftp("ftp.FTPplanet.com","anonymous","engineersonal@rediffmail.com");
try {

// establish connection
ftp.connect();
} catch (FtpException ex) {
ex.printStackTrace();
}
String listing = null;
try {
listing = ftp.getDirListingAsString();
// lastmodifiedtime = ftp.getFileTimestamp();
} catch (FtpException ex) {
ex.printStackTrace();
}

// print directory listing to console
System.out.println(listing);
Enumeration files = null;
try {
files = ftp.getDirListing();
// lastmodifiedtime = ftp.getFileTimestamp();
} catch (FtpException ex) {
ex.printStackTrace();
}

// iterate through listing
while(files.hasMoreElements()) {
FtpFile file = (FtpFile)files.nextElement();

// only print files that are directories;
if(file.isDirectory()) {
System.out.println(file.getFilename());
String fileName = file.getFilename();
String fileName1 = "Sample files";
if(fileName.equals(fileName1))
{
File fl1 = new File(fileName,"batch2.txt");
try {
FileReader fr1 = new FileReader(fl1);
BufferedReader br = new BufferedReader(fr1);
String s = null;


while((s = br.readLine())!=null)
System.out.println(s);

br.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}

}
try {
lastmodifiedtime = ftp.getFileTimestamp(file.getFilename());
} catch (FtpException ex) {
ex.printStackTrace();
}

System.out.println(lastmodifiedtime);
System.out.println(file.getFilesize());
//System.out.println(file.getTime());
}
else{
System.out.println(file.getFilename());
System.out.println(file.getFilesize());

try {
lastmodifiedtime = ftp.getFileTimestamp(file.getFilename());
} catch (FtpException ex) {
ex.printStackTrace();
}

System.out.println(lastmodifiedtime);

}
}


// change local directory
ftp.setLocalDir(new File("C:/tmp"));
try {
// change transfer mode to ASCII
ftp.setAscii();
} catch (FtpException ex) {
ex.printStackTrace();
}
try {

// download a file named test.txt in remote directory
ftp.download("batch1.txt");
} catch (FtpException ex) {
ex.printStackTrace();
}

// release the FTP server connection
ftp.disconnect();

/*File file = new File("C:/tmp","batch1.txt");
System.out.println(new Date(file.lastModified()));*/

}

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42267
    
  64
Please do not post the same question to multiple forums: CarefullyChooseOneForum

Let's continue the discussion in this duplicate thread.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get the last modified time of a text file