Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.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 我想写一个从txtfile读取的方法,这个方法可以工作,但是保持循环,不能停止_Java - Fatal编程技术网

Java 我想写一个从txtfile读取的方法,这个方法可以工作,但是保持循环,不能停止

Java 我想写一个从txtfile读取的方法,这个方法可以工作,但是保持循环,不能停止,java,Java,我是java新手,这是一种从文本文件读取并保存为字符串的方法,因为我希望它显示在javafx textArea()中,它可以工作,但它会一次又一次地读取所有行,循环无法停止。有人能帮忙吗 public static String getAllResults() throws IOException { BufferedReader br = new BufferedReader(new FileReader(

我是java新手,这是一种从文本文件读取并保存为字符串的方法,因为我希望它显示在javafx textArea()中,它可以工作,但它会一次又一次地读取所有行,循环无法停止。有人能帮忙吗

    public static String getAllResults() throws IOException {
         BufferedReader br = new BufferedReader(new 
                              FileReader("gameResults.txt"));
        String line = "";
        // Read a single line from the file until there are no more lines to 
        //read
        String currCol=null;
        //first loop to read from textfile by line
        while ((line = br.readLine()) != null) {
            StringTokenizer st = new StringTokenizer(line, ","); 
            currCol = new String(); // Each currCol has 5 fields
            //System.out.println(line);


            while (st.hasMoreTokens()) { 
                //second loop using hasmoretokens() operator to check and 
                //keep read if there is more token
                currCol+=line; //st.nextToken().toString()
                //System.out.println(currCol);
            }

            //resultList.add(currCol);
            //System.out.println(resultList);
        }
        br.close();
        return currCol;
    }
“StringTokenizer是一个出于兼容性原因保留的旧类,尽管在新代码中不鼓励使用它。建议寻求此功能的任何人使用String的拆分方法或java.util.regex包。”

你可以这样写你的内部循环

    String[] result = line.split(",");
 for (int x=0; x<result.length; x++){ //write your stuff here}
String[]result=line.split(“,”);

对于(int x=0;x如果您只想将文件的全部内容作为单个字符串,可以执行以下操作:

public static String getAllResults() throws IOException {

    Path path = Paths.get("gameResults.txt");
    return Files.lines(path).collect(Collectors.joining("\n"));

}

如果你不调用圣NeXToToCK()<代码>,StHasMeReToNoSee()/代码>将永远是正确的。考虑只使用<代码>行。拆分(“,”)< /代码>,而不是内循环。我尝试了一些类似于CurCal= ST NeXtotok()的方法。;不起作用@无论如何,我将继续尝试使用此方法返回什么?在文本文件中有许多行,每行包含3个文件,我尝试将整个文件读入字符串,然后返回到文本区域如果您希望将整个内容作为单个
字符串
,您根本不需要拆分标记,是吗?或者你这么做还有其他原因吗?