Article View: borland.public.jbuilder.java.language
Article #8100java.util.Timer
From: Stace Merkel
Date: Wed, 25 Apr 2001 17:58
Date: Wed, 25 Apr 2001 17:58
159 lines
6366 bytes
6366 bytes
I have a swing application that I want to periodically poll a folder,
determine if files have been saved to the folder, open the files, apply
specific formatting, and save files as new files.
I have read information about both the Swing Timer object and the
java.util.Timer and have chosen the java.util.Timer because I don't need
this polling process to interact with the UI.
Problem: The Timer object fires on a periodic basis, calling an object that
extends TimerTask - this works fine. The run method of the object that
extends TimerTask reads a text file that contains a list of files to poll
for, and loops through the list to determine if the files are present in the
designated folder. If a file is present, logic formats the file, and uses a
FileOutputStream to save the file. The problem is after looping through
and formatting a list of ten files (3kb each), the only file that is
actually saved is the last file in the list. I have event tried adding the
FileOutputStream.flush() method to hopefully force file save - this didn't
work. When I step through the code in the debugger, all ten files are
saved. What am I doing wrong?
***Here's the controlling code... placed in the JBuilder application object
/**
* timerInit
* Parameters : None
* Returns : None
*/
private void timerInit(Integer time) {
Timer t = new Timer(true);
int delay = 30000;
t.schedule(new Test(), delay, delay);
try {
Thread.currentThread().sleep(12000);
} catch (java.lang.InterruptedException e) {}
}
***Here's the remaining code...
public class Test extends TimerTask {
private StringBuffer file;
public Test() {}
public void run() {
String path;
Vector vectFiles = new Vector();
Properties prop = new Properties();
try{
// Get properties file
prop.load(new FileInputStream("bbpositions"));
// Get a list of files
vectFiles = getFiles();
// Loop thru all elements in vector
for(int i = 0; i<vectFiles.size(); i++) {
String row[] = (String[])vectFiles.elementAt(i);
path = prop.getProperty("SSBPath") + "\\" + row[6];
// Test each file to determine if in directory
if(new File(path).exists()) {
// Call createFile
createFile(row);
}
}
} catch (Exception e) {
cancel();
}
}
/**
* getFiles
* Parameters : None
* Returns:
* vectFiles : Vector containing list of files to upload.
*/
public Vector getFiles() {
Vector vectFiles = new Vector();
try {
BufferedReader in = new BufferedReader(
new FileReader(".\\bbpositions_portfolios.txt"));
int rowNbr = 0;
String temp;
while((temp = in.readLine()) != null) {
StringTokenizer tok = new StringTokenizer(temp, "\",", false);
String row[] = new String[8];
row[0] = tok.nextToken(); // id
row[1] = tok.nextToken(); // portfolio id
row[2] = tok.nextToken(); // action
row[3] = tok.nextToken(); // portfolio name
row[4] = tok.nextToken(); // currency
row[5] = tok.nextToken(); // filing date
row[6] = tok.nextToken(); // file name
vectFiles.addElement(row);
}
in.close();
} catch (FileNotFoundException e1) {
} catch (IOException e2) {
}
return vectFiles;
}
/**
* createFile
* Parameters: None
* Returns: None
*/
public synchronized void createFile(String[] row) {
Properties prop = new Properties();
try{
prop.load(new FileInputStream("bbpositions"));
file = new StringBuffer();
file.append("SYSTEM=" + prop.getProperty("System") + "\r\n");
file.append("USERNUMBER=" + prop.getProperty("UserNumber") + "\r\n");
file.append("LOGIN=" + prop.getProperty("Login") + "\r\n");
file.append("CLIENT_ID=" + prop.getProperty("ClientID") + "\r\n");
file.append("PORTFOLIO_ID=" + row[1] + "\r\n");
file.append("ACTION=" + row[2] + "\r\n");
file.append("PORTFOLIO_NAME=" + row[3] + "\r\n");
file.append("PORTFOLIO_CURRENCY=" + row[4] + "\r\n");
file.append("FILING_DATE=" + row[5] + "\r\n");
file.append("DELIMITER=" + prop.getProperty("Delimiter") + "\r\n");
file.append("FIELDS=" + "ID_TYPE ID DUM SIZE COST\r\n");
file.append("START_OF_DATA\r\n");
// procedure for rows here
file.append("END_OF_DATA\r\n");
file.append("END_OF_FILE\r\n");
writeFile(file.toString());
} catch (java.io.FileNotFoundException ex1) {
} catch (java.io.IOException ex2) {
}
}
/**
* writeFile
* Parameters:
* file : Name to use for file
* Returns: None
*/
private void writeFile(String file) {
String name = ".\\P" + getDate() + ".csv";
if(!file.equals(null) && !file.equals("")) {
try {
FileOutputStream fs = new FileOutputStream(name);
fs.write(file.getBytes());
fs.flush();
fs.close();
(new File(name)).deleteOnExit();
} catch (IOException e) {}
}
}
/**
* getDate
* Paramters : None
* Returns : java.util.Date
*/
private String getDate() {
SimpleDateFormat dateFmt = new SimpleDateFormat("yyyyMMdd'_'hhmmss");
java.util.Calendar cldr = Calendar.getInstance();
return dateFmt.format(cldr.getTime());
}
}
Technical Details
Message-ID:
<3ae71016$1_2@dnews>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!194.25.134.62!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.gwi-ag.com!dnewsShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,aed21b73e5577e91,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-25 11:19:57 PST From: "Stace Merkel" <stace_merkel@calpers.ca.gov> Newsgroups: borland.public.jbuilder.java.language Subject: java.util.Timer Date: Wed, 25 Apr 2001 10:58:30 -0700 Lines: 161 Organization: CalPERS X-Newsreader: Microsoft Outlook Express 4.72.3612.1700 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3612.1700 NNTP-Posting-Host: 199.164.56.5 Message-ID: <3ae71016$1_2@dnews> X-Trace: dnews 988221462 199.164.56.5 (25 Apr 2001 10:57:42 -0700) Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!194.25.134.62!newsfeed00.sul.t-online.de!t-online.de!news1.dtag.de!news.gwi-ag.com!dnews