Java 请帮助,FileNotFound异常以及其他问题
我完全迷失在阵列中,需要帮助……这是这个项目的最终目标 在名为AccountArray.java的文件中,编写一个客户端程序(您的主方法),从名为customers.txt的文件中读取数据。读取文件中的第一个数字并创建一个 帐户对象的数组,具有该数量的元素。使用“for”循环为从文件中读取的每行信息创建Account对象,并将其存储到数组的元素中 这就是我目前所处的位置。。。我主要关心的是FileNotFound异常错误。。。。我有一个名为customers.txt的文件保存在程序文件夹中,但我是否需要以某种方式对其进行初始化 任何关于我在这个程序中做错的事情的其他输入都会被极大地接受,我才刚刚开始学习这些东西Java 请帮助,FileNotFound异常以及其他问题,java,arrays,loops,for-loop,filenotfoundexception,Java,Arrays,Loops,For Loop,Filenotfoundexception,我完全迷失在阵列中,需要帮助……这是这个项目的最终目标 在名为AccountArray.java的文件中,编写一个客户端程序(您的主方法),从名为customers.txt的文件中读取数据。读取文件中的第一个数字并创建一个 帐户对象的数组,具有该数量的元素。使用“for”循环为从文件中读取的每行信息创建Account对象,并将其存储到数组的元素中 这就是我目前所处的位置。。。我主要关心的是FileNotFound异常错误。。。。我有一个名为customers.txt的文件保存在程序文件夹中,但我
public class AccountArray {
/**
* @param args
*/
public static void main(String[] args) {
List<Account> accountsArray = new ArrayList <Account>();
String name, accountnumber, balance;
Scanner diskScanner = new Scanner(new File("customers.txt"));
Scanner scanner= new Scanner ("customers.txt");
scanner.useDelimiter(" ");
int objects= scanner.nextInt();
Account[] accounts=new Account[objects];
while (objects>0){
name = scanner.nextLine();
accountnumber = scanner.nextLine();
balance = scanner.nextLine();
for(int i = 1; i < objects; i++) {
accountsArray.add(new Account(i, name, accountnumber, balance));
}
objects=objects-1;
System.out.println(name+ " " + accountnumber + " " + balance +"\n"); }// just for debugging
}
这是运行表单eclipse。如果是,则需要将此文件放在项目根文件夹下。您始终可以通过使用找到绝对路径
new File("customers.txt").getAbsoultePath();
将此文件打印到控制台,然后查看此位置是否存在文件。首先,您使用了错误的
扫描仪
对象:
Scanner diskScanner = new Scanner(new File("customers.txt")); // Scans through your file --Use this one
Scanner scanner= new Scanner ("customers.txt"); // Scans through the String "customers.txt" --Not helpful
要修复FileNotFound异常,需要将文件customers.txt移动到通过新文件(“customers.txt”).getAbsolutePath()输出的文件夹中代码>由古怪的汤米建议
您还将进一步遇到一些其他错误,但我将让您自己找出这些错误…相对于编译的java类,“保存在程序文件夹中”的位置是什么?.txt文件需要在同一个目录中。这是一个非常新的目录,不太确定目录是什么。。。。我有一个名为AccountArray的程序,有3个类和file@user2954611你知道什么是相对路径和绝对路径吗this@user2954611阅读尝试给出文件的绝对路径。如果您使用的是windows,那么它应该类似于要访问的c:\some folder\other folder\file
。因此我有位置….c:\Users\shc115\workspace\AccountArray\customers.txt,但是我如何让它读这个呢
Scanner diskScanner = new Scanner(new File("customers.txt")); // Scans through your file --Use this one
Scanner scanner= new Scanner ("customers.txt"); // Scans through the String "customers.txt" --Not helpful