Je travaille sur un projet où il est nécessaire d'aller chercher un enregistrement à partir d'une table et de l'écrire dans le fichier plat comme txt. Donc j'utilise BufferedWriter de Java IO api pour le faire. Mais malheureusement, le fichier ne semble pas être écrit.Écrire la sortie dans un fichier via le jeu de résultats
Voici ce que je suis en train:
public class PrintConnectionStatistics {
public static void main(String[] args) {
String userName = "root";
String pass = "root";
String url = "jdbc:mysql://localhost:3306/monitoring";
String driver ="com.mysql.jdbc.Driver";
Connection con = null;
Statement stm = null;
ResultSet rs = null;
try{
//registering the driver.
Class.forName(driver);
con = DriverManager.getConnection(url,userName,pass);
stm = con.createStatement();
String sql = "SELECT id, start_time, end_time, layer_name, client, value, created_on, parameter FROM monitoring.status ORDER BY start_time DESC, value DESC";
rs = stm.executeQuery(sql);
StringBuilder statRow = null;
StringBuilder cellVal = null;
String columnSeperator = ",";
int rowCounter = 0;
if(rs != null){
long startTimeTracker = 0;
File file = new File("E:/Files/User Tracking/ConnectionTrackingReport.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
//true = append file. This is for open file in append mode
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bw = new BufferedWriter(fileWritter);
while (rs.next()){
rowCounter++;
long startTime = rs.getLong("start_time");
long end_time = rs.getLong("end_time");
String layer = rs.getString("layer_name");
String client = rs.getString("client");
int val = rs.getInt("value");
if(startTime != startTimeTracker){
//reset start time tracker
startTimeTracker = startTime;
//next column started
if(statRow != null){
//append to output file
// System.out.println(statRow.toString());
bw.write(statRow.toString());
statRow = null;
}
statRow = new StringBuilder();
String strStartTime = converMillisecondsToString(startTime);
String strEndTime = converMillisecondsToString(end_time);
statRow.append(strStartTime).append("-").append(strEndTime);
}
cellVal = new StringBuilder();
if(client != null && !("".equals(client.trim()))){
cellVal.append(client.trim());
}
else{
cellVal.append(layer);
}
cellVal.append("-").append(val);
statRow.append(columnSeperator).append(cellVal.toString());
cellVal = null;
}
//this is for last row
if(statRow != null){
//append to output file
// System.out.println(statRow.toString());
bw.write(statRow.toString());// I am trying output here to the file which fails to write. But System.out.println print it to the console properly.
statRow = null;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
con.close();
con = null;
}
catch(Exception e){
}
}
}
}
La sortie ressemble à ceci:
12:39:35-12:39:35,MYSQL-62,demo-1
12:39:35-12:39:35,MYSQL-57,demo-1
Je ne comprends pas ce qui ne va pas avec le code. Cependant, il crée un fichier sur un emplacement donné mais n'y écrit pas de données. S'il vous plaît, corrigez-moi là où j'ai tort.
Je pense que vous avez oublié de vider l'objet BufferedWriter. Appelez bw.flusf() à la fin. – zombie