[英]Read set of numbers from file in Java as set something like 1 to 3, 4 to 16
In a loop, I want to read numbers from a csv file having n numbers of rows.在一个循环中,我想从具有 n 行数的 csv 文件中读取数字。 In each iteration of the loop I want to start from the next set of numbers where the previous set was ended.在循环的每次迭代中,我想从上一组结束的下一组数字开始。 For example, my file has numbers 1, 2, 3, 4........N例如,我的文件有数字 1、2、3、4........N
Lets say, I want to read 3 numbers in each iteration or any value that user defines to read in each iteration.可以说,我想在每次迭代中读取 3 个数字或用户定义在每次迭代中读取的任何值。 Iteration 1: Number 1, 2, 3 Iteration 2: Number 4, 5, 6迭代 1:编号 1、2、3 迭代 2:编号 4、5、6
Here is the sample code that I tried这是我尝试过的示例代码
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadTest {
public static void main(String[] args) throws IOException {
for (int z = 1; z <= 5; z++) {
System.out.println("File Read Test Iteration "+z);
BufferedReader bufferedReader = new BufferedReader(new FileReader("testNumbers.csv"));
String line = bufferedReader.readLine();
String testString = "Start of the string";
int rowCount = 3; //5
int itr = 1;
int l = 1;
while (line != null && itr <= (l * rowCount)) {
itr++;
testString += "{" + line.split(",")[0] + "}";
line = bufferedReader.readLine();
if (itr < (l * rowCount + 1)) testString += ",";
}
l++;
testString += "end of the string";
System.out.println(testString);
bufferedReader.close();
}
}
}
Currently I am getting out as:目前我正在退出:
File Read Test Iteration 1
Start of the string{1},{2},{3}end of the string
File Read Test Iteration 2
Start of the string{1},{2},{3}end of the string
File Read Test Iteration 3
Start of the string{1},{2},{3}end of the string
File Read Test Iteration 4
Start of the string{1},{2},{3}end of the string
File Read Test Iteration 5
Start of the string{1},{2},{3}end of the string
I want the output as something like:我希望 output 类似于:
File Read Test Iteration 1
Start of the string{1},{2},{3}end of the string
File Read Test Iteration 2
Start of the string{4},{5},{6}end of the string
File Read Test Iteration 3
Start of the string{7},{8},{9}end of the string
File Read Test Iteration 4
Start of the string{10},{11},{12}end of the string
File Read Test Iteration 5
Start of the string{13},{14},{15}end of the string
My file looks like enter image description here我的文件看起来像在此处输入图像描述
Any help is greatly appreciated.任何帮助是极大的赞赏。
set testString
to ""
(the empty string) after printing it ( testString = "";
).打印后将testString
设置为""
(空字符串)( testString = "";
)。
System.out.println("test string is " + testString);
testString = "";
l++;
full code:完整代码:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class FileReadTest {
public static void main(String[] args) throws IOException {
System.out.println("File Read Test");
BufferedReader bufferedReader = new BufferedReader(new FileReader("testNumbers.csv"));
String line = bufferedReader.readLine();
String testString = "";
int rowCount = 3; //Based on this variable, the number of items to picked in each iteration
int itr = 1;
int l = 1;
for (int z = 1; z <= 5; z++) {
while (line != null && itr <= (l * rowCount)) {
itr++;
testString += "{" + line.split(",")[0] + "}";
line = bufferedReader.readLine();
}
System.out.println("test string is " + testString);
testString = "";
l++;
}
bufferedReader.close();
}
}
Output: Output:
File Read Test
test string is {1}{2}{3}
test string is {4}{5}{6}
test string is {7}{8}{9}
test string is {10}{11}{12}
test string is {13}{14}{15}
you almost do it, you just need restart the string你几乎做到了,你只需要重新启动字符串
System.out.println("File Read Test");
BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/cesarjesusgutierrez/Downloads/test.csv"));
String line = bufferedReader.readLine();
String testString = "";
int rowCount = 3; //Based on this variable, the number of items to picked in each iteration
int itr = 1;
int l = 1;
for (int z = 1; z <= 5; z++) {
testString = ""; // check this :)
while (line != null && itr <= (l * rowCount)) {
itr++;
testString += "{" + line.split(",")[0] + "}";
line = bufferedReader.readLine();
}
System.out.println("test string is " + testString);
l++;
}
bufferedReader.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.