从java中的文本文件中获取多个输入
我写了一个程序,需要为不同的输入(整数)值生成结果。我必须从几个文本文件中读取一些整数,并将它们存储在这些变量中从java中的文本文件中获取多个输入,java,input,file-io,Java,Input,File Io,我写了一个程序,需要为不同的输入(整数)值生成结果。我必须从几个文本文件中读取一些整数,并将它们存储在这些变量中 选择 numofNodes numofoints 在每个文件中,可能有多个数字应分配给上述变量之一,例如: 第一个文本文件包含第一个变量的五个值,其他两个变量在单独的行上有一个值,如下所示: 1 2 3 4 5 60 50 1 40 50 60 70 80 50 第二个文本文件包含第二个变量的五个值,其他两个变量在单独的行中有一个值,如下所示: 1 2 3 4 5 60 5
- 选择
- numofNodes
- numofoints
1 2 3 4 5
60
50
1
40 50 60 70 80
50
第二个文本文件包含第二个变量的五个值,其他两个变量在单独的行中有一个值,如下所示:
1 2 3 4 5
60
50
1
40 50 60 70 80
50
等等
我不知道如何从文本文件中读取它们。
任何帮助都将不胜感激
这是我的主要课程:
public static void main(String[] args)throws InterruptedException {
// how to read numbers from different text files here
// and store them in these variables to call func method?
// int choice = ?
// int numofNode = ?
// int numofPoint = ?
path ob=new path(choice,numofNode,numofPoint);
ob.func();
}
将文件路径放入字符串数组中。然后,您可以通过创建
java.util.Scanner
类的实例来读取文件,该类有多种读取文件内容的方法
您所需要的只是使用for和for-each循环遍历文件并读取它们
这里有一些代码,我希望能对你有所帮助
/**
* converts array of string numbers to array of integer numbers.
*
* @param numbers is array of strings
* @return an integer array which is parsed from <b>numbers</b>
*
*/
static int[] parseInt(String[] numbers) {
return Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}
public static void main(String[] args) {
// put input files path here
String[] name_and_path_of_files = {
"C:\\Users\\YOUR_USER\\Desktop\\input_1.txt",
"C:\\Users\\YOUR_USER\\Desktop\\input_2.txt"
};
// file reader
Scanner inputFileReader = null;
try {
// for all files
for (String fileInfo : name_and_path_of_files) {
// create reader object with file info
inputFileReader = new Scanner(new File(fileInfo));
int line_index = 0;
int choices[] = null;
int numofNodes[] = null;
int numofPoints[] = null;
// trying to read file content
while (inputFileReader.hasNext()) {
String separated_numbers[] = inputFileReader.nextLine().split(" ");
switch (line_index) {
case 0:
choices = parseInt(separated_numbers);
break;
case 1:
numofNodes = parseInt(separated_numbers);
break;
case 2:
numofPoints = parseInt(separated_numbers);
break;
}
line_index++;
}
for (int choice : choices) {
for (int numofPoint : numofPoints) {
for (int numofNode : numofNodes) {
path ob = new path(choice, numofNode, numofPoint);
ob.func();
}
}
}
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
} finally {
if (inputFileReader != null) {
inputFileReader.close();
}
}
}
/**
*将字符串数数组转换为整数数组。
*
*@param numbers是字符串数组
*@返回从数字解析的整数数组
*
*/
静态int[]parseInt(字符串[]数字){
返回Arrays.stream(numbers).mapToInt(Integer::parseInt).toArray();
}
公共静态void main(字符串[]args){
//将输入文件路径放在这里
字符串[]文件的名称和路径={
“C:\\Users\\YOUR\u USER\\Desktop\\input\u 1.txt”,
“C:\\Users\\YOUR\u USER\\Desktop\\input\u 2.txt”
};
//文件读取器
扫描仪inputFileReader=null;
试一试{
//对于所有文件
for(字符串文件信息:文件的名称和路径){
//使用文件信息创建读卡器对象
inputFileReader=新扫描仪(新文件(fileInfo));
int line_index=0;
int-choices[]=null;
int numofNodes[]=null;
int numofPoints[]=null;
//正在尝试读取文件内容
while(inputFileReader.hasNext()){
字符串分隔的_number[]=inputFileReader.nextLine().split(“”);
开关(线路索引){
案例0:
选项=parseInt(分隔的数字);
打破
案例1:
numofNodes=parseInt(分隔的_数);
打破
案例2:
numofPoints=parseInt(分隔的数字);
打破
}
line_index++;
}
for(int-choice:choices){
for(int numofPoint:numofPoints){
for(int numofNode:numofNodes){
路径ob=新路径(选项、numofNode、numofPoint);
ob.func();
}
}
}
}
}捕获(例外e){
System.out.println(“错误:+e.getMessage());
}最后{
if(inputFileReader!=null){
inputFileReader.close();
}
}
}
阅读并@TheLostMind,编辑question@Tina检查我的答案。