Forums Register Login

Iterate through a string of file paths in a directory

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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:




+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
You might find it easier to iterate over your list of file-paths using a foreach loop.
+Pie Number of slices to send: Send
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.


I'm not dead! I feel happy! I'd like to go for a walk! I'll even read a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1396 times.
Similar Threads
BufferedReader contents into Array
Reading through Files in a Directory
Array.clone() not producing a distinct copy?
Reading IE favorite shortcut url?
Reading a text file and storing the values in appropriate places
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:10:23.