2013-05-18 2 views
2

У меня есть небольшое приложение, предназначенное для получения двойных чисел и хранения их в двоичном файле. Затем прочитайте их все поодиночке и сохраните их в массиве, но я не могу понять, как правильно прочитать файл?Как читать двоичный файл двойных чисел с помощью Java?

Вот код:

 import java.awt.*; 
    import java.awt.event.ActionEvent; 
    import java.awt.event.ActionListener; 
    import java.io.*; 
    import java.util.ArrayList; 

    import net.miginfocom.swing.MigLayout; 

    import javax.swing.*; 


    public class Q3 extends JFrame { 

private JPanel thePanel; 
private JLabel lblDouble; 
private JTextField txtDouble; 
private JButton btnAdd, btnStore, btnRead; 

ArrayList<Double> doubleNumberArray = new ArrayList<Double>(); 
ArrayList readArr = new ArrayList(); 
int index = 0; 
int index2 = 0; 

String fileName = "data.dat"; 

FileOutputStream fileOut = null; 
DataOutputStream dOut = null; 

FileInputStream fileIn = null; 
DataInputStream dIn = null; 

public static void main(String[] args) { 

    new Q3(); 


} 

public Q3() { 

    this.setSize(250, 150); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 

    Color myColor = new Color(54, 139, 255); 

    thePanel = new JPanel(new MigLayout()); 
    thePanel.setBackground(myColor); 

    lblDouble = new JLabel("Enter a Double "); 
    // Text Field 
    txtDouble = new JTextField(5); 
    // Buttons 
    btnAdd = new JButton("Add"); 

    btnStore = new JButton("Store"); 

    btnRead = new JButton("Read File"); 

    ListenerForButton lForAddButton = new ListenerForButton(); 
    // Adding action listener to buttons 
    btnAdd.addActionListener(lForAddButton); 
    btnStore.addActionListener(lForAddButton); 
    btnRead.addActionListener(lForAddButton); 

    thePanel.add(lblDouble); 
    thePanel.add(txtDouble, "wrap"); 
    thePanel.add(btnAdd, "skip1,split2"); 
    thePanel.add(btnStore, "wrap"); 
    thePanel.add(btnRead, "skip1"); 


    this.add(thePanel); 
    this.setVisible(true); 

} 

// Implement Listener 

public class ListenerForButton implements ActionListener { 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     if (e.getSource() == btnAdd) { 

      double convertDouble = Double.parseDouble(txtDouble.getText()); 
      doubleNumberArray.add(index, convertDouble); 
      index++; 
      txtDouble.setText(""); 

      System.out.print(doubleNumberArray); 

     } else if (e.getSource() == btnStore) { 

      for (int i = 0; i < doubleNumberArray.size(); i++) { 

       try { 

        fileOut = new FileOutputStream(fileName); 

        dOut = new DataOutputStream(fileOut); 

        dOut.writeDouble(doubleNumberArray.get(i)); 


       } catch (Exception ex) { 

        ex.printStackTrace(); 

       } finally { 
        try { 
         dOut.close(); 


        } catch (IOException e1) { 

         e1.printStackTrace(); 
        } 
       } 

      } // end of loop 

      //System.out.println("Done"); 
      doubleNumberArray.clear();// empty our array 
      index = 0; 

     } else if (e.getSource() == btnRead) { 

      try { 

       fileIn = new FileInputStream(fileName); 
       dIn = new DataInputStream(fileIn); 

       System.out.println("Din" + dIn.available()); 

       try { 

        double d ; 

        while (dIn.available() > 0) { 

         d = dIn.readDouble(); 
         readArr.add(d); 



        } 

       } catch (IOException e1) { 
        e1.printStackTrace(); 
       } 

      } catch (Exception exception) { 
       exception.printStackTrace(); 
      } 

      System.out.print(readArr); 

     } 


    }// end of read button 

}// action performed 

} // конец слушателя

+0

Я рад, что это было разрешено. Не забудьте принять ответ, спасибо! –

ответ

3

Используйте DataInputStream и DataOutStream, если вы хотите, чтобы читать и писать примитивные типы соответственно. This example поможет вам начать работу.

Образец для чтения до конца файла. Обратите внимание, что DataStreams обнаруживает условие конца файла, перехватывая EOFException, вместо проверки на недопустимое возвращаемое значение. Все реализации методов DataInput используют EOFException вместо возвращаемых значений.

public class Q3 extends JFrame 
{ 
    private final JPanel thePanel; 
    private final JLabel lblDouble; 
    private final JTextField txtDouble; 
    private final JButton btnAdd, btnStore, btnRead; 

    private final List<Double> doubleNumberArray = new ArrayList<Double>(); 
    private final List<Double> readArr = new ArrayList<Double>(); 
    private int index = 0; 

