[英]Read set of numbers from file in Java as set something like 1 to 3, 4 to 16
在一個循環中,我想從具有 n 行數的 csv 文件中讀取數字。 在循環的每次迭代中,我想從上一組結束的下一組數字開始。 例如,我的文件有數字 1、2、3、4........N
可以說,我想在每次迭代中讀取 3 個數字或用戶定義在每次迭代中讀取的任何值。 迭代 1:編號 1、2、3 迭代 2:編號 4、5、6
這是我嘗試過的示例代碼
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();
}
}
}
目前我正在退出:
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
我希望 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
我的文件看起來像在此處輸入圖像描述
任何幫助是極大的贊賞。
打印后將testString
設置為""
(空字符串)( testString = "";
)。
System.out.println("test string is " + testString);
testString = "";
l++;
完整代碼:
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:
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}
你幾乎做到了,你只需要重新啟動字符串
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.