Closing a Stream after instantiating a bufferedWriter

80 Views Asked by At

Just wondering if my bufferedWrites will work outside this try clause as it should as. i atm have no way of checking the outcome atm but. As in i only need the stream to be open for the instantiation of the BufferedWriter correct. So this code should work:

    BufferedWriter bufferedWriter;
    // ServletOutputStream responseOutputStream = null;

    FileOutputStream fos = new FileOutputStream(file);

    if (zip) {
        try (ZipOutputStream zOut = new ZipOutputStream(fos);) {
            String fileName = file.getName().replace(".zip", "");
            zOut.putNextEntry(new ZipEntry(fileName));
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(zOut, Charset.forName(enc)));
        }
    } else {
        try (OutputStreamWriter opsw = new OutputStreamWriter(fos, Charset.forName(enc))) {
            bufferedWriter = new BufferedWriter(opsw);
        }
    }

    File fin = new File(tempFile);
    FileInputStream fis = new FileInputStream(fin);
    BufferedReader bufferedReader;
    try(InputStreamReader ipsr = new InputStreamReader(fis)){
        bufferedReader = new BufferedReader(ipsr);
    }
    String aLine = null;
    int rowCount = 1;

    logger.info("Copy file for output");
    while ((aLine = bufferedReader.readLine()) != null) {
        if (rowCount == 1) {
            // Ful fix wax klarar nog inte att s�tta encoding.
            if (encoding.equals(ProductFeedOutputController.ENCODING_88591)) {
                bufferedWriter.write("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");

            } else {
                bufferedWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            } 
            ......
0

There are 0 best solutions below