java.io.IOException似乎是最常见的例外类型,巧合的是,它似乎也是最模糊的.throws IOException每当用套接字,文件和c编写时,我都会看到它.然而,我从来没有真正对我开枪,所以我想知道是什么原本应该解雇这个例外.该文档对解释正在发生的事情没有多大帮助:
表示发生某种I/O异常的信号.此类是由失败或中断的I/O操作产生的一般异常类.
有人可以解释一些抛出IOException的实例,或者它应该如何被利用.
我有以下简单的代码:
import java.io.*;
class IO {
public static void main(String[] args) {
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
System.out.println(userInput);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误消息:
----------
1. ERROR in io.java (at line 10)
while ((userInput = stdIn.readLine()) != null) {
^^^^^^^^^^^^^^^^
Unhandled exception type IOException
----------
1 problem (1 error)roman@roman-laptop:~/work/java$ mcedit io.java
Run Code Online (Sandbox Code Playgroud)
有人有什么想法吗?我只是想简化总和网站上给出的代码(这里).我过度简化了吗?
How do I wait for the file to be free so that ss.Save() can overwrite it with a new one? If I run this twice close together(ish), I get a generic GDI+ error.
///<summary>
/// Grabs a screen shot of the App and saves it to the C drive in jpg
///</summary>
private static String GetDesktopImage(DevExpress.XtraEditors.XtraForm whichForm)
{
Rectangle bounds = whichForm.Bounds;
// This solves my problem but creates a clutter issue
// var timeStamp = DateTime.Now.ToString("ddd-MMM-dd-yyyy-hh-mm-ss");
// var fileName …Run Code Online (Sandbox Code Playgroud) 这段代码以前有用.然后,也许我在某处改变了某些东西(或者如果我知道Android正确,则更新会在媒体播放器中引入错误).它停止在某些设备上工作!特别是我的Nexus S(2.3.6).
该test.m4a应用程序下载了该文件(17 775 201字节).为了验证其完整性,我将其复制到SD并在我的PC上播放.没问题!二进制 - 将它与原始文件进行比较,它匹配100%.
try {
_mediaPlayer = new MediaPlayer();
_mediaPlayer.setOnCompletionListener(this);
_mediaPlayer.setOnPreparedListener(this);
_mediaPlayer.setOnSeekCompleteListener(this);
_mediaPlayer.setOnBufferingUpdateListener(this);
_mediaPlayer.setOnInfoListener(this);
_mediaPlayer.setOnErrorListener(this);
// I even tried reading the file from SD card. Same error...
File file = new File("/data/data/com.mycompany.myapp/files/MediaCache/Test.m4a");
// file.isFile() == true
// file.length() == expected value!
FileInputStream is = new FileInputStream(file);
_mediaPlayer.setDataSource(is.getFD()); // Throws!
// If I use the filename as parameter, it throws later when preparing the media player...
_mediaPlayer.prepareAsync();
}
catch (Exception e) {
// …Run Code Online (Sandbox Code Playgroud) 在这里提供了一些答案并阅读了一些注释后,似乎实际上IOException永远不会在文件I/O上关闭.
是否有任何情况下,在Stream/Reader/Writer上调用close实际上会抛出IOException?
如果实际抛出异常,应如何处理?
java.io.InputStream.close()声明该方法抛出一个IOException.在什么情况下会抛出这样的例外?
编辑:是的我已经阅读了javadoc.任何人都可以比"发生I/O错误时"更具体吗?关闭时会发生什么 I/O错误InputStream?
我正在尝试在Linux上的Jetty 7.0.1中运行的Java webapp中调试文件描述符泄漏.
由于打开的文件过多而导致请求开始失败,应用程序已经愉快地运行了一个月左右,并且必须重新启动Jetty.
java.io.IOException: Cannot run program [external program]: java.io.IOException: error=24, Too many open files
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at org.apache.commons.exec.launcher.Java13CommandLauncher.exec(Java13CommandLauncher.java:58)
at org.apache.commons.exec.DefaultExecutor.launch(DefaultExecutor.java:246)
Run Code Online (Sandbox Code Playgroud)
起初我认为问题在于启动外部程序的代码,但它使用的是commons-exec,我没有看到它有什么问题:
CommandLine command = new CommandLine("/path/to/command")
.addArgument("...");
ByteArrayOutputStream errorBuffer = new ByteArrayOutputStream();
Executor executor = new DefaultExecutor();
executor.setWatchdog(new ExecuteWatchdog(PROCESS_TIMEOUT));
executor.setStreamHandler(new PumpStreamHandler(null, errorBuffer));
try {
executor.execute(command);
} catch (ExecuteException executeException) {
if (executeException.getExitValue() == EXIT_CODE_TIMEOUT) {
throw new MyCommandException("timeout");
} else {
throw new MyCommandException(errorBuffer.toString("UTF-8"));
}
}
Run Code Online (Sandbox Code Playgroud)
在服务器上列出打开的文件我可以看到大量的FIFO:
# lsof -u jetty
... …Run Code Online (Sandbox Code Playgroud) 我很困惑它自己的android问题或Java问题.
当我调试一个与蓝牙一起工作的android应用程序时,流程停在了一个catch块上IOException,后来我发现异常e为null ....当我试图从InputStream读取时抛出它
是的,它不是一个NullPointerException但是其他类型的异常是空的 - 更好的说抛出未初始化.
可能吗?在哪种情况下可以抛出这种单元化异常?

我一直在研究这段似乎微不足道的小代码,但我仍然无法确定问题出在哪里.我的功能很简单.打开文件,复制其内容,替换内部的字符串并将其复制回原始文件(然后在文本文件中进行简单的搜索和替换).我真的不知道怎么做,因为我在原始文件中添加了行,所以我只创建了一个文件副本,(file.temp)副本也备份(file.temp)然后删除原始文件(文件)并将file.temp复制到文件.我在删除文件时遇到异常.以下是示例代码:
private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
{
Boolean result = false;
FileStream fs = new FileStream(file.FullName + ".tmp", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
StreamReader streamreader = file.OpenText();
String originalPath = file.FullName;
string input = streamreader.ReadToEnd();
Console.WriteLine("input : {0}", input);
String tempString = input.Replace(extractedMethod, modifiedMethod);
Console.WriteLine("replaced String {0}", tempString);
try
{
sw.Write(tempString);
sw.Flush();
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
streamreader.Close();
streamreader.Dispose();
File.Copy(originalPath, originalPath + ".old", true);
FileInfo newFile = new FileInfo(originalPath + ".tmp");
File.Delete(originalPath);
File.Copy(fs., originalPath, true); …Run Code Online (Sandbox Code Playgroud) filecontent.java:15:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出
演出文件(); ^ filecontent.java:78:未报告的异常java.io.IOException; 必须被抓住或宣布被抛出
演出文件(); ^
我已经抛出了java.io.IOException,但它仍然显示了这些错误.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class filecontent extends Frame implements ActionListener
{
TextField t[] = new TextField[4];
TextArea ta[] = new TextArea[4];
Button submit;
Panel p1;
filecontent()
{
setGUI();
setRegister();
showfile();
setTitle("FileData");
setVisible(true);
setSize(300, 300);
/* addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
*/
}
void setGUI()
{
p1 = new Panel();
p1.setLayout(new GridLayout(5, 4, 10, 10));
for(int i=0; i<4; i++)
{
t[i] = …Run Code Online (Sandbox Code Playgroud)