All,
I am working on a simple program that reads temperature, humidity, and dew point from a sensor and logs the result to a .csv file. I am using netbeans and using alt-enter to surround code with try-catch whenever netbeans suggests. The result is pretty ugly and I can't imagine that a professional java developer would do things this way.
Would someone mind looking at this code and suggesting ways clean it up?
Also, netbeans is putting loggers in all the catch blocks and I don't know what the logger is for or how to read it.
Thanks,
Bill
Here's the code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package logomega;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author wdrago
*/
public class LogOmega {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
String host = "xxx.xxx.xxx.xxx";
int port = 2000;
String tmp = null;
String hum = null;
String dew = null;
File directory = new java.io.File("."); //Same dir as this program.
String logFileName = directory + "\\templog.csv";
FileWriter outFile = null;
BufferedWriter outStream;
Omega iThx = new Omega(host, port);
while (true) {
try {
iThx.connect();
} catch (UnknownHostException ex) {
System.out.println("Cannot connect to " + host + ":" + port + ". Unknown host");
//Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
System.out.println("IO Exception while connecting to " + host + ":" + port);
//Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
}
try {
tmp = iThx.readTemperature();
hum = iThx.readHumidity();
dew = iThx.readDewpoint();
} catch (IOException ex) {
System.out.println("IO Exception while reading from " + host + ":" + port);
}
try {
iThx.close();
} catch (IOException ex) {
Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
}
try {
outFile = new FileWriter(logFileName, true); //true means append to file
} catch (IOException ex) {
Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
}
outStream = new BufferedWriter(outFile);
try {
outStream.write(tmp + "," + hum + "," + dew + "\r\n");
} catch (IOException ex) {
Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
}
try {
outStream.close();
} catch (IOException ex) {
Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
}
try {
outFile.close();
} catch (IOException ex) {
Logger.getLogger(LogOmega.class.getName()).log(Level.SEVERE, null, ex);
}
Thread.sleep(6000); // 6 seconds
System.out.println("Logging...");
} // end while
} // end main()
} // end class