systematic file(s) deletion

O

Oliver Wong

NickName said:
long ftime = fd.lastModified();
// test output of the file day value
System.out.println(ftime);
// result: 0
// comment: bad
// question: what's wrong?

This is much better. See the javadocs
http://java.sun.com/javase/6/docs/api/java/io/File.html

<quote>
Returns:
A long value representing the time the file was last modified, measured
in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if
the file does not exist or if an I/O error occurs
</quote>

So either the file does not exist, or an I/O error occurred. Did you try
checking the output of fd.exists() ?

- Oliver
 
N

NickName

Oliver said:
This is much better. See the javadocs
http://java.sun.com/javase/6/docs/api/java/io/File.html

<quote>
Returns:
A long value representing the time the file was last modified, measured
in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if
the file does not exist or if an I/O error occurs
</quote>

So either the file does not exist, or an I/O error occurred. Did you try
checking the output of fd.exists() ?

- Oliver

Ok, I now changed the code to the following (please see embeded
comments) below:

// list files
// the Temp directory has 10 files, of which one
file name includes white spaces


File dir = new File("Temp");


String[] children = dir.list();
if (children == null) {

} else {
for (int i = 0; i < children.length; i++) {

String filename = children;

// test file existence
boolean exist = (new
File(filename)).exists();
if (exist) {
System.out.println(filename);
}
else {
System.out.println("file instantiation
failed for " + filename +
"why? because it has already been instantiated?");
}

}


Thanks. Would this technique not be able to solve this problem?
 
O

Oliver Wong

NickName said:
Oliver said:
This is much better. See the javadocs
http://java.sun.com/javase/6/docs/api/java/io/File.html

<quote>
Returns:
A long value representing the time the file was last modified,
measured
in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if
the file does not exist or if an I/O error occurs
</quote>

So either the file does not exist, or an I/O error occurred. Did you
try
checking the output of fd.exists() ?

Ok, I now changed the code to the following (please see embeded
comments) below:

// list files
// the Temp directory has 10 files, of which one
file name includes white spaces


File dir = new File("Temp");


String[] children = dir.list();
if (children == null) {

} else {
for (int i = 0; i < children.length; i++) {

String filename = children;

// test file existence
boolean exist = (new
File(filename)).exists();
if (exist) {
System.out.println(filename);
}
else {
System.out.println("file instantiation
failed for " + filename +
"why? because it has already been instantiated?");
}

}


Thanks. Would this technique not be able to solve this problem?


I'm not sure what problem you're referring to when you say "this
problem". It won't magically create a file, if that's the problem you're
trying to solve. Not that whether or not a file exists on the file system
has nothing to do with the JVM's instantiation of objects. Consider this
example code to gain some enlightment as to what the exist() does:

public class Example {

public static boolean checkFileExists(String pathToFile) {
File temp = new File(pathToFile);
return temp.exists();
}

public static void main(String[] args) {
if (checkFileExists("C:/autoexec.bat")) {
System.out.println("You have an autoexec.bat file in the root of your
C drive.");
} else {
System.out.println("You don't have an autoexec.bat file in the root of
your C drive.");
}
}
}

Notice that this program always instantiates exactly 1 file object,
regard of whether or not you have an autoexec.bat file on your file system.

- Oliver
 
E

EJP

NickName said:
String[] children = dir.list();
if (children == null) {

} else {
for (int i = 0; i < children.length; i++) {

String filename = children;
File fd = new File(filename);


File fd = new File(dir, filename);
 
S

see

Ok, I now changed the code to the following (please see embeded
comments) below:
File dir = new File("Temp");
String[] children = dir.list();
[ ... }
for (int i = 0; i < children.length; i++) {
String filename = children;
// test file existence
boolean exist = (new
File(filename)).exists();
if (exist) {
System.out.println(filename);
}
else {
System.out.println("file instantiation
failed for " + filename +
"why? because it has already been instantiated?");
}
}
Thanks. Would this technique not be able to solve this problem?


Note that you didn't tell us the results of running your updated
code, but I'm fairly certain that it reported that "file instantiation
failed ...". (By the way, File.exists() does not instantiate the
file, it only checks for its existence.)

So, go back and read the javadocs referenced above. Look at the
wording for the definition of function File.list(). Now compare
that to the wording of File.getName() and File.getPath(). Pay
particular attention to the distinction between the terms "name"
and "pathname."

In any case, you might consider using File.listFiles() instead of
File.list(), which would both save you a step or two and, more
than likely, incidentally eliminate your symptoms.

- dmw
 
N

NickName

[ ... ]

Note that you didn't tell us the results of running your updated
code, but I'm fairly certain that it reported that "file instantiation
failed ...". (By the way, File.exists() does not instantiate the
file, it only checks for its existence.)

So, go back and read the javadocs referenced above. Look at the
wording for the definition of function File.list(). Now compare
that to the wording of File.getName() and File.getPath(). Pay
particular attention to the distinction between the terms "name"
and "pathname."

In any case, you might consider using File.listFiles() instead of
File.list(), which would both save you a step or two and, more
than likely, incidentally eliminate your symptoms.

- dmw

The docs are very helpful, thank you.
 
N

NickName

EJP said:
NickName said:
String[] children = dir.list();
if (children == null) {

} else {
for (int i = 0; i < children.length; i++) {

String filename = children;
File fd = new File(filename);


File fd = new File(dir, filename);


Thanks. It's almost there. The revised code per your instruction
above looks like this:
String[] children = dir.list();
if (children == null) {

} else {
for (int i = 0; i < children.length; i++) {

String filename = children;

File fd = new File(dir, filename);
long ft =
(fd.lastModified()/(8640000));

long now = (new Date().getTime() /
(8640000));

if ( (now - ft)/10 > 30) {
System.out.println("\nTemp files older than 30 days: " +
filename);
}

Comments:
1) Currently the (now - ft)/10 formula would seem to generate a date
difference value between TODAY and a file's date from the list.
However, previously when I used a test file, doing something like

File testFile = new File("myTest.txt");
Long testFileDate = testFile.lastModified();

then, the formula of (now - testFileDate)
// now, as long now = (new Date().getTime() / (8640000));
would yield a date difference 2 if the myTest.txt is two days old.

How come? Thanks.
 
N

NickName

Oliver said:
[ ... ]
Thanks. Would this technique not be able to solve this problem?

I'm not sure what problem you're referring to when you say "this
problem". It won't magically create a file, if that's the problem you're
trying to solve. Not that whether or not a file exists on the file system
has nothing to do with the JVM's instantiation of objects. Consider this
example code to gain some enlightment as to what the exist() does:

public class Example {

public static boolean checkFileExists(String pathToFile) {
File temp = new File(pathToFile);
return temp.exists();
}

public static void main(String[] args) {
if (checkFileExists("C:/autoexec.bat")) {
System.out.println("You have an autoexec.bat file in the root of your
C drive.");
} else {
System.out.println("You don't have an autoexec.bat file in the root of
your C drive.");
}
}
}

Notice that this program always instantiates exactly 1 file object,
regard of whether or not you have an autoexec.bat file on your file system.

- Oliver

Thank you very much for the interesting example. With your code, you
have a public class, which has a method, and the method is being called
by the class itself, and here you have a basic flow control construct.
And the famous String class (external) is being called/used.

Would you add a constructor somewhere with the code? Or probably I
need to read on the OOP concepts a bit before asking more questions.
Once again, I appreciated.
 
N

NickName

John said:
Is it your intention for "now" to be in units of tenths of a day? :)

I "stole" the following formula
long now = (new Date().getTime() / (8640000));
from the net and thought it was for a day. What formular would be for
a day?

Thanks.
 
O

Oliver Wong

NickName said:
Oliver Wong wrote: [...]
public class Example {

public static boolean checkFileExists(String pathToFile) {
File temp = new File(pathToFile);
return temp.exists();
}

public static void main(String[] args) {
if (checkFileExists("C:/autoexec.bat")) {
System.out.println("You have an autoexec.bat file in the root of
your
C drive.");
} else {
System.out.println("You don't have an autoexec.bat file in the root
of
your C drive.");
}
}
}

Notice that this program always instantiates exactly 1 file object,
regard of whether or not you have an autoexec.bat file on your file
system.

Thank you very much for the interesting example. With your code, you
have a public class, which has a method, and the method is being called
by the class itself,

I don't like this phrasing, "the method is being called by the class
itself". I prefer to think that the JVM "magically" starts a main thread,
and has that main thread call my static void main method.
and here you have a basic flow control construct.
And the famous String class (external) is being called/used.

I don't think it makes sense to say a class is called. Only methods can
be called. And Strings are used in my example, yes, but that's wasn't the
main focus of the example. It's about as significant, IMHO, to the fact that
the character 'e' was used in typing out that source code. It's a true
statement, but not very relevant.
Would you add a constructor somewhere with the code?

I'm not sure I understand the question. If I wanted to add a
constructor, I could certainly do so, but I didn't want to add one, which is
why the example didn't contain one. Are you asking me to add a constructor
to the example to demonstrate how it might be done, or are you asking
something else?

- Oliver
 
N

NickName

Oliver Wong wrote:
[ ...]
Are you asking me to add a constructor
to the example to demonstrate how it might be done? >
- Oliver

Yes, thank you in advance.
 
J

John Ersatznom

NickName said:
I "stole" the following formula
long now = (new Date().getTime() / (8640000));
from the net and thought it was for a day. What formular would be for
a day?

1000 ms in a s * 60 s in a m * 60 m in an h * 24 h in a day gives
86400000 -- with one more zero.
 
O

Oliver Wong

NickName said:
Oliver Wong wrote:
[ ...]
Are you asking me to add a constructor
to the example to demonstrate how it might be done?

Yes, thank you in advance.


public class Example {

public Example() {
/*A constructor which doesn't do anything.*/
}

public static boolean checkFileExists(String pathToFile) {
File temp = new File(pathToFile);
return temp.exists();
}

public static void main(String[] args) {
if (checkFileExists("C:/autoexec.bat")) {
System.out.println("You have an autoexec.bat file in the root of your
C drive.");
} else {
System.out.println("You don't have an autoexec.bat file in the root of
your C drive.");
}
}
}

- Oliver
 
L

Lew

in message
Oliver said:
public class Example {
public Example() {
/*A constructor which doesn't do anything.*/
} ....
}

Oliver, you are a patient and generous person.

- Lew
 
O

Oliver Wong

Lew said:
in message



Oliver, you are a patient and generous person.

I can't help but think there must be a misunderstanding somewhere, but I
haven't figured out where the misunderstanding lies yet. Perhaps the problem
was that the OP did not know the appropriate syntax for adding a constructor
to a class.

- Oliver
 
N

NickName

Oliver said:
I can't help but think there must be a misunderstanding somewhere, but I
haven't figured out where the misunderstanding lies yet. Perhaps the problem
was that the OP did not know the appropriate syntax for adding a constructor
to a class.

- Oliver

"Perhaps the problem
was that the OP did not know the appropriate syntax for adding a
constructor
to a class. "

Yes, thanks for the note as well as the sample for a Constructor
construction. The little java doc that I got from JBuilder IDE talks a
little bit about Constructor, however, I still don't know if when a
constructor is required for a class and when it is not.
 
O

Oliver Wong

NickName said:
Yes, thanks for the note as well as the sample for a Constructor
construction. The little java doc that I got from JBuilder IDE talks a
little bit about Constructor, however, I still don't know if when a
constructor is required for a class and when it is not.

Whenever you create an object using the "new" keyword (e.g. "new
Foo()"), a new instance of the appropriate object is created, and then the
constructor is immediately invoked on that new instance. If you haven't
written a constructor, then the compiler will automatically provide a
default constructor which doesn't do anything.

See
http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html

- Oliver
 
J

John Ersatznom

NickName said:
Yes, thanks for the note as well as the sample for a Constructor
construction. The little java doc that I got from JBuilder IDE talks a
little bit about Constructor, however, I still don't know if when a
constructor is required for a class and when it is not.

A constructor is required for a class if either
* the instances need some initialization of their state, particularly in
a parametrized way so you need an explicit constructor that takes
arguments, or
* you want to restrict instance creation, so you need to make
constructors protected or private or package-private,
or both.

As for syntax for things like this, that and anything else truly basic
and fundamental is documented at java.sun.com; particularly, check out
the Java Tutorial there and the class library API docs. Those two are
the most crucial references to know; bookmark them in your browser and
be thankful they're free-as-in-beer! (As you grow more experienced, the
API docs will increase and the Tutorial decrease in relative importance
to you. Third party libraries and their API docs will become of interest
at some point too. One wonderful thing about Java in general is how
well-documented everything tends to be, and how avoidable paying for big
doorstops tends to be. For most other languages you need at least one
big fat "Essential C++" or "colored book" or "animal book" or something
of the sort to make any headway!)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top