无法打印bin文件中的数据

时间:2017-04-10 16:15:07

标签: java

我试过这个代码,它主要起作用。 但是,当我希望程序打印输入的数字时(在最后部分中),程序不想这样做。 我做错了什么?我怎么解决它?

public static void main(String[] args)
{
    FileOutputStream outChannel = null;
    DataOutputStream numberChannel = null; 
    Scanner kb = new Scanner(System.in);
    int sum = 0;
    int count = 0;
    int highest = 0;
    int number = 0;
    String name = "";
    try
    {
        System.out.println("Enter the name of the binary file: ");
        name = kb.nextLine();
        outChannel = new FileOutputStream(name);
        numberChannel = new DataOutputStream(outChannel);
        System.out.println("Enter negative value to stop.");
        System.out.println("Enter scores: ");
        while(true)
        {
            number = kb.nextInt();
            if(number <0)
                break;
            numberChannel.writeInt(number);
            sum+=number;
            count++;
            if(number>= highest)
                highest = number;

        }
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found");
    }
    catch(IOException e)
    {
        System.out.println("Cannot write file");
    }       
    finally
    {
        Scanner fileReader = new Scanner(name);
        while(fileReader.hasNextInt())
        {
            System.out.println(number);
        }

        fileReader.close();

        System.out.println("Average: " + ((double)sum/(double)count));
        System.out.println("Highest: " +highest);
        try
        {
            if (numberChannel!= null)
                numberChannel.close();
        }
        catch(IOException e)
        {

        }
    }       
}

感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

根据我给出的评论,使用PrintWriter写入文件,如下所示:

PrintWriter writer = new PrintWriter(new File(name));
writer.println(number);

并使用Scanner从文件中读取:

Scanner fileReader = new Scanner(new File(name));
while (fileReader.hasNextLine()) {
      String nu = fileReader.nextLine();
      System.out.println(nu);
}

编辑您的代码版本:

public static void main(String[] args) throws IOException {
        FileOutputStream outChannel = null;
        DataOutputStream numberChannel = null;
        Scanner kb = new Scanner(System.in);
        int sum = 0;
        int count = 0;
        int highest = 0;
        int number = 0;
        String name = "";
        try {
            System.out.println("Enter the name of the binary file: ");
            name = kb.nextLine();
            PrintWriter writer = new PrintWriter(new File(name));
            System.out.println("Enter negative value to stop.");
            System.out.println("Enter scores: ");
            while (true) {
                number = kb.nextInt();
                if (number < 0) {
                    break;
                }
                writer.println(number);
                sum += number;
                count++;
                if (number >= highest) {
                    highest = number;
                }

            }
            writer.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found");
        } finally {
            Scanner fileReader = new Scanner(new File(name));
            while (fileReader.hasNextLine()) {
                String nu = fileReader.nextLine();
                System.out.println(nu);
            }

            fileReader.close();

            System.out.println("Average: " + ((double) sum / (double) count));
            System.out.println("Highest: " + highest);
            try {
                if (numberChannel != null) {
                    numberChannel.close();
                }
            } catch (IOException e) {

            }
        }
    }