Automate sequential file opening?

Asked

Viewed 53 times

0

I want to open several binary Java files at once, but I don’t want to instantiate them all manually this way as follows in example. Is there an automatic way, or a method so I can speed this up?

Example:

File arquivo  = new File("temp0.bin");
FileInputStream fis0  = new FileInputStream(arquivo);
DataInputStream dis0  = new DataInputStream(fis0);

arquivo  = new File("temp1.bin");
FileInputStream fis1  = new FileInputStream(arquivo);
DataInputStream dis1  = new DataInputStream(fis1);

arquivo  = new File("temp2.bin");
FileInputStream fis2  = new FileInputStream(arquivo);
DataInputStream dis2  = new DataInputStream(fis2);

arquivo  = new File("temp3.bin");
FileInputStream fis3  = new FileInputStream(arquivo);
DataInputStream dis3  = new DataInputStream(fis3);
  • Wesley, no need to quote the language in the title, since the question already has the tag, it is redundant.

  • First time here, I’ll get the hang of it yet.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

1

This?

ArrayList<File> arquivo = new ArrayList<File>();
ArrayList<FileInputStream> fis = new ArrayList<FileInputStream>();
ArrayList<DataInputStream> dis = new ArrayList<DataInputStream>();
for (int i = 0; i < 4; i++) {
    arquivo.add(new File("temp" + i.toString() + ".bin");
    fis.add(new FileInputStream(arquivo));
    dis.add(new DataInputStream(fis0));
}

I put in the Github for future reference.

Whenever you need to vary something use a variable. Whenever there is a repetition use a loop while or for. If you need to maintain multiple states use one array or a list.

  • So I could even do this but I need all the files opened, so in case I’m not mistaken, dis0 will be associated only with the last file.

  • So? You can do better than this, but it depends on more advanced things that you should not yet know.

  • Give your mess so I tell you if I know about it or not, but then what your idea an arraylist of Datainputstream, I’ve been thinking about it I’m just not sure if it’s viable.

  • Obs: when I was typing you updated!.

  • In this case I think I only need the Data input Arraylist, the other values are expendable, so there is no need to persist them.

  • I think several things can be done but your example and description of the problem is very limited, so I can’t do the way I would because it might not suit your case.

  • So vlw, that will be enough for me to solve my problem.

Show 2 more comments

0

Something like that? It is possible from Java 7:

File raiz = new File("/home/usuario/Documents/files/");
File[] files = raiz.listFiles();

for (File file : files) {
  if(file.isFile() && file.getName().endsWith(".bin")){
   //   ...
  }
} 

In java 8 you can still use Ambdas if you want to:

Files.newDirectoryStream(Paths.get("."),
    path -> path.toString().endsWith(".bin"))
    .forEach(System.out::println);
  • No my problem can be solved with the same arraylist, anonymous functions are cool, however java not used yet also does not leave the code very readable, only in Haskell that I used more. But vlw for your contribution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.