【问题标题】:Java - binary file I/O with array-listJava - 带有数组列表的二进制文件 I/O
【发布时间】:2012-05-05 08:28:15
【问题描述】:

我是 java 初学者,在使用数组列表的二进制文件输入和输出时遇到问题。我正在尝试将数组列表中的数据存储在一个文件中,然后使用它在控制台中显示它。这是我的一些代码,它运行但显示错误信息,我也收到警告。对导致此问题的原因有任何帮助吗?谢谢!

public class Towers {
    public static ArrayList<String> allMoves= new ArrayList<String>();
    static{
        allMoves.add( "These Are the Disk Moves:" );
    }

    public static void move(final int aNumDisks){
        move(aNumDisks, 1, 2, 3);
        String fileName = "solution.dat";
        try{
            ObjectOutputStream outputStream = 
                    new ObjectOutputStream(
                            new FileOutputStream (fileName));
            outputStream.writeObject(allMoves);
            outputStream.close( );
        }
        catch ( IOException e ){
            System.out.println("Error writing to file " + fileName);
            System.exit(0);
        }
    }

在控制台中显示输出文件。

public class TReporter {        
    public static void reportSol(){
        String fileName = "solution.dat";
        ArrayList<String> allMovesA = null;
        try{
            ObjectInputStream inputStream =
                    new ObjectInputStream(
                            new FileInputStream (fileName));
            allMovesA = (ArrayList<String>)inputStream.readObject(); //WARNING!
                           //Type safety: Unchecked cast from Object to ArrayList<String>
            inputStream.close();
        }
        catch (Exception e){
            System.out.println("Problem reading the file " + fileName);
            System.exit(0);
        }
        for (int i = 0; i < allMovesA.size(); ++i){
            System.out.println( allMovesA.get(i) );
        }

它应该显示如下内容:

=== 0 disks move(int) ===
These Are the Disk Moves:

=== 0 disks move(,,) ===
These Are the Disk Moves:

=== 1 disk move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3

=== 2 disks ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 2 disks move(2,3,2,1) ===
These Are the Disk Moves:
Move disk from 3 to 2
Move disk from 3 to 1
Move disk from 2 to 1

=== 3 disks move(3) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

但我得到了这个:

=== 0 disks move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

=== 0 disks move(,,) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

=== 1 disk move(int) ===
These Are the Disk Moves:
Move disk from 1 to 3

=== 2 disks ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 2 disks move(2,3,2,1) ===
These Are the Disk Moves:
Move disk from 1 to 2
Move disk from 1 to 3
Move disk from 2 to 3

=== 3 disks move(3) ===
These Are the Disk Moves:
Move disk from 1 to 3
Move disk from 1 to 2
Move disk from 3 to 2
Move disk from 1 to 3
Move disk from 2 to 1
Move disk from 2 to 3
Move disk from 1 to 3

【问题讨论】:

  • 您希望显示什么?您目前看到的是什么?你得到什么警告?您需要发布这些内容,以便人们了解您的程序为何无法运行。
  • 是作业吗?如果是,请将其标记为这样。我没有看到任何主要方法或任何移动(int,int,int,int)。也请发帖

标签: java binary arraylist objectoutputstream objectinputstream


【解决方案1】:

您收到的警告是完全正常的。您可以通过在带有警告的行之前插入此行来抑制它:

@SuppressWarnings("unchecked")

对于你得到的错误信息:

  • 确保在尝试读取之前写入文件,您可能使用的是旧版本的文件。
  • 确保它确实的 ArrayList 包含正确的移动。

【讨论】:

    猜你喜欢
    • 2012-05-28
    • 2016-06-15
    • 2018-05-02
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 1970-01-01
    相关资源
    最近更新 更多