您好我想知道从txt文件中扫描int数的最佳和最简单的方法 一个接一个的数字 我能找到的数字是0到9 不是 例如:
24351235312531135
我希望每次都能扫描这些输入
2
4
3
5
1
2
编辑:
My input in txt file is something like this
13241235135135135
15635613513513531
13513513513513513
13251351351351353
13245135135135315
13513513513513531
具有已知位数的6行 .... 我找到了这段代码但是没有用
import java.util.Scanner;
public class ScannerReadFile {
public static void main(String[] args) {
// Location of file to read
File file = new File("data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
我无法找到面对
的确切查询答案 0 :(得分:2)
试试这个:
public static void main(String[] args) {
File file = new File("data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
for (int i = 0; i < line.length(); i++) {
System.out.println(line.charAt(i));
}
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
答案 1 :(得分:1)
你需要一个for循环:
for(int i=0;i<line.length();i++)
System.out.println(Integer.parseInt(line.charAt(i)));
答案 2 :(得分:0)
检查此示例,了解如何在Java中打开和读取文件: http://alvinalexander.com/blog/post/java/how-open-read-file-java-string-array-list
答案 3 :(得分:0)
您可以迭代每行的字符:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
for (char c: line.toCharArray()) {
System.out.println(c);
}
}