Advertisement

04.22.2008 at 07:08PM PDT, ID: 23345264
[x]
Attachment Details

Why my java program cannot write file to Linux's directory?

Asked by mawingho in Java Programming Language, Linux, MySQL

This is my Java program, it works fine on Windows XP. However, when I run it on Linux, it cannot write file even add new record to the database, I don't know why. The main point is, this java program does not throw any exception or even stop....................Start Free Trial
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:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
import java.io.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
 
public class fetchDeviceData extends Thread {
 
	/* Database information */
    private String user = "user";
    private String password = "password";
 
	/* thread information */
	private final int max_number_thread = 2;
	private static int threadCount = 0;
 
	private fetchDeviceData parent;
	private int fetcher_number;
	private String device_name;
	
	/* Timing nformation */
	private String start_time = "";
	private String end_time = "";
	
	/* Device Data */
	private String equipment_id_list[];
	private String result[][];
 
	/* Contructor for main function */
	public fetchDeviceData (){
	}
	
	/* Constructor for child thread */
	public fetchDeviceData (fetchDeviceData parent, int fetcher_number, String device_name){
		this.parent = parent;
		this.fetcher_number = fetcher_number;
		this.device_name = device_name;
	}
	
	/* Main method */
	public static void main(String [] args){
		fetchDeviceData controller = new fetchDeviceData();
		controller.set_start_time();
		controller.delete_log();
		controller.drop_all_tables("device_info");
		controller.delete_all_device_data_files();
		controller.delete_failed_data();
		controller.get_equipment_id_list();
		controller.create_fetcher();
	}
	
	/* Run method for child thread */
	public void run(){
		write_log("fetcher " + fetcher_number + ": " + device_name + " started");
		fetch_device_data(device_name);
		if(result != null && result.length > 0){
			write_device_data(device_name);
			store_device_data(device_name);
		}else{
			write_failed_data(device_name);
		}
		
		write_log("fetcher " + fetcher_number + ": " + device_name + " done");
		parent.notify(fetcher_number, device_name);
	}
	
	/* Create new thread */
	public void create_fetcher(){
		write_log("Start Time: " + start_time);
		write_log("Please wait while the data is being prepared");
		write_log("Number of Device: " + equipment_id_list.length + "\n");
		
		fetchDeviceData [] collector = new fetchDeviceData[max_number_thread];
		
		for(int i=0; i<equipment_id_list.length&&i<max_number_thread; i++){
			String device_name = equipment_id_list[i];
			collector[i] = new fetchDeviceData(this, i+1, device_name);
			threadCount++;
		}
		
		for(int j=0; j<max_number_thread; j++){
			fetchDeviceData d = collector[j];
			d.start();
		}
	}
 
	/* For child thread to notify parent */
	public synchronized void notify(int thread_id, String thread_name){
		
		if(threadCount < equipment_id_list.length){
			String device_name = equipment_id_list[threadCount];
			new fetchDeviceData(this, threadCount+1, device_name).start();
			threadCount++;
		}
		
		update_end_time();
		
		if(thread_id == equipment_id_list.length){
			write_log("\nThe entire process is finished");
			write_log("Start Time: " + start_time);
			write_log("End Time: " + end_time);
			write_log("Number of Threads: " +  threadCount);
			System.exit(0);
		}
	}
	
	/* Get current date and time */
	public String get_current_time(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		java.util.Date date = new java.util.Date();
		String datetime = dateFormat.format(date);
		return datetime;
	}
	
	/* set start time */
	public void set_start_time(){
		this.start_time = get_current_time();
	}
	
	/* Update end time */
	public void update_end_time(){
		this.end_time = get_current_time();
	}
	
	/* Create a database connection */
	public Connection connect_database(String database_name){
 
		Connection conn = null;
	
        try{
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            write_log("Cannot load Driver");
            System.exit(0);
        }
 
        try{
            String url = "jdbc:mysql://localhost/" + database_name + "?user=" + user + "&password=" + password;
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            write_log("Cannot open Connection");
			write_log(e.getMessage());
            System.exit(0);
        }
		
		return conn;
 
    }
	
	/* Create a database connection to other server */
	public Connection connect_database(String host, String database_name){
 
		Connection conn = null;
	
        try{
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            write_log("Cannot load Driver");
            System.exit(0);
        }
 
        try{
            String url = "jdbc:mysql://" + host + "/" + database_name + "?user=" + user + "&password=" + password;
            conn = DriverManager.getConnection(url);
        } catch (SQLException e) {
            write_log("Cannot open Connection");
			write_log(e.getMessage());
            System.exit(0);
        }
		
		return conn;
 
    }
	
