java.io.IOException: Input/output error

5 篇文章 0 订阅
1 篇文章 0 订阅

现象:项目中创建文件时正常,但是想加锁再写入时,在加锁使用FileChannel.tryLock后发生如下异常

java.io.IOException: Input/output error
	at sun.nio.ch.FileDispatcherImpl.lock0(Native Method)
	at sun.nio.ch.FileDispatcherImpl.lock(FileDispatcherImpl.java:94)
	at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:1114)
	at java.nio.channels.FileChannel.tryLock(FileChannel.java:1155)
	at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.onApplicationEvent(AnnotationAwareAspectJAutoProxyCreator.java:147)
	at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.onApplicationEvent(AnnotationAwareAspectJAutoProxyCreator.java:66)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361)
	at org.springframework.boot.context.event.EventPublishingRunListener.running(EventPublishingRunListener.java:108)
	at org.springframework.boot.SpringApplicationRunListeners.running(SpringApplicationRunListeners.java:77)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
	at 
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:107)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)

经过多轮排查发现是文件存储系统的问题,这里记录下排除方法,以防忘记

  • 通过命令查看存储磁盘的挂载参数
    mount | grep /data/cache/
    /data/cache 是存储位置

    不可加锁的参数是这样的

  • 可以加锁的参数是这样的 

  • 这里的区别是local_lock,all :可以加锁  none:不可加锁

  • 修改磁盘挂载参数,增加nolock或者local_lock,但是要注意local_lock会被nolock覆盖

        修改方式如下红色圈起来的部分     

   

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Here's an example Java program that meets the requirements you've described: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class AverageCalculator { public static void main(String[] args) { if (args.length != 1) { System.err.println("Usage: java AverageCalculator <filename>"); System.exit(1); } String filename = args[0]; int sum = 0; int count = 0; try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { String line; while ((line = reader.readLine()) != null) { try { int num = Integer.parseInt(line); sum += num; count++; } catch (NumberFormatException e) { System.err.println("Invalid integer: " + line); } } if (count == 0) { System.err.println("No valid integers found in file."); } else { double average = (double) sum / count; System.out.println("Average: " + average); } } catch (IOException e) { System.err.println("Error reading file: " + e.getMessage()); } catch (ArithmeticException e) { System.err.println("Error calculating average: " + e.getMessage()); } } } ``` Here's how the program works: 1. It checks that the user has provided a single command line argument specifying the filename. 2. It initializes variables to keep track of the sum and count of integers in the file. 3. It creates a BufferedReader to read the file line by line. 4. For each line, it attempts to parse it as an integer using Integer.parseInt(). If successful, it updates the sum and count variables. If not, it prints an error message but continues processing the rest of the file. 5. After processing the entire file, it checks whether any valid integers were found. If not, it prints an error message. Otherwise, it calculates the average and prints it to the console. 6. If any exceptions occur during file input/output or calculation of the average, the program catches them and prints appropriate error messages to the user.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值