[英]Java Program to read, union and save data
我对Java
非常陌生,感谢您在我试图解决的主题方面的帮助。 我想编写一个小的Java
程序,它可以:
union
它们我到了不确定我写的代码是否真的有效的地步,我被困在显示数据上。 我准备了两个数据文件的小例子:
车
Row Description Type Example
1 Name String XX
2 Year int 2021
3 Value double 0.37
4 Area double 2.84
5 Weight double 1000
6 Tyre String 210/10 R1
马达
Row Description Type Example
1 A String XY
2 B double 230
3 C int 1000
4 D int 3400
5 E int 1000
您可以使用JFileChooser
找到我的Java
代码,该代码目前能够打开对话框 select 文件,显示路径并读取数据(1)。 我坚持显示两个表的并union
并下载文件。 谢谢你的帮助!
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.*;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
@SuppressWarnings("serial")
public class JFileChooserExample extends JPanel {
// use a list model and JList that works *directly* with Files
private DefaultListModel<File> fileListModel = new DefaultListModel<>();
private JList<File> fileJList = new JList<>(fileListModel);
List<String> kfz_list = new ArrayList<>();
List<String> motor_list = new ArrayList<>();
static Path filePath;
static Path filePath2;
static Path car;
static Path motor;
public JFileChooserExample() {
JPanel buttonPanel = new JPanel();
buttonPanel.add(new JButton(new SelectFilesAction("Select Files", KeyEvent.VK_S)));
// help set the width and height of the JList
fileJList.setVisibleRowCount(10);
fileJList.setPrototypeCellValue(new File("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"));
JScrollPane scrollPane = new JScrollPane(fileJList);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(buttonPanel, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
private class SelectFilesAction extends AbstractAction {
public SelectFilesAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(true);
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = fc.showOpenDialog(JFileChooserExample.this);
if(result == JFileChooser.APPROVE_OPTION) {
fileListModel.clear(); // clear the model of prior files
File[] files = fc.getSelectedFiles();
for (File file : files) {
// add all files to the model
boolean matches = Pattern.matches(".*car*.", file.getPath());
boolean matches2 = Pattern.matches(".*motor*.", file.getPath());
if(matches) {
car = file.toPath();
}
if(matches2) {
motor = file.toPath();
}
System.out.println("car path");
System.out.println(car);
System.out.println("motor path");
System.out.println(motor);
Charset charset = StandardCharsets.UTF_8;
// car read ans instantiate
try (BufferedReader bufferedReader = Files.newBufferedReader(car, charset)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
// System.out.println(line);
kfz_list.add(line);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
Kfz kfz1 = new Kfz();
//kfz1.Kfz("A", 2015, 23.5, 2.3, "B", 12.0);
kfz1.Kfz(kfz_list.get(0), Integer.parseInt(kfz_list.get(1)), Double.parseDouble(kfz_list.get(2)), Double.parseDouble(kfz_list.get(3)), Double.parseDouble(kfz_list.get(4)), kfz_list.get(5));
System.out.println("checking kfz1");
System.out.println(kfz1.getName() + " " + kfz1.getBaujahr());
// motor read and instantiate
try (BufferedReader bufferedReader2 = Files.newBufferedReader(motor, charset)) {
String line2;
while ((line2 = bufferedReader2.readLine()) != null) {
// System.out.println(line2);
motor_list.add(line2);
}
} catch (IOException ex) {
System.out.format("I/O error: %s%n", ex);
}
Motor motor1 = new Motor();
motor1.Motor(motor_list.get(0), Double.parseDouble(motor_list.get(1)), Integer.parseInt(motor_list.get(2)), Integer.parseInt(motor_list.get(3)), Integer.parseInt(motor_list.get(4)));
System.out.println("checking motor1");
System.out.println("Bezeichnung: " + motor1.getBezeichnung() + " " + "Max_drehmoment: " + motor1.getMax_drehmoment());
// end
fileListModel.addElement(file);
}
}
}
}
private static void createAndShowGui() {
JFileChooserExample mainPanel = new JFileChooserExample();
JFrame frame = new JFrame("JFileChooser Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
在使用 GUI 程序时,我建议遵循 MVC 范例,其中所有 GUI 组件都位于 Gui class。 Controller class 接收 GUI 调用并将它们保存在模型中并从模型更新 GUI 调用。
为了您的方便,请使用 NetBeans IDE 您已经集成了 Visual GUI 构建器
我故意省略源代码示例,因为如您所见,您将需要一些示例,来自以下主题:
GUI class 扩展自 javax.swing.JFrame
Controller将实现所有侦听器,如 ActionListener、MouseListener、KeyListener 并将初始化所有模型。 将从 GUI 同步到 Model
模型 模型之一是 TableModel 要创建自定义 TableModel,只需从 AbstractTableModel 扩展您的 class 并覆盖方法使用二维数组来存储表数据。
下载文件要将数据从 Model 下载到文件中,您需要创建 Java object 并实现可序列化接口
另存为 Java
try {
FileOutputStream fout = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(cars);
out.close();
} catch (IOException ex) {
Logger.getLogger(CarCatalog.class.getName()).log(Level.SEVERE, null, ex);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.