gpt4 book ai didi

java - 如何使用 Java 读取双数的二进制文件?

转载 作者:行者123 更新时间:2023-11-30 06:24:37 24 4
gpt4 key购买 nike

我有一个小应用程序,旨在获取双数并将它们存储到二进制文件中。然后再一一读取并存储到一个数组中,但我不知道如何正确读取文件?

代码如下:

     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

}//监听结束

最佳答案

使用DataInputStreamDataOutStream如果你想分别读写原始类型。 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("");
}
}
}

关于java - 如何使用 Java 读取双数的二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16620123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com