Очень, очень новый для java здесь. В настоящее время у меня есть код для извлечения данных из одного текстового файла и анализа данных в формате, который можно использовать для меня (см. Ниже). К сожалению, для этого мне нужно изменить имя файла для каждого прогона. В идеале я хотел бы автоматизировать его, перейдя через массив строк имен файлов для сбора данных.Java InputData цикл чтения/записи с массивом имен файлов
Любая идея, как я могу передать массив имен файлов и закодировать их через каждое имя?
package weatherfiledata;
import java.io.*;
/**
*
*
*/
public class WeatherFileData {
int month[] = new int[8760];
int day[] = new int[8760];
int hour[] = new int[8760];
int db[] = new int[8760];
int wb[] = new int[8760];
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int count;
//here i would declare the array of fileNames and start the loop
//String fileNames[] = {"fileName1","fileName2"...};
//for(count = 0; count < fileNames.length; count++){
// TODO code application logic here
//create an object named "DataInput" of type "WeatherFileData"
WeatherFileData DataInput = new WeatherFileData();
DataInput.InputData(DataInput);
//display data
int i;
for(i=0; i<DataInput.month.length; i++) {
System.out.print(DataInput.wb[i] +",");
}
//} end of loop
}
void InputData(WeatherFileData DataInput) {
BufferedReader br = null;
try {
String sCurrentLine;
//this is where I currently have to change the fileName each time;
//I would like to be able to use the fileName array loop to automate
br = new BufferedReader(new FileReader("fileName1.txt"));
int i=0;
while ((sCurrentLine = br.readLine()) != null) {
String arr[] = sCurrentLine.split("\t");
DataInput.month[i] = Integer.parseInt(arr[0]);
DataInput.day[i] = Integer.parseInt(arr[1]);
DataInput.hour[i] = Integer.parseInt(arr[2]);
DataInput.wb[i] = Integer.parseInt(arr[3]);
DataInput.db[i] = Integer.parseInt(arr[4]);
i++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Если я правильно понимаю проблему, существует метод '' 'listFiles()' '' в '' 'File''' классе. Вы можете создать файл с каталогом своих файлов, а затем перечислить все файлы внутри. – Mati
Метод listFiles() отлично работает и является очень быстрым и простым способом интеграции этой функции. благодаря! – coolhand