“import java.io.RandomAccessFile;”[重复]的问题

问题描述 投票:-1回答:2

这个问题在这里已有答案:

这是我的家庭作业......“你是一家五金店的老板,需要保留一份库存,告诉你你有哪些不同的工具,每个工具有多少,以及每个工具的成本。创建随机访问文件“hardware.dat”的程序。您的程序应该允许您输入每个工具的数据并将数据写入它创建的文件。使用以下信息启动您的文件:...“然后它给出了一个信息列表。

这些是我的作业......(下面的类)第二个(硬件类)我没有问题,但我只是想确保它是正确的。但我在第一个链接中遇到RandomAccessFile问题。如果我做得对,我不知道。每当我将鼠标悬停在“import java.io.RandomAccessFile;”上时,它会一直说“RandomAccessFile已在此编译中定义”那是什么意思?? (我不能粘贴链接而不包含代码)我做错了什么?

RandomAccessFile类:

import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Scanner;

public class RandomAccessFile  {
    public static void main(String[] args) 
    {
        RandomAccessFile file;
        Scanner sc = new Scanner(System.in);

        int toolId;
        String toolName;
        int quantity;
        double cost;

        System.out.println("Please Input Tool ID: ");
        toolId = sc.nextInt();

        System.out.println("Please Input Tool Name: ");
        sc.nextLine();
        toolName = sc.nextLine();

        System.out.println("Please Input the Quantity: ");
        quantity = sc.nextInt();

        System.out.println("Please Input the Price: ");
        cost = sc.nextDouble();

            try
            {
                file = new RandomAccessFile(new File("hardware.dat"), "rw");
                long FileSize = file.length();
                file.seek(FileSize);

                file.writeInt(toolId);

                file.writeUTF(toolName);
                for(int i = 0; i < 24- toolName.length(); i++)
                {
                    file.writeByte(24);
                }

                file.writeInt(quantity);

                file.writeDouble(cost);

                System.out.println("Item added to inventory");
                file.close();
            }

            catch (FileNotFoundException e)
            {
                System.err.println("File not found. Check Project Folder and if you have read and write permissions to it");
            }

            catch (IOException e)
            {
                e.getStackTrace();
            }
    }
     }

硬件类:

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;

public class Hardware 
{

    private static final int RECORD = 24;

    public static void main(String[] args) 
    {
        RandomAccessFile file;
        int toolId = 0;
        String toolName = null;
        int quantity = 0;
        double cost = 0.00;

        try 
        {
            file = new RandomAccessFile(new File("hardware.dat"), "rw");

            long FileSize = file.length();
            file.seek(0);
            long NUMRecords = FileSize / RECORD;
            for (int j = 0; j < NUMRecords; j++) 
            {

                toolId = file.readInt();

                toolName = file.readUTF();
                for (int i = 0; i < 24- toolName.length(); i++) 
                {
                    file.readByte();
                }

                quantity = file.readInt();

                cost = file.readDouble();

                System.out.println("Tool ID: "  + toolId + "  Tool Name: " + toolName + "  Quantity: " + quantity + "  Cost:  $" + cost);

            }

            file.close();
        }

        catch (FileNotFoundException e)
        {
            System.err.println("File not found. Check Project Folder and if you have read and write permissions to it");
        } 

        catch (IOException e) 
        {
            e.getStackTrace();
        }

    }
}
java randomaccessfile
2个回答
0
投票

我无法访问您的链接,但显然您已将您的类命名为RandomAccessFile?如果是这样,请尝试更改其名称,因为同时导入另一个名为java.io.RandomAccessFile的类,因此出错。


0
投票

我想到了。这是愚蠢/愚蠢的事情。我的班级名称是“RandomAccessFile”(为了帮助我保持课堂上的组织),一旦我改变了它,那么代码运行良好......

© www.soinside.com 2019 - 2024. All rights reserved.