例如,假设以下片段的VB.NET代码删除目录.
Try
Dim SomeFolder="c:\somefolder"
System.IO.Directory.Delete(SomeFolder, True)
Catch ioex As System.IO.IOException
'What went wrong?
'File locked by another process?
'File not found?
'something else?
End Try
Run Code Online (Sandbox Code Playgroud)
在异常处理程序中,如果目录或其中的文件是打开的,我想让用户有机会关闭文件并重试操作,但前提是IOException是由锁定问题引起的.
问题是IOException可能由于多种原因而被抛出,例如在文件上设置了无效路径或只读标志.这些条件中的每一个都设置了不同的值.异常对象的message属性,但是对错误消息中的特定字符串进行硬编码以检测失败的具体原因只是错误.我不太相信错误字符串将与.net的未来版本一致,并且不愿意编写本地化代码以处理消息以非英语之外的其他方式返回的可能性.
必须有一种更好的方法来处理一个非常常见的异常处理问题.我错过了什么吗?
更新/澄清:感谢目前为止的答案,但我可能让我的例子有点过于通用.至少现在,我正在寻找一种方法来检测由异常处理程序中的另一个进程锁定的文件的状况.
我用以下命令写入套接字:
OutputStream socketStream = socket.getOutputStream();
socketStream.write(buf);
Run Code Online (Sandbox Code Playgroud)
但这可能会抛出IOException,所以我这样做:
try {
OutputStream socketStream = socket.getOutputStream();
socketStream.write(buf);
} catch (IOException e) {
// logging
} finally {
socket.close();
}
Run Code Online (Sandbox Code Playgroud)
还socket.close逼我去抓IOException!那么我还需要try ... catch再次使用它吗finally?
当catch IOExceptionfrom时close,这意味着套接字未关闭?那么再试close一次吗?或者该怎么办?
谢谢
来自java文档
public FileWriter(String fileName)抛出IOException
抛出:
IOException - 如果指定的文件存在但是是目录而不是常规文件,则不存在但无法创建,或者由于任何其他原因无法打开
与此
public FileOutputStream(File file,boolean append)抛出FileNotFoundException
抛出:
FileNotFoundException - 如果文件存在但是是目录而不是常规文件,则不存在但无法创建,或者由于任何其他原因无法打开
这个选择有特定原因吗?
java filenotfoundexception ioexception filewriter fileoutputstream
我正在尝试编写一个列出目录中所有非隐藏文件的方法.但是,当我添加条件时,!Files.isHidden(filePath)我的代码将无法编译,并且编译器返回以下错误:
java.lang.RuntimeException: Uncompilable source code - unreported exception
java.io.IOException; must be caught or declared to be thrown
Run Code Online (Sandbox Code Playgroud)
我试图赶上IOException,但编译器仍然拒绝编译我的代码.有什么明显的东西让我失踪吗?代码如下.
try {
Files.walk(Paths.get(root)).forEach(filePath -> {
if (Files.isRegularFile(filePath) && !Files.isHidden(filePath)) {
System.out.println(filePath);
} });
} catch(IOException ex) {
ex.printStackTrace();
} catch(Exception ex) {
ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试将https://us.mc-api.net/ 中的 API用于项目,并将其作为测试。
public static void main(String[] args){
try {
URL url = new URL("http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
System.out.println("I/O Error");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我一个 IOException 错误,但是当我在网络浏览器中打开同一个页面时,我得到
false,Unknown-Username
Run Code Online (Sandbox Code Playgroud)
这是我想从代码中得到的。我是新手,真的不知道为什么会发生这种情况或为什么会这样。编辑:堆栈跟踪
java.io.FileNotFoundException: http://us.mc-api.net/v3/uuid/193nonaxishsl/csv/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at com.theman1928.Test.Main.main(Main.java:13)
Run Code Online (Sandbox Code Playgroud) 我想运行一个代码,为此,我在我的Ubuntu机器上安装了Pycharm.现在.当我打开Pycharm应用程序并尝试打开终端时,它会抛出错误并且不会打开终端.
java.io.IOException:Exec_tty错误:未知的pycharm
附加说明:我在Vmware上运行Ubuntu 16.04.
我已经实现了 Classroom API 的 Java QuickStart,但在运行时收到错误消息“java.io.FileNotFoundException:找不到资源:/credentials.json”。我将凭证.json 文件复制到项目 res 目录,但仍然出现此错误。有什么建议么?
我有一个 Android 应用程序,它向 Flask 中的 REST API 发出 http 请求。我使用 Retrofit2 和 okhttp3 向带有 Raspbian Lite 的 Raspberry Pi 上托管的服务器发出请求。我的问题是,有时我会收到 IOException -> java.net.ProtocolException: 意外的流结束 但它有时会发生,其他时候它工作得很好。http客户端构建如下:
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("User-Agent","myUserAgent")
.header("Connection","close")
.addHeader("Accept-Encoding", "identity")
.method(original.method(),original.body())
.build();
return chain.proceed(request);
}
});
Retrofit retrofit=null;
try {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}catch (Exception e){
//Log.d
}
ApiService API_SERVICE=null; …Run Code Online (Sandbox Code Playgroud) 我认为这是基本的东西,但我不知道该怎么做.我为什么得到IOException never thrown in body of corresponding try statement
public static void main(String[] args)
{
File inputFile = null ;
try
{
inputFile = new File("records.txt") ;
}
catch (IOException e)
{
System.out.print("file not found!") ;
}
Run Code Online (Sandbox Code Playgroud) 我确实有一些无法序列化的代码。我试图在if语句中插入CanRead()和CanWrite(),这表明它们没有读写权限。我也尝试将'java.io.File.setReadable'和'java.io.File.setWriteable'插入为true,但仍然会引发错误。
代码如下:
public static void save(Object obj, String filename) throws FileNotFoundException, IOException
{
File f = new File("c:/DatoChecker/" + filename);
File dir = new File("c:/DatoChecker");
if(!dir.exists())
dir.mkdirs();
f.setReadable(true);
f.setWritable(true);
if(!f.exists())
{
f.createNewFile();
}
FileOutputStream op = null;
ObjectOutputStream objectStream = null;
op = new FileOutputStream(f);
objectStream = new ObjectOutputStream(op);
objectStream.writeObject(obj);
objectStream.close();
}
public static Object fetch(String filename) throws FileNotFoundException, IOException, ClassNotFoundException
{
File f = new File("c:/DatoChecker" + filename);
File dir = new File("c:/DatoChecker");
if(!dir.exists())
dir.mkdirs();
f.setReadable(true);
f.setWritable(true);
if(!f.exists())
{ …Run Code Online (Sandbox Code Playgroud) ioexception ×10
java ×6
exception ×2
.net ×1
android ×1
credentials ×1
file-io ×1
filestream ×1
filewriter ×1
java-8 ×1
okhttp ×1
pycharm ×1
retrofit2 ×1
sockets ×1