Java文件处理与向量操作全解析
立即解锁
发布时间: 2025-08-17 02:29:08 阅读量: 1 订阅数: 5 

### Java文件处理与向量操作全解析
#### 1. 带GUI的文件输入输出
如今,大多数应用程序都具备图形用户界面(GUI),为文件处理程序提供这样的界面是很有必要的。常见的文件处理应用程序会提供一个对话框,让用户能够浏览计算机的文件系统,选择要打开的现有文件或指定要创建文件的目标目录。
在Java中,我们可以使用Swing类`JFileChooser`来实现这个功能。`JFileChooser`对象会打开一个模态对话框(即必须关闭该窗口才能进行后续处理),显示系统的目录结构,允许用户进行浏览。
创建`JFileChooser`对象后,可以使用`setFileSelectionMode`方法指定用户可以选择的是文件、目录还是两者皆可,通过以下常量实现:
- `JFileChooser.FILES_ONLY`:仅允许选择文件。
- `JFileChooser.DIRECTORIES_ONLY`:仅允许选择目录。
- `JFileChooser.FILES_AND_DIRECTORIES`:允许选择文件和目录。
示例代码如下:
```java
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
```
接着,可以调用`showOpenDialog`或`showSaveDialog`方法。`showOpenDialog`显示带有“打开”和“取消”按钮的对话框,`showSaveDialog`显示带有“保存”和“取消”按钮的对话框。这两个方法都接受一个参数,指定`JFileChooser`的父组件,并返回一个整数值。
返回的整数值可以与以下内置常量进行比较:
- `JFileChooser.CANCEL_OPTION`:表示用户取消了操作。
- `JFileChooser.APPROVE_OPTION`:表示用户选择了文件。
示例代码如下:
```java
int selection = fileChooser.showOpenDialog(null);
if (selection == JFileChooser.APPROVE_OPTION) {
// 指定选择文件后要执行的操作
}
```
如果用户选择了文件,可以使用`getSelectedFile`方法获取对应的`File`对象。对于字符串和基本类型的串行输入输出,可以将`Scanner`对象(用于输入)或`PrintWriter`对象(用于输出)包装在`File`对象周围。
示例代码如下:
```java
File file = fileChooser.getSelectedFile();
Scanner fileIn = new Scanner(file);
PrintWriter fileOut = new PrintWriter(file);
```
对于对象的串行输入输出,可以将`ObjectInputStream`对象和`FileInputStream`对象或`ObjectOutputStream`对象和`FileOutputStream`对象包装在`File`对象周围。
示例代码如下:
```java
ObjectInputStream fileIn = new ObjectInputStream(new FileInputStream(file));
ObjectOutputStream fileOut = new ObjectOutputStream(new FileOutputStream(file));
```
由于使用了GUI,需要实现`ActionListener`接口来处理按钮选择。以下是一个简单的应用程序示例,用于读取考试成绩文件并逐个显示学生的成绩:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class UseFileChooser extends JFrame implements ActionListener {
private JPanel displayPanel, buttonPanel;
private JLabel surnameLabel, firstNamesLabel, markLabel;
private JTextField surnameBox, firstNamesBox, markBox;
private JButton openButton, nextButton;
private Scanner input;
public static void main(String[] args) {
UseFileChooser frame = new UseFileChooser();
frame.setSize(350, 150);
frame.setVisible(true);
}
public UseFileChooser() {
setTitle("FileChooser Demo");
setLayout(new BorderLayout());
displayPanel = new JPanel();
displayPanel.setLayout(new GridLayout(3, 2));
surnameLabel = new JLabel("Surname");
firstNamesLabel = new JLabel("First names");
markLabel = new JLabel("Mark");
surnameBox = new JTextField();
firstNamesBox = new JTextField();
markBox = new JTextField();
surnameBox.setEditable(false);
firstNamesBox.setEditable(false);
markBox.setEditable(false);
displayPanel.add(surnameLabel);
displayPanel.add(surnameBox);
displayPanel.add(firstNamesLabel);
displayPanel.add(firstNamesBox);
displayPanel.add(markLabel);
displayPanel.add(markBox);
add(displayPanel, BorderLayout.NORTH);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
openButton = new JButton("Open File");
openButton.addActionListener(this);
nextButton = new JButton("Next Record");
nextButton.addActionListener(this);
nextButton.setEnabled(false);
buttonPanel.add(openButton);
buttonPanel.add(nextButton);
add(buttonPanel, BorderLayout.SOUTH);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent event) {
if (input != null) {
input.close();
}
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == openButton) {
try {
openFile();
} catch (IOException ioEx) {
JOptionPane.showMessageDialog(this, "Unable to open file!");
}
} else {
try {
readRecord();
} catch (EOFException eofEx) {
nextButton.setEnabled(false);
JOptionPane.showMessageDialog(this, "Incomplete record!\nEnd of file reached.");
} catch (IOException ioEx) {
JOptionPane.showMessageDialog(this, "Unable to read file!");
}
}
}
public void openFile() throws IOException {
if (input != null) {
input.close();
input = null;
}
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int selection = fileChooser.showOpenDialog(null);
if (selection == JFileChooser.CANCEL_OPTION) {
return;
}
File results = fileChooser.getSelectedFile();
if (results == null || results.getName().equals("")) {
JOptionPane.showMessageDialog(this, "Invalid selection!");
return;
}
input = new Scanner(results);
readRecord();
nextButton.setEnabled(true);
}
public void readRecord() throws IOException {
String surname, firstNames, textMark;
surnameBox.setText("");
firstNamesBox.setText("");
markBox.setText("");
if (input.hasNext()) {
surname = input.nextLine();
surnameBox.setText(surname);
} else {
JOptionPane.showMessageDialog(this, "End of file reached.");
nextButton.setEnabled(false);
return;
}
if (!input.hasNext()) {
throw (new EOFException
```
0
0
复制全文
相关推荐










