繁体   English   中英

FileNotFound 错误,但我的 txt 文件与 java 文件位于同一位置

[英]FileNotFound error, but i have the txt file in the same location as the java file

所以我有这个程序应该读取一个关于物品清单的txt文件,问题是当我运行它时我得到这个“文件清单.txt没有找到。”,我把它都放在同一个文件夹中,java文件, class 文件和 txt 文件,但仍然没有任何反应。 这是代码:

import java.util.StringTokenizer;
import java.io.*;
import java.text.DecimalFormat;
class InventoryItem {
   private String name;
   private int units;   // number of available units of this item
   private float price;  // price per unit of this item
   private DecimalFormat fmt;
   public InventoryItem (String itemName, int numUnits, float cost) {
      name = itemName;
      units = numUnits;
      price = cost;
      fmt = new DecimalFormat ("0.##");
   }
   public String toString()   {
      return name + ":\t" + units + " at " + price + " = " +
             fmt.format ((units * price));
   }
}
public class Inventory{
   //  Reads data about a store inventory from an input file,
   //  creating an array of InventoryItem objects, then prints them.
   public static void main (String[] args)   {
      final int MAX = 100;
      InventoryItem[] items = new InventoryItem[MAX];
      StringTokenizer tokenizer;
      String line, name, file="inventory.txt";
      int units, count = 0;
      float price;

      try{
         FileReader fr = new FileReader (file);
         BufferedReader inFile = new BufferedReader (fr);
         line = inFile.readLine();
         while (line != null) {
            tokenizer = new StringTokenizer (line);
            name = tokenizer.nextToken();
            try            {
               units = Integer.parseInt (tokenizer.nextToken());
               price = Float.parseFloat (tokenizer.nextToken());
               items[count++] = new InventoryItem (name, units, price);
            }
            catch (NumberFormatException exception)            {
               System.out.println ("Error in input. Line ignored:");
               System.out.println (line);
            }
            line = inFile.readLine();
         }
         inFile.close();

                 for (int scan = 0; scan < count; scan++)
                     System.out.println (items[scan]);
               }
               catch (FileNotFoundException exception)      {
                  System.out.println ("The file " + file + " was not found.");
               }
               catch (IOException exception)      {
                  System.out.println (exception);
               }
            }
         }

现在这是我希望它读取的 txt 文件:

Widget 14 3.35 Spoke 132 0.32 Wrap 58 1.92 Thing 28 4.17 Brace 25 1.75 Clip 409 0.12 Cog 142 2.08

编辑:我的错,我没有指定目录,所以我的文件在我的下载文件中,所以路径是这个 cd\\users\\person\\downloads。 我这里有 Inventory.java、Inventory.class 和inventory.txt

如果您的.txt文件位于以下路径中:MyProject \\ src \\ main \\ java \\ aPackage \\ inventory.txt

你可以这样做:

String file = new File(new File("src/main/java/aPackage").getAbsoluteFile() + File.separator + "inventory.txt").toString();

要么

String file = System.getProperty("user.dir") + File.separator + "src/main/java/aPackage"+ File.separator + "inventory.txt";

这是一个使用您提供的代码的工作示例:

import java.util.StringTokenizer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;

class InventoryItem {
    private String name;
    private int units;   // number of available units of this item
    private float price;  // price per unit of this item
    private DecimalFormat fmt;
    public InventoryItem (String itemName, int numUnits, float cost) {
        name = itemName;
        units = numUnits;
        price = cost;
        fmt = new DecimalFormat ("0.##");
    }
    public String toString()   {
        return name + ":\t" + units + " at " + price + " = " +
                fmt.format ((units * price));
    }
}
public class Inventory{
    //  Reads data about a store inventory from an input file,
    //  creating an array of InventoryItem objects, then prints them.
    public static void main (String[] args)   {
        final int MAX = 100;
        InventoryItem[] items = new InventoryItem[MAX];
        StringTokenizer tokenizer;
        String line, name;
        String file = new File(new File("src/main/java/aPackage").getAbsoluteFile() + File.separator + "inventory.txt").toString();
        int units, count = 0;
        float price;

        try{
            BufferedReader inFile = new BufferedReader(new FileReader(file));
            line = inFile.readLine();
            while (line != null) {
                tokenizer = new StringTokenizer (line);
                name = tokenizer.nextToken();
                try            {
                    units = Integer.parseInt (tokenizer.nextToken());
                    price = Float.parseFloat (tokenizer.nextToken());
                    items[count++] = new InventoryItem (name, units, price);
                }
                catch (NumberFormatException exception)            {
                    System.out.println ("Error in input. Line ignored:");
                    System.out.println (line);
                }
                line = inFile.readLine();
            }
            inFile.close();

            for (int scan = 0; scan < count; scan++)
                System.out.println (items[scan]);
        }
        catch (FileNotFoundException exception)      {
            System.out.println ("The file " + file + " was not found.");
        }
        catch (IOException exception)      {
            System.out.println (exception);
        }
    }
}

尝试创建一个新文件夹,我们暂时将其称为“文本”。 然后,您的代码将如下所示:

//your code here
String line, name, file="texts\\inventory.txt";
//more code

希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM