我可以将任何InputStream写入FileChannel吗?
我正在使用java.nio.channels.FileChannel打开文件并将其锁定,然后将InputStream写入输出文件.InputStream可以由另一个文件,URL,套接字或任何东西打开.我写了以下代码:
FileOutputStream outputStream = new FileOutputStream(outputFile);
FileChannel outputChannel = outputStream.getChannel();
FileLock lock = outputChannel.lock();
try {
outputChannel.transferFrom(???);
} finally {
lock.release();
outputChannel.close();
outputStream.close();
}
Run Code Online (Sandbox Code Playgroud)
但是,outputChannel.transferFrom(...)的第一个参数请求ReadableByteChannel对象.由于我使用InputStream作为输入,因此它没有inputStream.getChannel()方法来创建所需的通道.
有没有办法从InputStream获取ReadableByteChannel?
您如何查询正在运行的进程以找出他们锁定的文件夹或文件?(即你去弹出一个驱动器,你被告知它不能被弹出,因为它正在使用中)
我想要获得"现成的"下载,或者编写.NET 3.5应用程序来执行此操作(主要是Windows问题).
我需要能够用Java模仿'tail -f'.我正在尝试读取一个日志文件,因为它是由另一个进程写的,但是当我打开文件来读取它时,它会锁定文件而另一个进程无法再写入它.任何帮助将不胜感激!
这是我目前使用的代码:
public void read(){
Scanner fp = null;
try{
fp = new Scanner(new FileReader(this.filename));
fp.useDelimiter("\n");
}catch(java.io.FileNotFoundException e){
System.out.println("java.io.FileNotFoundException e");
}
while(true){
if(fp.hasNext()){
this.parse(fp.next());
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我有时需要从正在写入的文件中读取并因此被锁定.正如我从其他问题中理解的那样,我应该捕获IOException并重试直到我能够阅读.
但我的问题是我如何确定该文件已被锁定,并且它不是另一个IOExcetpion发生的.
在WPF DocumentViewer中显示XPS文件并关闭DocumentViewer实例后,XPS文件被锁定,我无法删除它.我需要释放XPS文件上的锁,以便我可以删除它,编写另一个具有相同名称的文件,并可选择在新的DocumentViewer实例中显示新的XPS文件.我需要在同一个应用程序实例中执行此操作 - 无需关闭应用程序(这是打印预览方案).
换句话说,如何在不在"File.Delete(tempXpsFile);"处抛出异常的情况下运行以下代码?声明?
var tempXpsFile = @"c:\path\to\Temporary.xps";
var previewWindow = new Window();
var docViewer = new DocumentViewer();
previewWindow.Content = docViewer;
GenerateXpsFile(tempXpsFile);
var xpsDocument = new XpsDocument(tempXpsFile);
previewWindow.ShowDialog();
File.Delete(tempXpsFile); //this will throw an exception due to a file lock on tempXpsFile
GenerateXpsFile(tempXpsFile); //assume this generates a different file
//otherwise the scenario doesn't make sense as we could just skip the above delete
//and this statement and re-use the same file
previewWindow = new Window();
docViewer = new DocumentViewer();
previewWindow.Content …Run Code Online (Sandbox Code Playgroud) 我有这个代码保存pdf文件.
FileStream fs = new FileStream(SaveLocation, FileMode.Create);
fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
fs.Flush();
fs.Close();
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是,有时它不会立即释放锁定,并导致文件锁定异常,并且在此次运行后运行函数.
有没有理想的方法在fs.Close()之后立即释放文件锁
我注意到,java.io和java.nio随机访问文件的实施对于略有不同如何FileLocks进行处理.
看起来好像(在Windows上)java.io为您提供强制文件锁定,并java.nio在分别请求时为您提供建议文件锁定.强制文件锁定意味着锁定适用于所有进程,并且建议适用于遵循相同锁定协议的良好行为进程.
如果我运行以下示例,我可以*.nio手动删除该文件,而*.io文件拒绝删除.
import java.io.*;
import java.lang.management.ManagementFactory;
import java.nio.*;
import java.nio.channels.*;
import java.nio.file.*;
public class NioIoLock {
public static void main(String[] args) throws IOException, InterruptedException {
String workDir = System.getProperty("user.dir");
FileChannel channelIo, channelNio;
FileLock lockIo, lockNio;
// use io
{
String fileName = workDir
+ File.separator
+ ManagementFactory.getRuntimeMXBean().getName()
+ ".io";
File lockFile = new File(fileName);
lockFile.deleteOnExit();
RandomAccessFile file = new RandomAccessFile(lockFile, "rw");
channelIo = file.getChannel();
lockIo …Run Code Online (Sandbox Code Playgroud) 我正在使用Python模块filelock。
在 Windows 上,当释放锁时,支持该锁的文件将被删除。
在 UNIX 上,即使锁被释放后,锁文件仍然存在于文件系统上。
操作系统之间存在差异是否有原因?如果没有理由不同,那么以下哪种行为更正确?
首先,这些是我的意图:
点1-3完美地工作.当我尝试移动数据库时,问题就开始了.我得到一个错误说明:
'The process cannot access the file because it is being used by another process.'
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
首先,我创建一个上下文.我必须在几种方法中使用它,我不想在每次需要时创建它.所以我将它存储为成员.
_sqliteContext = new SqlLiteContext(sqliteContextName);
Run Code Online (Sandbox Code Playgroud)
然后我想访问一个名为的表sync并获取其最新条目.
var sync = _sqliteContext.Syncs.OrderByDescending(s => s.Date);
_lastSync = sync.Any() ? sync.First().Date : new DateTime(0);
Run Code Online (Sandbox Code Playgroud)
而已.然后我关闭上下文.
_sqliteContext.Dispose();
Run Code Online (Sandbox Code Playgroud)
并尝试移动文件.
File.Move(sqliteUploadLocation, sqliteDownloadLocation);
Run Code Online (Sandbox Code Playgroud)
这是我得到例外的地方.
当我用插入替换选择时,如下所示:
var sync = new Sync { Id = Guid.NewGuid().ToString(), Date = DateTime.Now };
_sqliteContext.Syncs.Add(sync);
_sqliteContext.SaveChanges();
Run Code Online (Sandbox Code Playgroud)
这有效,我可以移动数据库.任何想法为什么我的选择不释放锁定?
更新
// Start synchronisation.
new SyncManager(mssqlString, sqliteUploadLocation).Start();
// Move file from upload to download …Run Code Online (Sandbox Code Playgroud) 每当我从fileURI开始 Camel 路由时,我都会看到 Camel 获得了文件的“锁定”。例如,如果文件名为myinput.xml,则 Camel 在其上创建一个名为.lock 的“锁定文件”,位于同一目录中myinput.xml.camelLock。
filelock ×10
java ×4
c# ×3
java-io ×2
.net ×1
.net-3.5 ×1
apache-camel ×1
file-io ×1
file-move ×1
filechannel ×1
filestream ×1
filesystems ×1
inputstream ×1
ioexception ×1
linux ×1
nio ×1
python ×1
sqlite ×1
wpf ×1
xpsdocument ×1