我使用C#.NET,vs 2008,.net 3.5
对我来说,很难,但我需要C#中的示例代码:
例如,在我的问题中.
我尝试删除文件,我得到"进程无法访问文件'XYZ',因为它正被另一个进程使用." 例外.
try
{
File.Delete(infoFichero.Ruta);
}
catch (IOException ex)
{
// ex.Message == "The process cannot access the file 'XYZ' because it is being used by another process."
}
Run Code Online (Sandbox Code Playgroud)
但如果.NET是西班牙语,我会得到"El proceso no puede obtener acceso alarchivo'00000004.PDF'polqueestásiendoutilizado en otro proceso"的消息.
System.IO.IOException: El proceso no puede obtener acceso al archivo '00000004.PDF' porque está siendo utilizado en otro proceso.
en System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
en System.IO.FileInfo.Delete()
Run Code Online (Sandbox Code Playgroud)
我需要一个错误代码用于该异常.在Trace中,我见过System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)
如何获取IOException的错误代码"进程无法访问文件'XYZ',因为它正由另一个进程使用."
我正在尝试创建一个目录并将文件复制到它.我实现的代码及其输出如下.问题似乎是不言自明的,但我会明确地告诉那些无法分辨的人.
无论我做什么,我似乎无法创建复制文件所需的目标文件.
get是要复制的文件,是要复制到dest的目录.ERR>为清楚起见,添加了行号和" ".我已经注释了我尝试过的其他文件创建方法,但它们都失败了.
115: private void copyTo(File get, File dest)
116: {
117: try
118: {
119: dest = new File((dest.getPath().endsWith(File.separator) ? dest.getPath() : dest.getPath() + File.separator) + get.getName());
120:
121: java.io.FileInputStream fis = new java.io.FileInputStream(get);
122: if (dest.exists())
123: while(!dest.delete());
124: dest.mkdir();
125:// dest.createNewFile();
126:// java.io.FileWriter w = new java.io.FileWriter(dest);
127:// w.write("");
128: System.out.println("Writing \"" + get + "\" to \"" + dest + "\"");
129:ERR> java.io.FileOutputStream fos = new java.io.FileOutputStream(dest); …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码在我的wpf应用中显示一些图像:
<Image Source="{Binding Path=TemplateImagePath, Mode=TwoWay}" Grid.Row="3" Grid.Column="2" Width="400" Height="200"/>
Run Code Online (Sandbox Code Playgroud)
并通过浏览某个目录在代码隐藏的构造函数中设置它的绑定属性,下面是代码:
DirectoryInfo Dir = new DirectoryInfo(@"D:/Template");
if (Dir.Exists)
{
if (Dir.GetFiles().Count() > 0)
{
foreach (FileInfo item in Dir.GetFiles())
{
TemplateImagePath = item.FullName;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果用户上传了一些其他图像,那么我需要删除这个旧图像,这是我按照以下方式进行并将图像绑定设置为null:
DirectoryInfo Dir = new DirectoryInfo(@"D:/Template");
if (Dir.Exists)
{
if (Dir.GetFiles().Count() > 0)
{
foreach (FileInfo item in Dir.GetFiles())
{
TemplateImagePath= null;
File.Delete(item.FullName);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到的异常是无法删除某些其他进程使用的文件.我该如何删除它?
我正在 SD 卡上创建/保存文件。这是代码:
File sdDir = Environment.getExternalStorageDirectory();
public void btnsave_clicked(View v) throws FileNotFoundException, IOException{
File f;
f=new File(sdDir, "deposit.dma");
if(!f.exists())
f.createNewFile();
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我单击按钮时,它会抛出“权限被拒绝”。不要问我添加使用权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
我已经在 Manifest 中添加了这个。
我该怎么办?LogCat 信息:
10-19 20:25:14.681: E/AndroidRuntime(29016): FATAL EXCEPTION: main
10-19 20:25:14.681: E/AndroidRuntime(29016): java.lang.IllegalStateException: Could not execute method of the activity
10-19 20:25:14.681: E/AndroidRuntime(29016): at android.view.View$1.onClick(View.java:2168)
10-19 20:25:14.681: E/AndroidRuntime(29016): at android.view.View.performClick(View.java:2552)
10-19 20:25:14.681: E/AndroidRuntime(29016): at android.view.View$PerformClick.run(View.java:9229)
10-19 20:25:14.681: E/AndroidRuntime(29016): at android.os.Handler.handleCallback(Handler.java:587)
10-19 20:25:14.681: E/AndroidRuntime(29016): at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 20:25:14.681: E/AndroidRuntime(29016): at android.os.Looper.loop(Looper.java:130)
10-19 20:25:14.681: …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚为什么我的代码会出现上述错误。每次调用该方法时,我都会创建 FileWriter 和 BufferedWriter 的新实例,但显然流已经关闭。
public static void addSpawn(Location spawn)
{
File spawns = new File("spawns.dat");
FileWriter write = null;
BufferedWriter out = null;
try
{
write = new FileWriter(spawns, true);
out = new BufferedWriter(write);
out.write(locToStr(spawn));
out.newLine();
}
catch(Exception e)
{
System.out.println("Error writing spawn file: " + e.getMessage());
}
finally
{
if(write != null)
{
try
{
write.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
if(out != null)
{
try
{
out.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
} …Run Code Online (Sandbox Code Playgroud) 我在arff文件中有一些数据,我无法转换它.我不知道出了什么问题.
@RELATION relationName
@ATTRIBUTE IP STRING
@ATTRIBUTE data STRING
@ATTRIBUTE adress STRING
@ATTRIBUTE error_code STRING
@ATTRIBUTE dunno STRING
@ATTRIBUTE class STRING
@DATA
202.32.92.47,01:Jun:1995:00:00:59,/~scottp/publish.html,200,271
ix-or7-27.ix.netcom.com,112,/~ladd/ostriches.html,200,20
...
Run Code Online (Sandbox Code Playgroud)
我得到的错误如下:无法确定为arff(原因:java.io.IOException:过早的行结束,读取令牌[EOL],第11行.
第11行在第一行之后.我也试图将它转换为csv,标题如下:
a b c d e
Run Code Online (Sandbox Code Playgroud)
但后来我收到一个错误:错误的标题......
这有什么好处/垮台吗?
通常,从流中读取时会抛出异常:
try {
inputStream.read();
}catch(IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
但是当使用a时Scanner,你不会被迫处理异常.相反,如果抛出一个,你会使用Scanner#ioException().
我知道Scanner不是一个流,而不是一个解析数据的tokenizer,如果需要,但为什么它处理它的异常不像其他涉及IO的行为?我什么时候应该以这种方式处理异常?
我制作了一个允许加入许多客户端的服务器。
但是我有一个问题。
我添加了“启动/停止”按钮,该按钮应该启动/停止服务器。但代码并不像我想要的那样工作:连接未关闭,代码转到 IOException“这是问题”(在 ServerLogic 部分)。
此外,客户端仍然可以与服务器联系。
服务器逻辑
public class ServerLogic
{
private static ServerSocket m_sSocket;
private static Set<ServerSubscriber> m_subscriberList = new HashSet<ServerSubscriber>();
private static boolean m_isServerRun = false;
private static class ServerLogicHolder
{
static final ServerLogic INSTANCE = new ServerLogic();
}
private ServerLogic()
{}
public static ServerLogic getServerLogic()
{
return ServerLogicHolder.INSTANCE;
}
/**
* It starts listening of incoming connections from the clients.
*
* @param port
*/
public void startListening(int port)
{
try
{
if (!m_isServerRun)
{
m_sSocket …Run Code Online (Sandbox Code Playgroud) 什么是 IO 异常 (java.io.IOException) 以及导致它们的原因?
可以使用哪些方法/工具来确定原因,以便阻止异常导致提前终止?这是什么意思,我能做些什么来解决这个异常?
我正在为一段代码编写测试,其中包含一个IOException catch,我试图覆盖它.try/catch看起来像这样:
try {
oos = new ObjectOutputStream(new FileOutputStream(cacheFileName));
} catch (IOException e) {
LOGGER.error("Bad news!", e);
} finally {
Run Code Online (Sandbox Code Playgroud)
最简单的方法似乎是让FileOutputStream抛出一个FileNotFoundException,但也许我会以错误的方式解决这个问题.
有人在那里有任何提示吗?
ioexception ×10
java ×8
file ×2
android ×1
arff ×1
c# ×1
c#-4.0 ×1
creation ×1
delete-file ×1
error-code ×1
file-io ×1
filewriter ×1
junit ×1
permissions ×1
server ×1
sockets ×1
testing ×1
weka ×1
wpf ×1