“导入java.io.RandomAccessFile;”问题

时间:2019-04-03 15:27:50

标签: java randomaccessfile

这是我的作业... “您是一家五金店的所有者,需要保留一份库存,以告诉您拥有哪些不同的工具,手头有多少种工具以及每种工具的成本。编写一个程序来创建随机访问权限文件“ 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();
        }

    }
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我知道了。有点傻/傻。我的班级名称是“ RandomAccessFile”(以帮助我使班级的事情井井有条),一旦更改,代码就可以正常运行...