0
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.

2 Answers 2

2

You must close the streams, especially the outputstreams. Otherwise you can loose data.

At the end you must do

 output.close();
 inp.close();
Sign up to request clarification or add additional context in comments.

1 Comment

inp.close() immediately after the constructor, maybe with its own try-catch-finally, FileNotFoundException is of interest. Only after closing a file, one might again write to it, if living in a perfect world.
1

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.

1 Comment

Thank you all for timely response,but I have tried your solution and problem pursuits. I closing all streams, but it still writes over existing data, but the odd thing is that, if I run the program once, then take the outputstream change name of the file in the constructor run it again it works. I just make sure to change the inputstream to the new name of the outstream it works perfectly. I don't understand this at all. I would greatly appreciate any more solutions. As I new to Java, and I want to learn from this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.