从Java中的工作目录读取txt文件(不工作)

从Java中的工作目录读取txt文件(不工作),java,arrays,io,int,java.util.scanner,Java,Arrays,Io,Int,Java.util.scanner,我读过几篇关于如何将txt文件读入int[]的文章,通过多种可能的方式,我都没有成功 import java.io.*; import java.util.Scanner; public class SequenceSquare { private int[] arr; public SequenceSquare() { int count = 0; File fileName = new File("Sequence.txt"); // fileName for open

我读过几篇关于如何将txt文件读入int[]的文章,通过多种可能的方式,我都没有成功

import java.io.*;
import java.util.Scanner;

public class SequenceSquare
{
private int[] arr;

public SequenceSquare() 
{
    int count = 0;
    File fileName = new File("Sequence.txt"); // fileName for opening
    try
    {
        Scanner input = new Scanner(fileName);
        while (input.hasNextLine()) //while there is a line to read
        {
            if(input.hasNextInt()) //if this line has an int
            {
                count++; // increment count 
                input.nextInt(); //move to next integer
            }
        }

        arr = new int[count]; // create arr with sufficient size
        input.close(); // close scanning file

        Scanner newInput = new Scanner(fileName);

        for(int i = 0; i < arr.length; i++)
        {
            while (newInput.hasNextLine()) // same as above
            {
                if(newInput.hasNextInt()) // same as above
                {
                   arr[i] = newInput.nextInt(); //store int scanned into arr 
                }
            }
        }
        newInput.close();
    }
    catch(FileNotFoundException f)
    {
        f.printStackTrace();
    }
}
"Sequence.txt"
1
2
5
9
29
30
7
9
111
59
106
130
-2
因此,基本上在调用默认构造函数时,假定打开Sequence.txt并读取格式化为int数组的整数。在txt文件中,数字被格式化为每行的整数,如4\n 5\n等。
但是,当我遍历数组arr时,它似乎没有任何内容。我已经实现了许多测试函数来测试是否已填充未列出,但arr不返回任何内容。请帮忙。请告诉我发生了什么事。我宁愿知道对正在发生的事情的解释,也不愿知道答案,但两者都可以。另外,我知道可以使用ArrayList和List来执行相同的功能,但我希望它是一个数组。如果您想查看我可以发布的整个类,但其他代码在工作时是不必要的

这就是我们使用数组的方法。如果是列表,它会更简单

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileReading {
    public static final String FILE_PATH = "D:\\testing.txt";
    public static void main(String[] args) {        

        try {
            int[] newArr = readFile(FILE_PATH);
            //testing the new array
            for(int i=0;i<newArr.length;i++){
                System.out.println(newArr[i]);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static int[] readFile(String pathToFile) throws NumberFormatException, IOException{
        String sCurrentLine;
        int[] arr = new int[countLines(pathToFile)];
        BufferedReader br = new BufferedReader(new FileReader(pathToFile));
        int i=0;
        while ((sCurrentLine = br.readLine()) != null) {                
            arr[i] = Integer.parseInt(sCurrentLine);
            i++;
        }
        br.close();
        return arr;
    }

    public static int countLines(String pathToFile) throws NumberFormatException, IOException{
        BufferedReader br = new BufferedReader(new FileReader(pathToFile));
        int count =0;
        while ((br.readLine()) != null) {
            count++;
        }
        br.close();
        return count;
    }
}

逐行显示Sequence.txt文件。正如你发布的代码一样,你可以使用文本文件内容你可以使用列表而不是数组?我可以使用列表或arraylist,但我更喜欢使用数组来学习。你的Sequence.txt应该有一些问题。因为这对我来说非常有效。确保在其他路径中没有相同的命名文件。在arr[I]=newInput.nextInt中分配数组元素之后,我刚刚打印了该数组元素。我没有抛出异常或使用BufferedReader的经验,但这看起来像是一个解决方案。给我一点时间,让我把它写进我的代码中。代码非常有用。非常感谢。