'jTable saving each row element in the new line in the .txt file
I have a problem with saving the jTable contents to a file. Every row
data printed to file makes a new line along.
So reading from .txt
looks like this:
Here is my code:
try {
FileWriter fw = new FileWriter(file, false);
BufferedWriter bw = new BufferedWriter(fw);
for(int i=0; i<jTable2.getRowCount(); i++){
for(int j=0; j<jTable2.getColumnCount(); j++){
bw.write(jTable2.getModel().getValueAt(i, j) + ";");
}
bw.newLine();
}
bw.close();
fw.close();
} catch (IOException ex) {
Logger.getLogger(HomeGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
BufferedReader br = new BufferedReader(new FileReader(file));
DefaultTableModel model = (DefaultTableModel) jTable3.getModel();
Object[] lines = br.lines().toArray();
/* */
for (Object line : lines) {
String[] rowData = line.toString().split(";");
model.addRow(rowData);
}
What I want is for every row
to be printed in the same line in the .txt
but for some reason every time write
is called, it makes new line in a file.
Solution 1:[1]
he guys. I can solve your problem:
for(int i = 0; i < lines.length; i++){
String[] rowData = line[i].toString().split(";");
model.addRow(rowData);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | 2471_Tr??ng Thiên L?nh |