Let's say, I have a folder called maps
and inside maps
I have map1.txt
, map2.txt,
and map3.txt
. How can I use Java and the BufferReader
to read all of the .txt
files in folder maps
(if it is at all possible)?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
With
NIO
you can do the following:To read the file contents you may use
Files#readString
but, as usual, you need to handleIOException
inside lambda expression.Something like the following should get you going, note that I use apache commons FileUtils instead of messing with buffers and streams for simplicity...
I would take @Andrew White answer (+1 BTW) one step further, and suggest you would use FileNameFilter to list only relevant files:
Using only JDK, If all your files are in one directory:
To exclude files, you can use
listFiles(FileFilter)
If you want a better way of doing this using the new java.nio api, then this is the way, taken from the java docs