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 check if I'm reading the same file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "How to check if I Watch "How to check if I New topic
Author

How to check if I'm reading the same file

Bing Lu
Ranch Hand

Joined: Oct 10, 2000
Posts: 42
Say I open a file, do some stuff w/ it, and exit the application and the file is closed. After couple of days, I want to read the same file(same path) but not sure if that file is newly created or is the same old file, can anyone tell me how do I check that in JAVA?
thanks
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
If you try to read a file that does not exist you get a FileNotFoundException.
So let's make up a scenario.
Day 1:
you have some stuff to write on disk, so you have to create a FileOutputStream or a FileWriter. Say, we use FileOutputStream. Let's create one:
FileOutputStream outToBackup = new FileOutputStream("c:\temp\backup.bak");
then you write your stuff (outToBackup.write(...); )
Eventually, you close your application.
DAY 12:
You want to read what you stored in your backup file 12 days ago. You create a FileInputStream (or a FileReader) like this:
FileInputStream inFromBackup = new FileInputStream("c:\temp\backup.bak");
Then you read what you need (inFromBackup.read(...); )
And finally you close you application.
Note that you can pass a boolean to the FileOutputStream constructor in order to open write to the file in append mode (everything gets written at the end fo the file) or not (everything is written from the beginning of the file, contents may be erased).
That's all there is to file reading and writing...
To sum up, if you create a file and you don't move it, then it's gonna be at the same place even years later.
[ February 21, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
I'm still not entirely sure what the original question was, but you may also want to make sure of some File methods like exists() or lastModified(), if there's some question about whether a file is still where you left it, or if some other process has altered it since then.


"I'm not back." - Bill Harding, Twister
 
 
subject: How to check if I'm reading the same file
 
Threads others viewed
ODBC and xBase
c:if doubt
A basic problem
Validate HTML
jsp code wont run
IntelliJ Java IDE