gpt4 book ai didi

java - 如何用java锁定Excel文件的读写?

转载 作者:太空宇宙 更新时间:2023-11-04 11:50:53 24 4
gpt4 key购买 nike

我正在使用 spring 读取输入 channel ,无论进入哪个文件,我都会选择相同的文件。基本上该文件是 Excel 文件,因此我使用了 apache POI 工作簿。下面是App类

@SpringBootConfiguration
@EnableScheduling
@Component
public class App
{
public static void main( String[] args ) throws IOException
{
SpringApplication.run(App.class, args);
}

@Scheduled(fixedRate = 60000)
public static void displayDirectories () throws InvalidFormatException, IOException{
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml", App.class);
File inDir = (File) new DirectFieldAccessor(context.getBean(FileReadingMessageSource.class)).getPropertyValue("directory");
LiteralExpression expression = (LiteralExpression) new DirectFieldAccessor(context.getBean(FileWritingMessageHandler.class)).getPropertyValue("destinationDirectoryExpression");
File outDir = new File(expression.getValue());
System.out.println("Input directory is: " + inDir.getAbsolutePath());
System.out.println("Archive directory is: " + outDir.getAbsolutePath());
System.out.println("===================================================");
}
}

下面是我的 bean context.xml 中的一些配置:

<file:inbound-channel-adapter id="filesIn"
directory="file:${java.io.tmpdir}/input">
<integration:poller id="poller" fixed-rate="60000"/>
</file:inbound-channel-adapter>

<integration:service-activator
input-channel="filesIn" output-channel="filesOut" ref="handler" />

<file:outbound-channel-adapter id="filesOut"
directory="file:${java.io.tmpdir}/archive" delete-source-files="true">
</file:outbound-channel-adapter>

<bean id="handler" class="com.practice.Handler" />

下面是我的处理程序类,我试图在其中获取 Excel 文件读取的锁定:

@SpringBootConfiguration
@Component
public class Handler {
FileInputStream fileIn = null;
FileLock lock = null;
public File handleFile(File input) {
try {
fileIn = new FileInputStream(input);
lock = fileIn.getChannel().tryLock();
Workbook filename = WorkbookFactory.create(fileIn);
.
/*Some logic*/
.
}
catch(Exception e){
System.out.println("Exception occured");
e.printStackTrace();
}
finally{
try {
lock.release();
fileIn.close();
} catch (IOException e) {
System.out.println("Exception in fileclose or lock");
e.printStackTrace();
}
}
return input;
}
}

不实现锁定会多次读取我的文件并多次处理它。我的任务是读取该文件一次并对其进行处理,然后将其存档。当我尝试在上面的代码中实现锁定时,我收到一个异常java.nio.channels.NonWritableChannelException。但是,它建议我将其更改为 POIFSFileSystem 来解决该问题。我尝试了谷歌上的建议,它们都仅建议仅适用于 RandomAccessFile。我正在为该项目使用 Spring Boot。对此的任何帮助将不胜感激。

最佳答案

我们可以锁定文件,读取它,修改工作表并写入它,然后释放锁定。这是一个重要的用例。每个电子表格程序都是这样工作的。

使用 Apache POI 4.1.1 的工作测试用例:

public static void main(String[] args) throws Exception{
File testFile = new File("src/main/resources/test.xls");
if(! testFile.exists()){
throw new FileNotFoundException(testFile.getAbsolutePath());
}
RandomAccessFile randomAccessFile = new RandomAccessFile(testFile, "rw");
FileChannel fileChannel = randomAccessFile.getChannel();
FileLock fileLock = fileChannel.tryLock();
if(fileLock == null){
throw new NullPointerException("Cannot get lock.");
}
boolean readOnly = false;
POIFSFileSystem filesystem = new POIFSFileSystem(fileChannel, readOnly);
HSSFWorkbook workbook = new HSSFWorkbook(filesystem);
workbook.write();
fileLock.release();
workbook.close();
if(!testFile.delete()){
throw new RuntimeException("Could not delete test file.");
}
}

关于java - 如何用java锁定Excel文件的读写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41844312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com