我有一个应用程序,我有时需要从正在写入的文件中读取并因此被锁定.正如我从其他问题中理解的那样,我应该捕获IOException并重试直到我能够阅读.
但我的问题是我如何确定该文件已被锁定,并且它不是另一个IOExcetpion发生的.
我试图使用我在本页底部找到的一些代码.这是我为它创建的类中的代码:
import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;
public class LineCounter {
public static int countLines(String filename) throws IOException {
LineNumberReader reader = new LineNumberReader(new FileReader(filename));
int cnt = 0;
String lineRead = "";
while ((lineRead = reader.readLine()) != null) {}
cnt = reader.getLineNumber();
reader.close();
return cnt;
}
}
Run Code Online (Sandbox Code Playgroud)
我的目标是计算文本文件的行,将该数字存储为整数,然后在我的主类中使用该整数. 在我的主要课程中,我尝试了几种不同的方法来实现这一点,但是(作为一名新程序员)我错过了一些东西.这是我尝试的第一件事:
String sFileName = "MyTextFile.txt";
private int lineCount = LineCounter.countLines(sFileName);
Run Code Online (Sandbox Code Playgroud)
通过此尝试,我得到错误"未报告的异常java.io.IOException;必须被捕获或声明被抛出".我不明白为什么我得到这个,因为我可以看到异常是在我的"countLines"方法中声明的.我尝试在我发布的最后一段代码下使用try catch块,但这也没有用(我不认为我做得对).这是我试试的尝试:
String sFileName = "MyTextFile.txt";
private int lineCount;{
try{
LineCounter.countLines(sFileName);
}
catch(IOException ex){
System.out.println (ex.toString());
System.out.println("Could not find …Run Code Online (Sandbox Code Playgroud) 在以下情况下发生写入结束异常:两个线程:
A: PipedOutputStream put = new PipedOutputStream();
String msg = "MESSAGE";
output.wirte(msg.getBytes());
output.flush();
B: PipedInputStream get = new PipedOutputStream(A.put);
byte[] get_msg = new byte[1024];
get.read(get_msg);
Run Code Online (Sandbox Code Playgroud)
情况如下:A和B同时运行,A写入管道,B读取它.B只是从管道中读取并清除了该管道的缓冲区.然后A不会在未知的时间间隔内将msg写入管道.但是,在某一时刻,B再次读取管道并java.io.IOException: write end dead发生,因为管道的缓冲区仍然是空的.而且我不想睡眠()线程B等待A写管道,这也是不稳定的.如何避免这个问题并解决它?谢谢
我在一个大约100个请求/秒的网站上运行Jetty,前面有nginx.我刚刚在日志中注意到,在部署和启动Jetty之后的几分钟内,有一段时间它是垃圾邮件:
java.io.IOException: Too many open files
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:163)
at org.mortbay.jetty.nio.SelectChannelConnector$1.acceptChannel(SelectChannelConnector.java:75)
at org.mortbay.io.nio.SelectorManager$SelectSet.doSelect(SelectorManager.java:673)
at org.mortbay.io.nio.SelectorManager.doSelect(SelectorManager.java:192)
at org.mortbay.jetty.nio.SelectChannelConnector.accept(SelectChannelConnector.java:124)
at org.mortbay.jetty.AbstractConnector$Acceptor.run(AbstractConnector.java:708)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Run Code Online (Sandbox Code Playgroud)
一两分钟.我做了一个"lsof -u jetty"并看到了数百行:
java 15892 jetty 1020u IPv6 298105434 0t0 TCP 192.168.1.100:http-alt->192.168.1.100:60839 (ESTABLISHED)
java 15892 jetty 1021u IPv6 298105438 0t0 TCP 192.168.1.100:http-alt->192.168.1.100:60841 (ESTABLISHED)
java 15892 jetty 1022u IPv6 298105441 0t0 TCP 192.168.1.100:http-alt->192.168.1.100:60842 (ESTABLISHED)
java 15892 jetty 1023u IPv6 298105443 0t0 TCP 192.168.1.100:http-alt->192.168.1.100:60843 (ESTABLISHED)
Run Code Online (Sandbox Code Playgroud)
其中192.168.1.100是服务器内部IP.
正如你所看到的,这使得打开文件的数量达到默认的最大值1024.我可以增加它,但我想知道为什么会发生这种情况?它位于Jetty的nio socket接受器中,这是由连接请求风暴引起的吗?
我正在编写一个程序,我正在尝试在当前目录中创建一个新的文本文件,然后写一个字符串给它.但是,在尝试创建文件时,这段代码:
//Create the output text file.
File outputText = new File(filePath.getParentFile() + "\\Decrypted.txt");
try
{
outputText.createNewFile();
}
catch (IOException e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
给我这个错误信息:
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at code.Crypto.decrypt(Crypto.java:55)
at code.Crypto.main(Crypto.java:27)
Run Code Online (Sandbox Code Playgroud)
因此,我无法写入文件,因为它自然不存在.我在这做错了什么?
我对程序最近开始抛出的错误感到困惑.
java.io.IOException: No space left on device
at java.io.FileInputStream.close0(Native Method)
at java.io.FileInputStream.close(FileInputStream.java:259)
at java.io.FilterInputStream.close(FilterInputStream.java:155)
Run Code Online (Sandbox Code Playgroud)
我假设因为这是一个FileInputStream,该文件被保存在内存中,而不是物理磁盘上.内存级别看起来很棒,磁盘空间也是如此.这特别令人困惑,因为它发生在FileInputStream的结束时.感谢您对如何发生这种情况的任何解释.
编辑:审查代码
if (this.file.exists()) {
DataInputStream is = new DataInputStream(new FileInputStream(this.file));
this.startDate = new DateTime(is.readLong(), this.timeZone);
this.endDate = new DateTime(is.readLong(), this.timeZone);
is.close();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我只打开文件,阅读一些内容,然后关闭文件.
我正在为写入Process对象的StandardInput流的代码执行一些异常处理.进程有点像unix head命令; 它只读取部分输入流.当进程终止时,写入线程失败:
IOException
The pipe has been ended. (Exception from HRESULT: 0x8007006D)
Run Code Online (Sandbox Code Playgroud)
我想捕获这个异常并让它优雅地失败,因为这是预期的行为.但是,对我来说,如何将其与其他IOExceptions进行稳健的区分并不明显.我可以使用消息,但我理解这些是本地化的,因此这可能不适用于所有平台.我也可以使用HRESULT,但我找不到任何指定此HRESULT仅适用于此特定错误的文档.这样做的最佳方式是什么?
在Web服务中执行代码:
using (File.OpenRead(@"\\server\file.txt"))
{
// some stuff
}
Run Code Online (Sandbox Code Playgroud)
导致IO异常只是说"无效签名"
使用File静态方法时,我无法通过谷歌找到有关此错误消息的任何内容.
var exists = File.Exists(@"\\server\file.txt"))
Run Code Online (Sandbox Code Playgroud)
但是不会抛出异常,这使得它看起来好像可以看到那里的文件,但它无法打开它进行读取.
对于"无效签名"在这个例子中试图说的话,我真的很茫然.
谢谢.
更新:
即使使用模拟从服务器打开读取文件,它仍然会使用"无效签名"抛出IO异常.
堆:
<StackTrace>
<StackFrame Index="6">
<FileName />
<Method>Void RenderImages(System.String, System.Nullable`1[System.Int32])</Method>
<LineNumber>0</LineNumber>
<ColumnNumber>0</ColumnNumber>
<ILOffset>88</ILOffset>
</StackFrame>
<StackFrame Index="5">
<FileName />
<Method>Void .ctor(System.String, Boolean)</Method>
<LineNumber>0</LineNumber>
<ColumnNumber>0</ColumnNumber>
<ILOffset>0</ILOffset>
</StackFrame>
<StackFrame Index="4">
<FileName />
<Method>Void .ctor(System.String, System.Text.Encoding, Boolean, Int32)</Method>
<LineNumber>0</LineNumber>
<ColumnNumber>0</ColumnNumber>
<ILOffset>7</ILOffset>
</StackFrame>
<StackFrame Index="3">
<FileName />
<Method>Void .ctor(System.String, System.Text.Encoding, Boolean, Int32, Boolean)</Method>
<LineNumber>0</LineNumber>
<ColumnNumber>0</ColumnNumber>
<ILOffset>113</ILOffset>
</StackFrame>
<StackFrame Index="2">
<FileName />
<Method>Void .ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32, …Run Code Online (Sandbox Code Playgroud) 我在构建一个电子商务应用程序时遇到以下错误.
它正在成功构建但是当我尝试运行此应用程序时,给我错误如下:
我的build.gradle有以下依赖项:
dependencies {
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '27.1.0'
}
}
}
}
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:27.1.0'
compile 'com.android.support:design:27.1.0'
compile 'com.android.support:cardview-v7:27.1.0'
compile 'com.android.support:recyclerview-v7:27.1.0'
compile 'com.android.support:support-v4:27.1.0'
compile 'com.android.support:preference-v7:27.1.0'
compile 'com.android.support:preference-v14:27.1.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.slider:library:1.1.5@aar'
compile 'com.crystal:crystalrangeseekbar:1.1.1'
compile …Run Code Online (Sandbox Code Playgroud) 我有运行机器和Spring (Spring Boot 1.5.2.RELEASE)应用程序.最近我在我的日志文件中收到了很多警告:
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: null; nested exception is java.net.SocketTimeoutException
Run Code Online (Sandbox Code Playgroud)
我已经检查过tcpdump并且很多请求没有正文(空/ null)或者身体不正确,例如\00\00\00\00\00\00\00speed":"23.3","user_id":106312}
最大的问题是,经过一段时间后,我开始在我的应用程序中收到异常:
org.apache.tomcat.util.net.NioEndpoint : Socket accept failed
java.io.IOException: Too many open files
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:241)
at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:443)
at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)
ulimit -n说开放文件的限制是设置为65536所以我认为它足够大.
我假设接收大量无效请求会导致IOException,但为什么呢?我应该怎么做才能避免它并修复它?
ioexception ×10
java ×6
c# ×3
.net ×2
android ×1
android-ndk ×1
exception ×1
filelock ×1
hresult ×1
jetty ×1
nio ×1
spring ×1
spring-boot ×1
text-files ×1
try-catch ×1