I am reading a excel file which has contains some informations in it, i am able to read that excel file using apache POI. And i am able to print the cell values to the console.But my requirement is that i have to convert the output to a text file. I am giving the code which i have done so far ,
public class TestReadExcel {
public static void main(String[] args) throws IOException {
    Writer writer = null;
    try {
    String excelFilePath = "D:\\resources\\myExcel.xlsx";
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet firstSheet = workbook.getSheetAt(0);
    File file = new File("D:\\writedatasamp.txt");  
    writer = new BufferedWriter(new FileWriter(file));
    Iterator<Row> iterator = firstSheet.iterator();
    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue());
                    writer.write(cell.getStringCellValue());
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    writer.write(String.valueOf(cell.getBooleanCellValue()));
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue());
                    writer.write(String.valueOf(cell.getNumericCellValue()));
                    break;
            }
            System.out.print(" - ");
        }
        System.out.println();
    }
    workbook.close();
    inputStream.close();
 }catch(Exception e) {
     e.printStackTrace();
 }finally {
     writer.close();
 }
}
  }
The output is coming to the console , but i have to generate a text file of the output how i can do that ? please help
 
    