Java文件作为数组读入
我似乎无法将这些数组读入列中。正在寻找一个解决方案来帮助我最终将这些数字组成一个数组Java文件作为数组读入,java,arrays,Java,Arrays,我似乎无法将这些数组读入列中。正在寻找一个解决方案来帮助我最终将这些数字组成一个数组 public class TextFileInputAndOutput { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader("USStateCapitalsSelected.txt")); int
public class TextFileInputAndOutput
{
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("USStateCapitalsSelected.txt"));
int lineCounter = 1;
String line;
while ((line = reader.readLine()) != null) {
// parse line using any method.
// example 1:
Scanner intScanner = new Scanner(line);
while (intScanner.hasNextLine()) {
String nextInt = intScanner.nextLine();
System.out.println(nextInt + "Herro");
if (intScanner.hasNextDouble() == true) {
Scanner scanner = new Scanner(line);
while (scanner.hasNextDouble()) {
String nextString = scanner.next();
System.out.println(nextString);
}
}
}
}
}
}
您可以使用com.google.common.io.Files: Sample.txt: 拉巴基泽亚的名字
nameB labB quizeB
代码:
publicstaticvoidmain(字符串[]args)引发异常{
File myFile=新文件(“Sample.txt”);
ArrayList name=新的ArrayList();
ArrayList labs=新的ArrayList();
ArrayList quizes=新的ArrayList();
for(字符串行:Files.readLines(myFile,Charset.defaultCharset())){
字符串[]cols=line.split(“”);
name.add(cols[0]);
添加(cols[1]);
添加(cols[2]);
}
System.out.println(名称);
系统输出打印(实验室);
System.out.println(quizes);
}
Tryb这个
try{
File file = new File("filename");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] temp = line.split(" ");
//add values to arraylist
names.add(temp[0]);
labs.add(temp[1]);
quizes.add(temp[2]);
}
}catch (Exception ex) {
ex.printStackTrace();
}
你应该先看看扫描器,这正是你需要的,然后尝试一些简单的事情,比如读取文件的第一行,看看会发生什么。然后,向我们展示实际读取文件的代码——可能还有一个内容示例。仅供参考:您可以将代码粘贴到此处(格式化),而不是发布图像-这样更易于阅读。我在上面的程序中使用扫描仪。我正在读文件的第一行。问题是根据类别将列垂直拆分为数组。能否显示USStateCapitalsSelected.txt的内容?
try{
File file = new File("filename");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] temp = line.split(" ");
//add values to arraylist
names.add(temp[0]);
labs.add(temp[1]);
quizes.add(temp[2]);
}
}catch (Exception ex) {
ex.printStackTrace();
}