Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中,从文件中读取一组数字,例如1到3,4到16_Java_Loops - Fatal编程技术网

在Java中,从文件中读取一组数字,例如1到3,4到16

在Java中,从文件中读取一组数字,例如1到3,4到16,java,loops,Java,Loops,在循环中,我想从一个有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

在循环中,我想从一个有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{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=“”;

完整代码:

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();
    }
}

你几乎做到了,你只需要重新启动字符串

    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();
System.out.println(“文件读取测试”);
BufferedReader BufferedReader=new BufferedReader(new FileReader(“/Users/cesarjesusgutierrez/Downloads/test.csv”);
String line=bufferedReader.readLine();
字符串testString=“”;
int rowCount=3//基于此变量,在每次迭代中拾取的项目数
int-itr=1;
int l=1;

对于(int z=1;z我认为如果您使用调试器运行代码,您可以自己发现问题。您使用的IDE应该有一个。您应该学会使用它。您读过这个吗?查看您正在读取的文件会有帮助。谢谢。这很有帮助。如果我想要类似于此测试开始的输出,我还有一个问题字符串是[{1},{2},{3}]测试字符串的结束测试字符串的开始是[{4}{5}{6}]测试字符串的结束测试字符串的开始是[{7}{8}{9}]测试字符串的结束测试字符串的开始是[{10}{11}{12}]测试字符串的结尾谢谢。这很有帮助。我还有一个问题,如果我想要像这样的输出,测试字符串的开头是[{1},{2},{3}]测试字符串的结尾是[{4}{5}{6}]测试字符串的结尾测试字符串的开头是[{7}{8}{9}]测试字符串的结尾测试字符串的开头是[{10}{11}{12}]测试字符串的结尾测试字符串是{13}{14}{15}这将是一个新问题。嘿,塞萨尔,我已经更新了我的问题,你有什么建议吗
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();
    }
}
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();