	/* Drop all tables */
	public void drop_all_tables(String database_name){
		try{
			Connection conn = connect_database(database_name);
			Statement stmt = conn.createStatement();
			String sql = "SHOW TABLES FROM " + database_name;
			ResultSet rs = stmt.executeQuery(sql);
			
			int number_of_table = 0;
			String table_name_list[] = null;
			
			if(rs != null){
				rs.last();
				number_of_table = rs.getRow();
			}
			
			if(number_of_table > 0){
				table_name_list = new String [number_of_table];
				
				rs.first();
				
				int i=0;
				
				do{
					table_name_list[i] = rs.getString(1);
					i++;
				}while(rs.next());
				
				
				for(i=0; i<table_name_list.length; i++){
					sql = "DROP TABLE IF EXISTS " + table_name_list[i];
					stmt.executeUpdate(sql);
				}
			}
			
		} catch (SQLException e) {
            write_log("There are no table in the database " + database_name);
        }
		
	}
	
	/* Delete all the .cvs files */
	public void delete_all_device_data_files(){
		File dir = new File("./device_data");
		
		if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                boolean success = new File("./device_data/" + children[i]).delete();
                if (!success) {
                    // Deletion failed
                }
            }
        }
	}
	
	/* Delete failed device.txt */
	public void delete_failed_data(){
		boolean success = (new File("./failed_device_data/failed_device.txt")).delete();
	    if (!success) {
	        // Deletion failed
	    }
	}
	
	/* Delete log.txt */
	public void delete_log(){
		boolean success = (new File("./log.txt")).delete();
	    if (!success) {
	        // Deletion failed
	    }
	}
	
	/* Get equipment id list */
	public void get_equipment_id_list(){
 
		/*
		equipment_id_list = new String[2];
		equipment_id_list[0] = "CHW2B033WAV";
		equipment_id_list[1] = "ABE0C0512BSR";
		*/
		
        try{
			Connection conn = connect_database("10.247.5.10", "mcs");
            Statement stmt = conn.createStatement();
			String sql = "SELECT COUNT(DISTINCT(EquipID)) " +
			"FROM installation " +
			"WHERE " +
			"(EquipID like \"_________BSR\" " +
			"or EquipID like \"_________WA_\" " +
			"or EquipID like \"_________WB_\" or " +
			"EquipID like \"_________WC_\" or " +
			"EquipID like \"_________RA_\") " +
			"and status = \"Installed\" " +
			"ORDER BY EquipID";
			
			ResultSet rs = stmt.executeQuery(sql);
			
			rs.next();
			
			int array_size = rs.getInt(1);
			
			equipment_id_list = new String[array_size];
			
			// SELECT DISTINCT(EquipID) FROM installation WHERE (EquipID like "_________BSR" or EquipID like "_________WA_" or EquipID like "_________WB_" or EquipID like "_________WC_" or EquipID like "_________RA_") and status = "Installed" ORDER BY EquipID;
 
			sql = "SELECT DISTINCT(EquipID) " +
			"FROM installation " +
			"WHERE " +
			"(EquipID like \"_________BSR\" " +
			"or EquipID like \"_________WA_\" " +
			"or EquipID like \"_________WB_\" or " +
			"EquipID like \"_________WC_\" or " +
			"EquipID like \"_________RA_\") " +
			"and status = \"Installed\" " +
			"ORDER BY EquipID";
            rs = stmt.executeQuery(sql);
			
			int i=0;
			while(rs.next()){
				equipment_id_list[i] = rs.getString("EquipID");
				i++;
			}
			
        } catch (SQLException e) {
            write_log("Cannot process Query");
			write_log(e.getMessage());
            System.exit(0);
        }
 
    }
	
	/* Fetch device data from remote server */
	public void fetch_device_data(String device_name){
 
        String s = null;
		String data = null;
 
        try {
            
            Process p = Runtime.getRuntime().exec("wget -O - http://user:pass@61.10.0.168/cgi-bin/chkdocoper_chid.cgi?"+device_name);
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));
			
            while ((s = stdInput.readLine()) != null) {
				data += s;
            }
        }
        catch (IOException e) {
            write_log("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
		
		if(data != null){
			int first_pos = data.indexOf("<table");
			int last_pos = data.lastIndexOf("</table>");
			
			if(first_pos != -1 && last_pos != -1){
				data = data.substring(first_pos, last_pos + 8);
				data = data.replaceAll("<[/]{0,1}table[^>]*>", "");		//Remove all table tags
				data = data.replaceAll("<(tr|td)[^>]*>", "<$1>");		//Remove all the attributes in tr and td tags
				data = data.replaceAll("<([/]{0,1}[a-z]*)>[ ]*", "<$1>").trim();
				data = data.replaceAll("</(td|tr)>", "");				//Remove all ending tr and td tags
				data = data.replaceFirst("<tr>", "");				//Remove the first tr tag, which is not required, during split
				
				String temp[] = data.split("<tr>");
				result = new String[temp.length][];
				for(int i=0; i<temp.length; i++){
					temp[i] = temp[i].replaceFirst("<td>", "");
					result[i] = temp[i].split("<td>");
				}
			}
		}
    }
	
	/* Write data to .cvs file */
	public void write_device_data(String device_name){
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("./device_data/" + device_name +".csv", true));
			
			boolean modem_timeout = false;
			String record = "";
			
			for(int i=0; i<result.length; i++){
				record = "";
				
				for(int j=0; j<result[i].length; j++){
				
					if(result[i][j].indexOf("ModemTimeout!") != -1){
						modem_timeout = true;
					}
						
					record += "\"" + result[i][j] + "\"";
					
					if(j+1 < result[i].length){
						record += ",";
					}
				}
				record += "\n";
				
				if(modem_timeout == false){
					out.write(record);
				}
				
				modem_timeout = false;
			}
			
	        out.close();
			
		}catch(IOException e){
		}
	}
	
	/* Write failed data to failed device.txt */
	public void write_failed_data(String device_name){
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("./failed_device_data/failed_device.txt", true));
	        out.write(device_name + "\n");
	        out.close();
		}catch(IOException e){
		}
	}
	
	/* Write log */
	public void write_log(String line){
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter("./log.txt", true));
	        out.write(line + "\n");
	        out.close();
		}catch(IOException e){
		}
		
		System.out.println(line);
	}
	
	/* Store data to database */
	public void store_device_data(String device_name){
		try{
			Connection conn = connect_database("device_info");
			Statement stmt = conn.createStatement();
			String sql = "DROP TABLE IF EXISTS " + device_name;
			stmt.executeUpdate(sql);
			
			if(result != null && result.length > 0){
				sql = "CREATE TABLE " + device_name + "( ";
				
				for(int i=0; i<result[0].length; i++){
					String attribute = result[0][i].replace(" ", "_");
					
					if(attribute.equals("Index")){
						attribute = "Id";
					}
					
					sql += attribute + " VARCHAR(100)";
					
					if(i+1 < result[0].length){
						sql += ",";
					}
				}
			
				sql += " )";
				stmt.executeUpdate(sql);
			
				int i=0;
				int j=0;
				int k=0;
				
				boolean modem_timeout = false;
				
				for(i=1; i<result.length; i++){
					sql = "INSERT INTO " + device_name + " VALUES(";
					for(j=0; j<result[i].length; j++){
						
						if(result[i][j].indexOf("ModemTimeout!") != -1){
							modem_timeout = true;
						}
						
						sql += "\"" + result[i][j] + "\"";
						
						if(j+1 < result[i].length){
							sql += ",";
						}
					}
					
					/* add NULL if data not enough */
					for(k=j; k<result[0].length; k++){
						sql += ",NULL";
					}
					
					sql += ")";
					
					if(modem_timeout == false){
						stmt.executeUpdate(sql);
					}
					
					modem_timeout = false;
				}
				
				sql = "INSERT INTO cable_modem.cable_modem SELECT NOW(), \"" + device_name + "\", Uid, COUNT(Uid), AVG(RX), AVG(TX), AVG(SNR), STD(RX), STD(TX), STD(SNR) FROM device_info." + device_name + " GROUP BY Uid";
				stmt.executeUpdate(sql);
			}
			
		} catch (SQLException e) {
            write_log("Cannot process Query");
			write_log(e.getMessage());
            System.exit(0);
        }
	}
	
}
[+][-]04.22.2008 at 07:14PM PDT, ID: 21417280

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.22.2008 at 07:21PM PDT, ID: 21417315

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.22.2008 at 08:09PM PDT, ID: 21417505

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.22.2008 at 10:54PM PDT, ID: 21418012

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.22.2008 at 11:58PM PDT, ID: 21418268

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, Linux, MySQL
Sign Up Now!
Solution Provided By: CEHJ
Participating Experts: 4
Solution Grade: A
 
 
[+][-]05.05.2008 at 05:56AM PDT, ID: 21499634

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628