从 Java 中的工作目录读取 txt 文件(不工作)
Reading txt file from working directory in Java (not working)
我已经阅读了一些 post 如何将 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 array.In txt 文件的整数,数字被格式化为整数每行如 4\n 5\n 等。
但是,当我遍历数组 "arr" 时,它似乎没有内容。我已经实现了一些测试函数来测试是否已经填充(未列出)但是 arr 没有 return 任何东西。请帮忙。请告诉我发生了什么事。我宁愿知道对正在发生的事情的解释而不是答案,但两者都可以。我也知道你可以使用数组列表和列表来执行相同的功能,但我希望它是一个数组。如果你想看到整个 class 我可以 post 但其他代码是不必要的,因为它有效
这就是我们如何使用数组来做到这一点。如果它是列表,它会更简单。
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;
}
}
我已经阅读了一些 post 如何将 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 array.In txt 文件的整数,数字被格式化为整数每行如 4\n 5\n 等。 但是,当我遍历数组 "arr" 时,它似乎没有内容。我已经实现了一些测试函数来测试是否已经填充(未列出)但是 arr 没有 return 任何东西。请帮忙。请告诉我发生了什么事。我宁愿知道对正在发生的事情的解释而不是答案,但两者都可以。我也知道你可以使用数组列表和列表来执行相同的功能,但我希望它是一个数组。如果你想看到整个 class 我可以 post 但其他代码是不必要的,因为它有效
这就是我们如何使用数组来做到这一点。如果它是列表,它会更简单。
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;
}
}