Reputation: 740
try {
System.out.println(f.getAbsoluteFile());
// FileInputStream input = new FileInputStream(f);
FileInputStream inp = new FileInputStream("C:\\Users\\Jamal\\Desktop\\Japaneseword1.xls");
Workbook workbook = new HSSFWorkbook(inp);
FileOutputStream output = new FileOutputStream("C:\\Users\\Jamal\\Desktop\\Japaneseword2.xls");
Sheet sheet2 = workbook.getSheet("Japaneseword");
int num = sheet2.getLastRowNum();
num=++;
System.out.print(num);
Row row = sheet2.createRow(num);
Cell cell = row.createCell(0);
cell.setCellValue(Japaneseword);
Cell cell2 = row.createCell(1);
cell2.setCellValue(Japaneseword);
Cell cell3 = row.createCell(2);
cell3.setCellValue(Japaneseword);
Cell cell4 = row.createCell(3);
cell4.setCellValue(Japaneseword);
Cell cell5 = row.createCell(4);
cell5.setCellValue(Japaneseword);
Cell cellwebsite = row.createCell(5);
workbook.write(output);
}
catch(Exception e){
e.printStackTrace();
}
}
The purpose of this code is just to save the contents,add a row, and add the user's input to the excel file. I used InputStream
to attempt the save the previous entries,then add new entries, and use the outputstream
to print out excel file. I have done this in the past with no problems, but for some reason inputstream
is not saving correctly. I am very frustrated at this problem. Would love to know why this is happening.
Thanks.
--- Update ---
Okay, update, for some reason or another, it works occasionally. See if I create the file, using the above method it won't copy correctly. But if I change the file name in the constructor of the outputstream
method to a different name on the excel document to something arbitrary it starts counting the rows. It then saves the data correctly and works as it is intended. Does this make sense?
Basically, after I create the file, I then have to go back, change the code, then it saves and copys the data, and I can continue to edit the file. This solution is not practical.
Upvotes: 0
Views: 125
Reputation: 9405
put the closing statements in the finally block
try{
...
}catch(Exception ex){
}finally{
output.close();
inp.close();
}
to ensure that the input/output streams are closed/released even if a an exception is thrown.
Upvotes: 1
Reputation: 22074
You must close the streams, especially the outputstreams. Otherwise you can loose data.
At the end you must do
output.close();
inp.close();
Upvotes: 2