<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.10.0</version>
</dependency>
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
public static List<String> txtReader(String filePath, int size) throws Exception {
InputStream stream = FileUtils.openInputStream(new File(filePath));
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
List<String> datas = new ArrayList<>();
if (size > 0) {
System.out.println("header: " + reader.readLine());
for (int i = 1; i <= size; i++) {
System.out.println("content: " + reader.readLine());
datas.add(reader.readLine());
}
} else {
String line;
while ((line = reader.readLine()) != null) {
datas.add(line);
}
}
reader.close();
return datas;
}
public static List<String> csvReader(String filePath, int size) throws Exception {
InputStream stream = FileUtils.openInputStream(new File(filePath));
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
CSVParser parse = CSVFormat.EXCEL.parse(reader);
List<String> datas = new ArrayList<>();
int num = 0;
for (CSVRecord record : parse.getRecords()) {
StringBuilder resBuffer = new StringBuilder();
for (int i = 0; i < record.size(); i++) {
resBuffer.append(record.get(i)).append(",");
}
resBuffer.setLength(resBuffer.length() - 1);
datas.add(resBuffer.toString());
if (size > 0 && num == size) {
break;
}
num++;
}
reader.close();
return datas;
}
public static void txtWriter(String filePath) throws Exception {
String txtFile = filePath + ".txt";
FileWriter writer = new FileWriter(txtFile, true);
String data = "张三,25,男";
writer.append(data);
writer.append("\n");
writer.close();
}
public static void csvWriter(String filePath) throws Exception {
String csvFile = filePath + ".csv";
FileWriter writer = new FileWriter(csvFile, true);
List<String> strList = new ArrayList<>();
for (int i = 0; i < 5; i++) {
String data = "张三" + i + ",25,男";
strList.add(data);
}
for (String str : strList) {
writer.append(str);
writer.append("\n");
}
writer.close();
}
public static void lockWrite(String filePath, String data) {
try (RandomAccessFile file = new RandomAccessFile(filePath, "rw");
FileOutputStream outputStream = new FileOutputStream(file.getFD());
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
FileChannel channel = file.getChannel();
try (FileLock ignored = channel.lock()) {
file.seek(file.length());
writer.write(data);
writer.write(System.lineSeparator());
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<String> lockRead(String filePath) {
List<String> datas = new ArrayList<>();
try {
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
FileChannel channel = file.getChannel();
try (FileLock ignored = channel.lock(0, Long.MAX_VALUE, true)) {
String line;
while ((line = file.readLine()) != null) {
datas.add(line);
}
}
file.setLength(0);
} catch (IOException e) {
e.printStackTrace();
}
return datas;
}
}