    private final String fileName = "c:/home/data.dat"; 

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

    public Q3() 
    { 
    this.setSize(250, 150); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 

    Color myColor = new Color(54, 139, 255); 

    thePanel = new JPanel(); 
    thePanel.setBackground(myColor); 

    lblDouble = new JLabel("Enter a Double "); 
    // Text Field 
    txtDouble = new JTextField(5); 
    // Buttons 
    btnAdd = new JButton("Add"); 
    btnStore = new JButton("Store"); 
    btnRead = new JButton("Read File"); 

    // Adding action listener to buttons 
    btnAdd.addActionListener(new ListenerForButton()); 
    btnStore.addActionListener(new StoreButtonListener()); 
    btnRead.addActionListener(new ReadButtonListener()); 

    thePanel.add(lblDouble); 
    thePanel.add(txtDouble, "wrap"); 
    thePanel.add(btnAdd, "skip1,split2"); 
    thePanel.add(btnStore, "wrap"); 
    thePanel.add(btnRead, "skip1"); 

    this.add(thePanel); 
    this.setVisible(true); 
    } 

    public class ReadButtonListener implements ActionListener 
    { 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     DataInputStream din = null; 
     try 
     { 
     din = new DataInputStream(new FileInputStream(fileName)); 
     readArr.clear(); 

     while (true) 
     { 
      Double data = din.readDouble(); 
      System.out.printf("\n-> %s \n ", data); 
      readArr.add(data); 
     } 
     } 
     catch (EOFException ignore) 
     { 
     } 
     catch (Exception ioe) 
     { 
     ioe.printStackTrace(); 
     } 
     finally 
     { 
     if (din != null) 
     { 
      try 
      { 
      din.close(); 
      } 
      catch (IOException e1) 
      { 
      e1.printStackTrace(); 
      } 
     } 
     } 
    } 
    } 

    public class StoreButtonListener implements ActionListener 
    { 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     DataOutputStream outFile = null; 
     try 
     { 
     outFile = new DataOutputStream(new FileOutputStream(fileName)); 

     for (int i = 0; i < doubleNumberArray.size(); i++) 
     { 
      Double d = doubleNumberArray.get(i); 
      System.out.printf("\nWriting to file %s", d); 
      outFile.writeDouble(d); 
     } 
     } 
     catch (Exception ex) 
     { 
     ex.printStackTrace(); 
     } 
     finally 
     { 
     if (outFile != null) 
     { 
      try 
      { 
      outFile.close(); 
      } 
      catch (IOException e1) 
      { 
      e1.printStackTrace(); 
      } 
     } 
     } 
     doubleNumberArray.clear();// empty our array 
     index = 0; 
    } 
    } 

    public class ListenerForButton implements ActionListener 
    { 
    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     double convertDouble = Double.parseDouble(txtDouble.getText()); 
     doubleNumberArray.add(index, convertDouble); 
     index++; 
     txtDouble.setText(""); 
    } 
    } 
} 
+0

Как добавить то, что читается в arraylist ??? – CYBERSIX

+0

sir, 'in.available()> 0' лучше, чем перехват исключения. –

+0

@RaviTrivedi available() не работает с DataInputStream. Я предлагаю вам прочитать это http://docs.oracle.com/javase/tutorial/essential/io/datastreams.html –

2

Использование DataInputStream для чтения из файла и DataOutputStream записать в файл.

Почему DataInputStream и DataOutputStream?

Поскольку эти классы поддерживают Reading и Writing в модифицированUTF-8. KG

Что такое модифицированоUTF-8?

МодифицированныйUTF-8 означает данных будут считаны и записаны в виде Java Primitive Types. так что вы можете read/write прямо java variables в файле.

Запись:

DataOutputStream out = new DataOutputStream(new FileOutputStream("MyBinaryFile.txt")); 
out.writeDouble(double d); 

Reading:

DataInputStream in = new DataInputStream(new FileInputStream("MyBinaryFile.txt")); 
while(in.available() > 0) 
{ 
in.readDouble(); 
} 

Примечание: Убедитесь, что вы Read и Write используя те же потока классов.

Ответ Обновлено:

1) Возьмите эти 2 линии выхода из for цикла.

fileOut = new FileOutputStream(fileName); 
dOut = new DataOutputStream(fileOut); 

2) Также проверьте, сколько элементов существует в doubleNumberArray перед записью в файл.

3) Возьмите эту строку double d из цикла while. Определите d снаружи во время цикла, а затем используйте d = dIn.readDouble().

4) Использовать readArr.add(d) вместо readArr.add(index2,d). Потому что ArrayList создает индексы самостоятельно, как вам нужно.

Смежные вопросы