• Post Reply Bookmark Topic Watch Topic
  • New Topic

Iterate through a string of file paths in a directory  RSS feed

 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written code to copy the file paths of a number of files in a directory into an array.

I now want to take the array of file paths and loop through them one at a time and apply each file path to a FileInputStream within a BufferedReader (so that each file is opened and processed individually and closed).

Is there a general way to go about doing that? It seems a for loop would work, but what about Scanner to scan the array line by line?
 
Paul Murray-Cbr
Greenhorn
Posts: 12
Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say that you have copied the file paths, do you mean that you have an array of string and you have copied the file paths into there? Where are you getting these paths from? Are you just scanning a directory? If so, you would generally use File.listFiles and a for() loop.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a FileNameFilter that searches a directory for all files with the ".PAT" extension. This returns a list of file paths like so:

M:\\directory\\file1.PAT
M:\\directory\\file2.PAT
M:\\directory\\file3.PAT

I then have a FileInputStream within a BufferedReader that will read the file path and process the file accordingly.

I am trying to figure out the best way to iterate through the file paths, process the files into a new text file, then move onto the next file path.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a method that does whatever you want for a single path and then call it in a loop iterating over your collection of paths.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I'm trying to figure out. This is what I have so far, but it's not quite working the way I intend:




 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why don't you increment i inside the for statement?
It is hard to say what your code does (because you posted very little code).
And you forgot to put a semicolon after i++.
And try to indent it correctly so it is more readable.

Why do you mean by: it does not work? Remember: ItDoesntWorkIsUseless
 
chris webster
Bartender
Posts: 2407
36
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find it easier to iterate over your list of file-paths using a foreach loop.
 
Matt Wallace
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my full code. This code returns all of the data from the files I am wishing to process, however the formatter prints each line of data subsequntly, instead of processing one file, returning the file strings of data (H2OIN, CO2IN, etc) then moving to the next file.

I wish to process and extract the file name and five strings of data from each file, write the information to the output text file, then move to the next file path. When I test the writer with a single input file, the data outputs in the proper format.


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!