是否有使用JavaScript完成的本地文件操作?我正在寻找一种可以在没有安装空间的情况下完成的解决方案,例如需要AIR.
具体来说,我想从文件中读取内容并将这些内容写入另一个文件.在这一点上,我并不担心获得权限,只是假设我已经拥有这些文件的完全权限.
在Windows Server 2012 R2系统上,Kotlin程序用于FileChannel.tryLock()保存文件的独占锁定,如下所示:
val fileRw = RandomAccessFile(file, "rw")
fileRw.channel.tryLock()
Run Code Online (Sandbox Code Playgroud)
有了这个锁,我无法打开文件:
以编程方式使用C#,对于任何值FileShare:
using (var fileStream = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var textReader = new StreamReader(fileStream))
{
textReader.ReadToEnd();
}
Run Code Online (Sandbox Code Playgroud)从命令行,type命令:
C:\some-directory>type file.txt
The process cannot access the file because another process has locked a portion of the file.
Run Code Online (Sandbox Code Playgroud)Internet Explorer(是的,我很绝望)
我可以用记事本打开它.
记者怎么能打开一个锁定的文件,没有别的可以吗?
想象一下我想创建(或覆盖)以下文件: - C:\Temp\Bar\Foo\Test.txt
使用File.Create(..)方法,这可以做到这一点.
但是,如果我没有以下任何一个文件夹(从该示例路径,上面)
然后我得到一个DirectoryNotFoundException抛出.
那么.. 给定一个路径,我们如何递归创建创建文件所需的所有文件夹..对于该路径? 如果存在Temp或Bar文件夹,但Foo不存在......那么也会创建.
为简单起见,我们假设没有安全问题 - 所有权限都很好,等等.
基本上,我想在实际尝试打开文件之前检查是否有权打开文件; 除非必须,否则我不想使用try/catch进行此检查.有没有我可以检查的文件访问属性?
当多个文件放入监视目录时,我遇到了FileSystemWatcher的问题.我想在文件放入目录后立即解析它.通常,第一个文件解析正常,但向目录添加第二个文件会导致访问问题.偶尔,第一个文件甚至不会解析.只有一个应用程序正在运行并正在查看此目录.最终,此进程将在多台计算机上运行,并且它们将监视共享目录,但只有一台服务器可以解析每个文件,因为数据已导入数据库且没有主键.
这是FileSystemWatcher代码:
public void Run() {
FileSystemWatcher watcher = new FileSystemWatcher("C:\\temp");
watcher.NotifyFilter = NotifyFilters.FileName;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
Run Code Online (Sandbox Code Playgroud)
然后是解析文件的方法:
private void OnChanged(object source, FileSystemEventArgs e) {
string line = null;
try {
using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.None)) {
using (StreamReader sr = new StreamReader(fs)) {
while (sr.EndOfStream == false) {
line = sr.ReadLine();
//parse the line and insert into the database
}
}
}
}
catch (IOException ioe) …Run Code Online (Sandbox Code Playgroud) 是否可以同时复制另一个进程正在使用的文件?
我问,因为当我尝试使用以下代码复制文件时,会引发异常:
System.IO.File.Copy(s, destFile, true);
Run Code Online (Sandbox Code Playgroud)
提出的例外是:
该进程无法访问文件'D:\ temp\1000000045.zip',因为它正由另一个进程使用.
我不想创建新文件,我只想复制或删除它.这可能吗?
我正在使用PowerShell,运行包含以下行的脚本(从我的控制台):
$inpath = "C:\users\xxxxx\path\foo\bar"
Run Code Online (Sandbox Code Playgroud)
我一直收到这个错误:
Get-Content : Access to the path 'C:\users\xxxxx\path\foo\bar' is denied.
At C:\users\xxxxx\path\foo\testscript.ps1:53 char:12
+ Get-Content <<<< $txtfile | Get-WordCount -Exclude (Get-Content c:\temp\exclude.txt) | select -First 15
+ CategoryInfo : PermissionDenied: (C:\users\xxxxx\path\foo\bar:String) [Get-Content], UnauthorizedAcc
essException
+ FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand
Run Code Online (Sandbox Code Playgroud)
脚本和目标文件都位于我的本地驱动器上.我可以在资源管理器中访问这些文件,使用NotePad查看/编辑/保存它们,并且没有设置任何权限限制.当我在命令行上时,我可以get-content在路径中的文件上成功运行cmdlet.我可以更改目录PS C:> cd C:\users\xxxxx\path\foo\bar并成功列出其中的内容.更有趣的是,我可以复制脚本中出错的行,并且不会在命令行上收到错误.
PS C:\users\xxxxx\path\foo> $inpath = "C:\users\xxxxx\path\foo\bar"
PS C:\users\xxxxx\path\foo>
Run Code Online (Sandbox Code Playgroud)
这让我怀疑"权限被拒绝"错误实际上是别的东西,或者说有些模糊,以至于我不知道如何继续进行故障排除.PS是否可能拥有与运行它的用户不同的权限?有没有人见过这种行为,你是如何解决这个问题的?我确信有一个我不知道的简单解决方案.
我想做一件简单的事; 从互联网上读取图像,将其保存到iphone上的应用程序文档目录中,然后从该文件中读回来,以便我以后可以用它做其他事情.编写文件工作正常,但当我尝试读回来时,我在GDB中得到一个EXC_BAD_ACCESS错误,我不知道如何解决.这是我的代码基本上是这样的:
-(UIImage *) downloadImageToFile {
NSURL * url = [[NSURL alloc] initWithString: self.urlField.text];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
[paths release]
NSString * path = [documentsDirectory stringByAppendingString:@"/testimg.png"];
NSData * data = [[NSData alloc] initWithContentsOfURL:url];
[data writeToFile:path atomically:YES];
return [[UIImage alloc] initWithContentsOfFile:path];
}
Run Code Online (Sandbox Code Playgroud)
当我尝试从文件初始化UIImage时,代码在return语句中失败.有任何想法吗?
编辑:忽略添加最初是代码中的问题的版本.
我是OSGi的新手,并创建了一个OSGi-bundle,我在Apache Felix OSGi-container中运行.捆绑包中包含一个文件资源,我需要将其作为传递给方法java.io.File.要实例化File-object,需要"file"-scheme中的URI或字符串作为字符串.如何以干净的方式检索任何这些?
我尝试使用
context.getBundle().getResource("/myfile")(org.osgi.framework.BundleContext返回类型的上下文)返回URI bundle://6.0:0/myfile.但是这个URI不能使用File(URI uri)构造函数转换为File-instance,因为它具有"bundle"-scheme.
可以尝试构建一个知道工作目录的位置路径并利用我的bundle的bundleId,但我怀疑这是最好的做法.
有任何想法吗?
有没有办法可以检查文件是否正在使用或者是否被其他进程打开而不仅仅是尝试打开它并捕获异常?有没有服务方法来测试这样的事情?
file-access ×10
c# ×5
.net ×4
apache-felix ×1
cocoa-touch ×1
exception ×1
file ×1
file-copying ×1
file-locking ×1
iphone ×1
javascript ×1
locking ×1
notepad ×1
objective-c ×1
osgi ×1
powershell ×1
windows ×1