-2

I'm new to Java and need help as in How I should go ahead? I wrote a Java Program, which takes

FileInputStream(t$i.txt) //input to Java Program

and produces

FileOutputStream(model$i.txt) //Output of Java Program

$i runs from 0 to 100. There is a directory which contains files and shall be iterated in FileInputstream as below.

t0.txt
t1.txt
t2.txt
t3.txt
...
...
...
t100.txt

And produce corresponding FileoutputStream as below.

model0.txt
model1.txt
model2.txt
model3.txt
.
.
.
model100.txt

Reference: How do I iterate through the files in a directory in Java?

Community
  • 1
  • 1
Linguist
  • 123
  • 1
  • 10

1 Answers1

1

Something like

    for (int i = 0; i <= 100; i++) {
        File in = new File("t" + i + ".txt");
        if (in.exists()) {
            try (
                FileInputStream fis = new FileInputStream(in);
                FileOutputStream fos = new FileOutputStream("model" + i + ".txt")) {
                // write something on fos depending on fis;
            }
        }
    }

where the try-with-resources construction takes care of closing the streams.

Bram
  • 479
  • 2
  • 10