ObjectOutputStream.writeObject(...)卡住了

时间:2016-11-04 02:28:15

标签: java io objectoutputstream

我有一个家庭作业,当它调用writeObject(...)方法时,它会卡在ObjectOutputStream上(用FileOutputStream启动)。该对象是一个HashMap。我从我教授给我的例子中复制了writeObject代码的2行,它给了我"使用未经检查或不安全的操作"在writeObject上发出警告并被卡住;教授的代码都没有发生过。

     import java.io.FileInputStream;
     import java.io.FileOutputStream;
     import java.io.ObjectInputStream;
     import java.io.ObjectOutputStream;
     import java.awt.event.KeyEvent;
     import java.awt.event.KeyAdapter;
     import java.awt.event.ActionListener;
     import java.awt.event.ActionEvent;
     import javax.swing.JLabel;
     import javax.swing.JButton;
     import javax.swing.JTextField;
     import java.awt.FlowLayout;
     import javax.swing.JFrame;
     import java.io.IOException;
     import java.io.FileNotFoundException;
     import java.util.Map;
     import java.util.HashMap;
     import java.io.FileReader;
     import java.io.BufferedReader;

     class ScrabbleWords
     {
        Map<String,Integer> secondHashMap = null;

        public static void main(String[]args)
        {
           new ScrabbleWords();
        }

        public ScrabbleWords()
        {
           BufferedReader br = null;
           BufferedReader brValues = null;
           try
           {
              br = new BufferedReader(new FileReader("Words.txt"));
              brValues = new BufferedReader(new FileReader("ScrabbleLetterValues.txt"));
           }
           catch(FileNotFoundException ex){ex.printStackTrace();}
           String nextLine = "";
           Map<String,Integer> map = new HashMap<String,Integer>();
           Map<Character,Integer> values = new HashMap<Character,Integer>();
           try
           {
              for(int i = 0; (nextLine = brValues.readLine())!=null; i++)
              {
                 String[] splitStr = nextLine.split(",");
                 values.put(Character.toLowerCase(splitStr[0].charAt(0)),Integer.parseInt(splitStr[1]));
              }
              while((nextLine = br.readLine())!=null)
              {
                 int value = 0;
                 for(char c: nextLine.toCharArray())
                 {
                    value += values.get(Character.toLowerCase(c));
                 }
                 map.put(nextLine,value);
              }
              br.close();
              brValues.close();
           }
           catch(IOException ex){ex.printStackTrace();}

           try
           {
              ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("Scrabble_Hash_Map.dat",true));
              output.writeObject(map);
              System.out.println("boo!");
              output.close();

              ObjectInputStream input = new ObjectInputStream(new FileInputStream("Scrabble_Hash_Map.dat"));
              Object obj = input.readObject();
              if(obj instanceof HashMap) secondHashMap = (HashMap)obj;
              else throw new RuntimeException("Input File did not contain instance of HashMap for deserialization.");
              input.close();
           }
           catch(IOException|ClassNotFoundException ex){ex.printStackTrace();}

           JFrame fr = new JFrame();
           fr.setTitle("Scrabble Word Value Look-Up");
           fr.setDefaultCloseOperation(fr.EXIT_ON_CLOSE);
           fr.setSize(300,100);
           fr.setLocation(300,300);
           fr.setLayout(new FlowLayout());

           JTextField field = new JTextField(10);
           JButton btn = new JButton("Look-Up");
           JLabel lbl1 = new JLabel("The Value Is: ");
           JLabel lbl2 = new JLabel();

           fr.add(field);
           fr.add(btn);
           fr.add(lbl1);
           fr.add(lbl2);

           field.addKeyListener(new KeyAdapter()
           {
              public void keyPressed(KeyEvent e)
              {
                 if(e.getKeyCode()==e.VK_ENTER)
                 {
                    btn.doClick();
                    field.requestFocus();
                 }
              }
           });
           btn.addActionListener(new ActionListener()
           {
              public void actionPerformed(ActionEvent e)
              {
                 int value = -1;
                 if(secondHashMap.get(field.getText())!=null) value = secondHashMap.get(field.getText().toLowerCase());
                 lbl2.setText((value < 0? "Word Not Found":String.valueOf(value)));
              }
           });

           //set window visible
           fr.setVisible(true);
        }
     }

0 个答案:

没有答案