-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDeleteFile.java
92 lines (74 loc) · 3.19 KB
/
DeleteFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
CREATE or REPLACE JAVA SOURCE NAMED "DeleteFile" AS
import java.io.*;
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.util.Date;
import java.text.*;
import java.text.DateFormat.*;
/*
** Delete files in specified directories that were last modified before
** a specified date
*/
public class DeleteFile {
public static int delete(oracle.sql.ARRAY tbl) throws SQLException {
try {
// Retrieve the contents of the table/varray as a result set
ResultSet rs = tbl.getResultSet();
// Iterate through the rows returned by the result set
// NOTE: The JDBC 2.0 standard specifies that the ResultSet
// contains rows consisting of two columns.
//
// Column 1 stores the element index for the row.
// Column 2 stores the actual element value.
//
for (int ndx = 0; ndx < tbl.length(); ndx++) {
rs.next();
// Retrieve the array index
int aryndx = (int)rs.getInt(1);
// Retrieve the array element (an object returned as type STRUCT)
STRUCT obj = (STRUCT)rs.getObject(2);
// Retrieve the attributes for the object
// as an array of Java Objects
Object[] attrs = obj.getAttributes();
// Retrieve the individual attributes
// casting the object to the correct type
String fileDir = (String)attrs[0];
Timestamp saveDate = (java.sql.Timestamp)attrs[1];
// Check that the directory exists
if (new File (fileDir).isDirectory()) {
// Retrive a list of the files in the specified directory
String[] filesList = new File (fileDir).list();
// Loop through the files checking which ones are older then the date specified
for (int i = 0; i < filesList.length; i++) {
String fullPathName = fileDir + "\\"+filesList[i];
if (lastModified(fullPathName) < saveDate.getTime()) {
delete(fullPathName);
}
}
}
}
// Close the result set
rs.close();
return 0;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
public static long lastModified (String fileName) {
return new File (fileName).lastModified();
}
public static int delete (String fileName) {
return bln2int (new File (fileName).delete());
}
public static int bln2int (boolean valIn) {
if (valIn) return 1; else return 0;
}
}
/*======================================================================
| Supplement to the fifth edition of Oracle PL/SQL Programming by Steven
| Feuerstein with Bill Pribyl, Copyright (c) 1997-2009 O'Reilly Media, Inc.
| To submit corrections or find more code samples visit
| http://oreilly.com/catalog/9780596514464/
*/