Imagine i have .xml file with the below format.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<config>
<Directory>c:/test</Directory>
<headerLine>1</headerLine>
<pattern>.*\.txt</pattern>
</config>
The <Directory>
tag, Specify the path of the file.
The <headerLine>
tags, specify how many lines of my file have a header and the other is Body.(Not Important)
The <pattern>
tags. specify the Format of the files.
the question is:
i want to read a file. first of all, i have to go to <Directory>
tag, and get the directory of my files, then i have to read all of the files in that directory with the specify pattern, that shows in tags. for example .in C:/test , i have many file, but i should read files, that terminated with ".*.txt" . i use "Unmarshal" algorithm to read from .xml and convert it to java object. my problem, is how can i defind to read all of the files in c:/test directory with that pattern. could you please help me, how can i solve this problem?
FileSplitter fileSplitter = new FileSplitter("C:\\test.txt");
with this code, i can read just test.txt
file, but i want to read directories with specific pattern.
Yes, That,s right. as what is "Vignesh Vino" suggested, this code, solve my problem and answer my question
File folder = new File("/path/to/files");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().endsWith(".txt")) {
String content = FileUtils.readFileToString(file);
/* do somthing with content */
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.