工作中经常需要处理SQL文件、处理Excel,也经常有很多Word与在线文档可以转成Excel处理。
现就读取SQL文件,写入SQL文件,读取Excel表格、写入Excel做代码备份。
1、常用DEMO
1.1、常用修改文件
使用 FileWriter、BufferedWriter、PrintWriter 等常用的类。
try {
FileWriter fileWriter = new FileWriter("filepath");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("data to be written");
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
使用 PrintWriter 向文件写入数据:
try {
PrintWriter printWriter = new PrintWriter(new File("filepath"));
printWriter.println("data to be written");
printWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
在指定的行号插入一行文本:
Path path = Paths.get("filepath");
List<String> lines = Files.readAllLines(path, Charset.forName("UTF-8"));
lines.add(lineNumber, "data to be added");
Files.write(path, lines, Charset.forName("UTF-8"));
1.2、常用读取文件
使用 FileInputStream、BufferedReader、Scanner 等类。
使用 BufferedReader 逐行读取文本文件:
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader("filepath"));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
使用 Scanner 逐行读取文本文件:
try {
Scanner scanner = new Scanner(new File("filepath"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
读取二进制文件:
try {
InputStream inputStream = new FileInputStream("filepath");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
// 处理读取的数据
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
2、操作实例
怎么方便怎么来,创建一个Maven工程即够。
<dependencies>
<!--工具类-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
2.1、SQL文件操作
读取SQL文件,可以选择绝对路径,也可以选择resources下。
按行读取。
public void parseSql() {
File file = new File("D:\\xxx.sql");
List<MdbDTO> result = new ArrayList<>();
try (BufferedReader bf = new BufferedReader(new FileReader(file))) {
//按行读取
String line = null;
while ((line = bf.readLine()) != null) {
//doSomething to line;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
把String内容写入文件
private static void makeFile(StringBuilder sqlStr, String fileName) throws IOException {
String path = "src/main/resources/";
File file = new File(path + fileName);
if (!file.exists()) {
file.createNewFile();
} else {
file = new File(path + new Date().getTime() + "_" + fileName);
}
try (FileOutputStream outputStream = new FileOutputStream(file);) {
outputStream.write(sqlStr.toString().getBytes(StandardCharsets.UTF_8));
}
}
2.2、excel文件操作
可以在Hutool官网看具体用法
public List<List<Object>> parseExcel(){
File file = new File("D:\\xxx\\xxx.xlsx");
ExcelReader reader = ExcelUtil.getReader(file);
List<List<Object>> readAll = reader.read();
return readAll ;
}
2.3、文件传输接口
@RestController
@RequestMapping("/file")
public class FileController {
@PostMapping("/upload")
public String upload(@RequestParam("file") MultipartFile file) {
try {
// 获取原始文件名
String fileName = file.getOriginalFilename();
// 获取文件内容
byte[] bytes = file.getBytes();
//TODO 处理文件内容
return "success";
} catch (Exception e) {
e.printStackTrace();
return "failed";
}
}
}