如何使用Java中的扫描程序读取文本文件? [英] how to read a text file using scanner in Java?

查看:143
本文介绍了如何使用Java中的扫描程序读取文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我读取文本文件的代码。当我运行此代码时,输​​出会一直显示找不到文件。,这是 FileNotFoundException 的消息。我不确定这段代码中的问题是什么。

This is my code to read a text file. When I run this code, the output keeps saying "File not found.", which is the message of FileNotFoundException. I'm not sure what is the problem in this code.

显然这是java的一部分。对于整个java文件,它要求用户输入内容并使用输入作为名称创建文本文件。
之后用户应该再次输入之前创建的文本文件的名称(假设用户输入正确),然后程序应该读取文本文件。
我已正确完成程序的其他部分,但问题是当我再次输入名称时,它只是找不到文本文件,尽管它们位于同一文件夹中。

Apparently this is part of the java. For the whole java file, it requires the user to input something and will create a text file using the input as a name. After that the user should enter the name of the text file created before again (assume the user enters correctly) and then the program should read the text file. I have done other parts of my program correctly, but the problem is when i enter the name again, it just can not find the text file, eventhough they are in the same folder.

public static ArrayList<DogShop> readFile()
    {

        try 
        {   // The name of the file which we will read from
            String filename = "a.txt";

            // Prepare to read from the file, using a Scanner object
            File file = new File(filename);
            Scanner in = new Scanner(file);

            ArrayList<DogShop> shops = new ArrayList<DogShop>();

            // Read each line until end of file is reached
            while (in.hasNextLine())
            {
                // Read an entire line, which contains all the details for 1 account
                String line = in.nextLine();

                // Make a Scanner object to break up this line into parts
                Scanner lineBreaker = new Scanner(line);



                // 1st part is the account number
                try 
                {   int shopNumber = lineBreaker.nextInt();

                    // 2nd part is the full name of the owner of the account
                    String owner = lineBreaker.next();

                    // 3rd part is the amount of money, but this includes the dollar sign
                    String equityWithDollarSign = lineBreaker.next();

                    int total = lineBreaker.nextInt();

                    // Get rid of the dollar sign;
                    // we use the subtring method from the String class (see the Java API),
                    // which returns a new string with the first 'n' characters chopped off,
                    // where 'n' is the parameter that you give it
                    String equityWithoutDollarSign = equityWithDollarSign.substring(1);

                    // Convert this balance into a double, we need this because the deposit method
                    // in the Account class needs a double, not a String
                    double equity = Double.parseDouble(equityWithoutDollarSign);

                    // Create an Account belonging to the owner we found in the file
                    DogShop s = new DogShop(owner);



                    // Put money into the account according to the amount of money we found in the file
                    s.getMoney(equity);

                        s.getDogs(total);

                    // Put the Account into the ArrayList
                    shops.add(s);
                }

                catch (InputMismatchException e)
                {
                    System.out.println("File not found1.");

                }

                catch (NoSuchElementException e)
                {
                    System.out.println("File not found2");

                }

            }



        }


        catch (FileNotFoundException e)
        {
            System.out.println("File not found");

        }   // Make an ArrayList to store all the accounts we will make








        // Return the ArrayList containing all the accounts we made
        return shops;
    }


推荐答案

如果你在某些地方工作像Eclipse或NetBeans这样的IDE,您应该在项目的根目录中包含 a.txt 文件。 (而不是在 .class 文件构建的文件夹中或其他任何地方)

If you are working in some IDE like Eclipse or NetBeans, you should have that a.txt file in the root directory of your project. (and not in the folder where your .class files are built or anywhere else)

如果没有,你应该指定该文件的绝对路径。

If not, you should specify the absolute path to that file.

编辑:

你可以将 .txt 文件与 .class 放在同一个地方(通常也是 .java file,因为你在同一个文件夹中编译)编译文件,如果你用 javac 手工编译它。这是因为它使用相对路径,路径告诉JVM可执行文件所在的路径。


You would put the .txt file in the same place with the .class(usually also the .java file because you compile in the same folder) compiled files if you compile it by hand with javac. This is because it uses the relative path and the path tells the JVM the path where the executable file is located.

如果使用某个IDE,它将生成编译后的文件为你使用Makefile或类似的东西,并将其视为默认的文件结构,所以他知道相对路径从项目的根文件夹开始。

If you use some IDE, it will generate the compiled files for you using a Makefile or something similar for Windows and will consider it's default file structure, so he knows that the relative path begins from the root folder of the project.

这篇关于如何使用Java中的扫描程序读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