这里非常非常新的java。目前我有代码从单个文本文件中提取数据并以可用于我的格式解析数据(见下文)。不幸的是,这要求我必须为每次运行更改文件名。理想情况下,我希望通过循环遍历文件名字符串数组来自动化它来收集数据。
知道如何传递文件名数组并将其循环遍历每个名称吗?
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();
}
}
}
}
答案 0 :(得分:1)
首先应该为文件名引入一个String变量:
br = new BufferedReader(new FileReader(filename));
并将此变量作为参数传递给您的方法:
void inputData(WeatherFileData dataInput, String filename) {
然后,您可以在数组中维护这些文件的列表:
String[] filenames = new String[5];
filenames[0] = "fileName1.txt";
...
用以下方法迭代它们:
for(String filename : filenames){
WeatherFileData dataInput = new WeatherFileData();
dataInput.inputData(dataInput, filename);
... // Do what you want with DataInput
}
作为最后一条评论,请注意您不需要这样做:
dataInput.inputData(dataInput, filename);
这会更好:
dataInput.inputData(filename);
但在函数inputData()
内写下这个:
this.month[i] = Integer.parseInt(arr[0]);
而不是:
dataInput.month[i] = Integer.parseInt(arr[0]);
(我还从你的对象和方法名中删除了大写的第一个字母,这是惯例)
答案 1 :(得分:0)
我只是稍微调整了你的代码。希望它有效
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) {
//create an object named "DataInput" of type "WeatherFileData"
WeatherFileData dataInput = new WeatherFileData();
String fileNames[] = {"fileName1","fileName2"...};
for(int count = 0; count < fileNames.length; count++){
dataInput.inputData(fileNames[count],count);
}
//display data
int i;
for(i=0; i<dataInput.month.length; i++) {
System.out.print(dataInput.wb[i] +",");
}
//} end of loop
}
void inputData(String fileName, int currentIndex) {
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(fileName));
int i=0;
while ((sCurrentLine = br.readLine()) != null) {
String arr[] = sCurrentLine.split("\t");
this.month[currentIndex] = Integer.parseInt(arr[0]);
this.day[currentIndex] = Integer.parseInt(arr[1]);
this.hour[currentIndex] = Integer.parseInt(arr[2]);
this.wb[currentIndex] = Integer.parseInt(arr[3]);
this.db[currentIndex] = Integer.parseInt(arr[4]);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
答案 2 :(得分:0)
我建议你更灵活(java 7):
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) {
// TODO code application logic here
//create an object named "DataInput" of type "WeatherFileData"
WeatherFileData DataInput = new WeatherFileData();
DataInput.InputData(DataInput, args[0]/*Path to files*/);
//display data
int i;
for (i = 0; i < DataInput.month.length; i++) {
System.out.print(DataInput.wb[i] + ",");
}
}
void InputData(WeatherFileData DataInput, String path) {
final File[] files = new File(path).listFiles();
if (files != null) {
for (File file : files) {
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String sCurrentLine;
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();
}
}
}
}
}
当然,请阅读有关java命名约定的内容。 http://www.oracle.com/technetwork/java/codeconventions-135099.html