import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class XlsxToCsvConverter { public static void main(String[] args) { try { // 读取 xlsx 文件 FileInputStream file = new FileInputStream(new File("input.xlsx")); XSSFWorkbook workbook = new XSSFWorkbook(file); Sheet sheet = workbook.getSheetAt(0); // 创建 csv 文件 FileWriter csvWriter = new FileWriter("output.csv"); // 遍历 xlsx 文件的每一行 for (Row row : sheet) { // 遍历每一行的每一列 for (Cell cell : row) { // 将单元格的值写入 csv 文件 csvWriter.append(cell.getStringCellValue()); csvWriter.append(","); } csvWriter.append("\n"); } // 关闭文件 csvWriter.flush(); csvWriter.close(); workbook.close(); file.close(); } catch (IOException e) { e.printStackTrace(); } } }需要的pom
时间: 2023-06-13 07:09:15 浏览: 61
文件中需要添加以下依赖:
```
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
```
这个依赖包含了操作 Excel 文件的相关类。
相关问题
根据以下要求:Instead of using a text file to save all the point coordinates one by one, change the savaData method and the constructor of the Model class to use object serialization and a binary file called "points.bin" to write / read the whole arraylist of points to / from the file in one operation.修改下述代码:import java.awt.Point; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Model { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; public Model() { points = new ArrayList<Point>(); listeners = new ArrayList<ModelListener>(); // Read points from file if it exists File file = new File("points.txt"); if (file.exists()) { try { Scanner scanner = new Scanner(file); while (scanner.hasNextLine()) { String[] coordinates = scanner.nextLine().split(" "); int x = (int) Double.parseDouble(coordinates[0]); int y = (int) Double.parseDouble(coordinates[1]); points.add(new Point(x, y)); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { points.add(p); notifyListeners(); // points changed so notify the listeners. saveData(); // save point to file } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. saveData(); // save empty list to file } public void deleteLastPoint() { if (points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. saveData(); // save updated list to file } } private void notifyListeners() { for (ModelListener l : listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public void saveData() { try { FileWriter writer = new FileWriter("points.txt"); for (Point p : points) { writer.write(p.x + " " + p.y + "\n"); } writer.close(); } catch (IOException e) { e.printStackTrace(); } }
以下是修改后的代码:
import java.awt.Point;
import java.io.*;
public class Model implements Serializable {
private ArrayList<Point> points;
private ArrayList<ModelListener> listeners;
private static final String FILE_NAME = "points.bin";
public Model() {
points = new ArrayList<Point>();
listeners = new ArrayList<ModelListener>();
// Read points from file if it exists
File file = new File(FILE_NAME);
if (file.exists()) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
points = (ArrayList<Point>) in.readObject();
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public void addListener(ModelListener l) {
listeners.add(l);
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
points.add(p);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save point to file
}
public void clearAllPoints() {
points.clear();
notifyListeners(); // points changed so notify the listeners.
saveData(); // save empty list to file
}
public void deleteLastPoint() {
if (points.size() > 0) {
points.remove(points.size() - 1);
notifyListeners(); // points changed so notify the listeners.
saveData(); // save updated list to file
}
}
private void notifyListeners() {
for (ModelListener l : listeners) {
l.update(); // Tell the listener that something changed.
}
}
public int numberOfPoints() {
return points.size();
}
public void saveData() {
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME));
out.writeObject(points);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这里我们使用了Java的序列化机制来保存整个ArrayList对象,同时将数据保存到了一个二进制文件中(名为"points.bin")。在构造函数中,我们首先检查是否存在该文件,如果存在,则通过ObjectInputStream读取该文件并将ArrayList对象恢复到程序中。在saveData()方法中,我们使用ObjectOutputStream将整个ArrayList对象写入文件。注意:我们还必须将Model类实现Serializable接口来支持序列化。
java io练习题_练习 Java类IO流练习题
1. 编写程序从文件中读取数据,并输出到控制台。
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFromFile {
public static void main(String[] args) {
try {
File file = new File("data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
```
2. 编写程序从控制台读取数据,并将数据写入文件。
```java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class WriteToFile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter data: ");
String data = scanner.nextLine();
scanner.close();
try {
File file = new File("output.txt");
FileWriter writer = new FileWriter(file);
writer.write(data);
writer.close();
} catch (IOException e) {
System.out.println("Error writing to file.");
}
}
}
```
3. 编写程序从一个文件中复制数据到另一个文件。
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) {
try {
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");
FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream outputStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
System.out.println("Error copying file.");
}
}
}
```
4. 编写程序读取一个目录下的所有文件,并输出文件名和文件大小。
```java
import java.io.File;
public class ListFiles {
public static void main(String[] args) {
File directory = new File(".");
File[] files = directory.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println(file.getName() + " (" + file.length() + " bytes)");
}
}
}
}
```