我可以用try-catch循环"修复"下面的异常,但我无法理解原因.
代码和IOExceptions
$ javac ReadLineTest.java
ReadLineTest.java:9: unreported exception java.io.IOException; must be caught or declared to be thrown
while((s=in.readLine())!=null){
^
1 error
$ cat ReadLineTest.java
import java.io.*;
import java.util.*;
public class ReadLineTest {
public static void main(String[] args) {
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// WHY IOException here?
while((s=in.readLine())!=null){
System.out.println(s);
}
}
}
Run Code Online (Sandbox Code Playgroud) 所有,
我试图确保在捕获IOException时关闭了我用BufferedReader打开的文件,但看起来好像我的BufferedReader对象超出了catch块的范围.
public static ArrayList readFiletoArrayList(String fileName, ArrayList fileArrayList)
{
fileArrayList.removeAll(fileArrayList);
try {
//open the file for reading
BufferedReader fileIn = new BufferedReader(new FileReader(fileName));
// add line by line to array list, until end of file is reached
// when buffered reader returns null (todo).
while(true){
fileArrayList.add(fileIn.readLine());
}
}catch(IOException e){
fileArrayList.removeAll(fileArrayList);
fileIn.close();
return fileArrayList; //returned empty. Dealt with in calling code.
}
}
Run Code Online (Sandbox Code Playgroud)
Netbeans抱怨它在catch块中"找不到符号fileIn",但是我想确保在IOException的情况下Reader被关闭.如果没有第一次尝试/捕获构造的丑陋,我怎么能这样做呢?
关于这种情况下的最佳实践的任何提示或指示表示赞赏,
java exception-handling try-catch ioexception bufferedreader
当我尝试运行以下命令时:
jar cvfm myjar.jar manifest.txt *.class
Run Code Online (Sandbox Code Playgroud)
我得到以下例外情况:
java.io.IOException: invalid header field
at java.util.jar.Attributes.read(Attributes.java:410)
at java.util.jar.Manifest.read(Manifest.java:199)
at java.util.jar.Manifest.<init>(Manifest.java:69)
at sun.tools.jar.Main.run(Main.java:171)
at sun.tools.jar.Main.main(Main.java:1176)
Run Code Online (Sandbox Code Playgroud)
我得到这些例外的原因是什么?
我已经在这个bug上工作了好几天了,无法解决它.
我写了一个HttpsURLConnection客户端来使用POST请求上传大文件(> 1GB).我还使用com.sun.net.httpserver.HttpServer实现了服务器端.由于文件很大,我必须在我的连接上使用:setFixedLengthStreamingMode/setChunkedStreamingMode设置(使用其中的任何一个来复制bug).请注意我也使用HTTPS连接进行上传.
我正在将文件同时上传到多个服务器(每个http客户端的单独线程,连接到不同的服务器).我已经设置了并发上传的限制,所以每次只有X个线程有一个打开的UrlConnection(错误是用X = [1..4]重现的).(其他线程在信号量上等待)
我的问题是这样的:当上传时间不到5分钟(准确度不到4:50分钟)时,一切正常.如果第一批线程需要超过5分钟才能完成,则会为每个活动线程抛出异常:
java.io.IOException:在sun.net.www.protocol.http.HttpURLConnection $ StreamingOutputStream.checkError(未知来源)sun.net.www.protocol.http.HttpURLConnection $ StreamingOutputStream.write(未知)将请求正文写入服务器时出错资源)
尝试写入HttpURLConnection输出流时抛出异常.[outputStream.write(缓冲液,0,LEN);]
下一批线程可以正常工作(即使它们需要超过5分钟).请注意,服务器完全相同,现在该过程不会失败,从而导致我认为问题不在服务器端.(如果是那么第二批也是假设在5分钟后失败......)
我在连接上有/没有连接/读取超时重现了这个问题.此外,在服务器端,我看到文件正在创建和增长,直到异常发生.客户端抛出异常大约20-40秒后,服务器将抛出IOException"读取超时".
我使用wireshark收集了一个TCP/IP示例,并且看到服务器在大约客户端异常时发送了一个FIN数据包,我不明白为什么.(所有连接似乎都在此之前运行)
我已经阅读了许多关于类似问题的线程,但找不到任何合适的解决方案.(包括使用java.net.URLConnection来触发和处理HTTP请求)
关于为什么会发生这种情况的任何想法?我怎样才能找到它的原因?我该如何解决?
非常感谢.
PS我没有发布代码,因为它很长......但如果它能帮助理解我的问题,我会很高兴这样做.
我们在Android市场上有应用程序,我们的用户从这个错误中获得了很多: java.io.IOException:设备上没有剩余空间
从我到目前为止发现的情况来看,我知道他们在外部存储上留下了足够的空间,而这只发生在Android 2.x上.
我在Linux文件系统中进行了一项小型研究,我发现除了空间限制之外,每个目录中的文件数量和根目录下的文件总数都有限制.但看起来我们的应用程序没有击中任何一个.
编辑:这不是安装问题,但在尝试保存下载的文件时.
我正在尝试创建一个基本的蓝牙应用程序,用于测试设备.
我从developer.android获得了代码.这是链接:http://developer.android.com/guide/topics/connectivity/bluetooth.html#ConnectingDevices
这是我的线程代码的一部分:
public void run() {
mBluetoothAdapter.cancelDiscovery();
Log.i(TAG, "Discovery Cancel!");
try {
Log.i(TAG, "Connection Started");
mmSocket.connect();
Log.i(TAG, "Connection Ended");
} catch (IOException e) {
try {
Log.e(TAG, "Connection Failed", e);
mmSocket.close();
} catch (IOException e2) {
Log.e(TAG, "Connection Close Failed", e2);
}
return;
}
Run Code Online (Sandbox Code Playgroud)
无论我尝试mmSocket.connect();过什么都行不通.总是抛出IOException并从logcat中获取该日志:
java.io.IOException: Service discovery failed
at android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:403)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:213)
我看了这些文章,并尝试了写的东西,没有一个解决了我的问题.
Android蓝牙java.io.IOException:连接被拒绝了?
顺便说一句,我正在研究android ics 4.0.4.
我知道这不是设备问题,因为我在不同的设备上尝试过这个应用程序.
我的应用程序的一部分通过蓝牙连接到设备,通常工作正常但偶尔它不会连接,我得到以下错误
03-11 10:29:20.328: E/BluetoothComService(8059): accept() failed
03-11 10:29:20.328: E/BluetoothComService(8059): java.io.IOException: Operation Canceled
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothSocket.accept(BluetoothSocket.java:316)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:105)
03-11 10:29:20.328: E/BluetoothComService(8059): at android.bluetooth.BluetoothServerSocket.accept(BluetoothServerSocket.java:91)
03-11 10:29:20.328: E/BluetoothComService(8059): at com.mypackage.name.bluetooth.BluetoothService$AcceptThread.run(BluetoothService.java:298)
Run Code Online (Sandbox Code Playgroud)
这是我得到例外的那一行
socket = mmServerSocket.accept();
Run Code Online (Sandbox Code Playgroud)
这是完整的AcceptThread
private class AcceptThread extends Thread {
// The local server socket
private BluetoothServerSocket mmServerSocket;
public boolean successInit = false;
public AcceptThread() {
closeAllConnections();
/*
* if(mmServerSocket != null) { try { mmServerSocket.close(); } catch
* (IOException e) …Run Code Online (Sandbox Code Playgroud) 我在C#上运行一个C#MVC网站很长一段时间.最近,我在记录器数据中注意到了这条日志消息:
System.IO.IOException: The network name cannot be found.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileSystemEnumerableIterator`1.CommonInit()
at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
at System.IO.Directory.EnumerateFiles(String path)
at System.Web.WebPages.Deployment.WebPagesDeployment.AppRootContainsWebPagesFile(String path)
at System.Web.WebPages.Deployment.PreApplicationStartCode.OnChanged(String key, Object value, CacheItemRemovedReason reason)
at System.Web.Caching.CacheEntry.CallCacheItemRemovedCallback(CacheItemRemovedCallback callback, CacheItemRemovedReason reason)
Run Code Online (Sandbox Code Playgroud)
我也知道:
ASP.global_asax文件中抛出此异常.如您所见,此调用堆栈中没有任何内容存在于我的代码中.因此,我不知道如何重复这个或解决这个问题.
你怎么建议我解决这个问题?任何提示都会有所帮助.
我正在将Java代码库从Java 7(80)迁移到Java 8(162).(是的......我们处于技术前沿.)
在切换之后,我在一个高度并发的环境中从部署的jar加载XML资源文件时遇到了问题.正在使用try-with-resourcesSAX 访问和解析资源文件:
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
try (InputStream in = MyClass.class.getResourceAsStream("resource.xml")) {
parser.parse(in, new DefaultHandler() {...});
}
} catch (Exception ex) {
throw new RuntimeException("Error loading resource.xml", ex);
}
Run Code Online (Sandbox Code Playgroud)
如果我错了,请纠正我,但这似乎是通常建议阅读资源文件的方法.
这在IDE中运行良好,但是一旦它被部署在jar中,我经常(但不是普遍的,并且不总是使用相同的资源文件)得到一个IOException,具有以下堆栈跟踪:
Caused by: java.io.IOException: Stream closed
at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:142)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2919)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:302)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1895)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanName(XMLEntityScanner.java:728)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1279)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2784)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
at …Run Code Online (Sandbox Code Playgroud) I want to createNewFile with a path but I got an IOException. The question is, the detailed message cannot be interpreted, I can only see a bunch of question marks.
I am with Windows 10 originally in Spanish, but with Chinese language pack installed. The java language already set to en and file encoding UTF-8:
java -version
Picked up _JAVA_OPTIONS: -Duser.country=US -Duser.language=en -Dfile.encoding=UTF-8
openjdk version "11" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build …Run Code Online (Sandbox Code Playgroud) ioexception ×10
java ×7
android ×3
bluetooth ×2
exception ×2
asp.net ×1
asp.net-mvc ×1
c# ×1
filesystems ×1
https ×1
io ×1
iobluetooth ×1
jar ×1
java-11 ×1
java-8 ×1
post ×1
sockets ×1
tcp ×1
try-catch ×1