Search code examples
javaiofilenames

Name file outputStream with a running Number


I am Splitting large files along a Line END OF STATEMENT and writing new files. I need to name the files with a running number i.e Statement1, Statement2.... Here is what I have:

String[] filenames1 = statements.list(only);//only is filenamefilter
        int count;
        for (int k = 0; k < filenames1.length; k++) {
            try {
                FileInputStream fs = new FileInputStream("C:/statements/" + filenames1[k]);
                System.out.println(filenames1[k]);
                BufferedReader br = new BufferedReader(new InputStreamReader(fs));
                count = 0;
                FileOutputStream fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
                while ((lines = br.readLine()) != null) {
                    String mine = lines.trim();
                    if (mine.startsWith("********END OF STATEMENT********")) {
                        bw.close();
                        fos.close();
                        count++;
                        fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
                        bw = new BufferedWriter(new OutputStreamWriter(fos));
                        continue;
                    }
                    if (mine.isEmpty()) {
                        continue;
                    }
                    bw.write(lines);
                    bw.newLine();
                    bw.flush();
                }

                fs.close();
                br.close();
                fos.close();
                bw.close();
            } catch (Exception e) {
                System.out.println("Exception: " + e);
            }
        }

I am getting only one file with Statement0 Meaning the names are getting overwritten. What Exactly am I doing wrong with count++


Solution

  • I actually got it. Initializing count outside the for loop somehow solved it.

    String[] filenames1 = statements.list(only);//only is filenamefilter
            int count = 0;
            for (int k = 0; k < filenames1.length; k++) {
                try {
                    FileInputStream fs = new FileInputStream("C:/statements/" + filenames1[k]);
                    System.out.println(filenames1[k]);
                    BufferedReader br = new BufferedReader(new InputStreamReader(fs));
                    FileOutputStream fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
                    while ((lines = br.readLine()) != null) {
                        String mine = lines.trim();
                        if (mine.startsWith("********END OF STATEMENT********")) {
                            bw.close();
                            fos.close();
                            count++;
                            fos = new FileOutputStream("C:/ABC Statements/Statement" + count + ".RPT");
                            bw = new BufferedWriter(new OutputStreamWriter(fos));
                            continue;
                        }
                        if (mine.isEmpty()) {
                            continue;
                        }
                        bw.write(lines);
                        bw.newLine();
                        bw.flush();
                    }
    
                    fs.close();
                    br.close();
                    fos.close();
                    bw.close();
                } catch (Exception e) {
                    System.out.println("Exception: " + e);
                }
            }