小编mat*_*vei的帖子

在繁忙的环境中可靠的文件保存(File.Replace)

我正在研究定期需要将数据保存到磁盘的服务器软件.我需要确保旧文件被覆盖,并且在意外情况下文件不会被破坏(例如,仅部分覆盖).

我采用了以下模式:

string tempFileName = Path.GetTempFileName();
// ...write out the data to temporary file...
MoveOrReplaceFile(tempFileName, fileName);
Run Code Online (Sandbox Code Playgroud)

... MoveOrReplaceFile所在的位置:

public static void MoveOrReplaceFile( string source, string destination ) {
    if (source == null) throw new ArgumentNullException("source");
    if (destination == null) throw new ArgumentNullException("destination");
    if (File.Exists(destination)) {
        // File.Replace does not work across volumes
        if (Path.GetPathRoot(Path.GetFullPath(source)) == Path.GetPathRoot(Path.GetFullPath(destination))) {
            File.Replace(source, destination, null, true);
        } else {
            File.Copy(source, destination, true);
        }
    } else {
        File.Move(source, destination);
    }
}
Run Code Online (Sandbox Code Playgroud)

只要服务器具有对文件的独占访问权限,这就可以正常工作.但是,File.Replace似乎对外部文件访问非常敏感.每当我的软件在具有防病毒或实时备份系统的系统上运行时,随机的File.Replace错误就会弹出:

System.IO.IOException:无法删除要替换的文件.

以下是我已消除的一些可能原因:

  • 未发布的文件句柄:using()确保尽快释放所有文件句柄. …

.net c# windows filesystems

25
推荐指数
1
解决办法
6959
查看次数

如何在JFileChooser中导航到网络主机?

问题

我有一个JFileChooser,我需要以编程方式将其currentDirectory设置为包含多个SMB共享的网络主机(例如\\blah).从技术上讲,这不是一个"目录",而是一个表示可用共享列表的shell文件夹.

  • JFileChooser在导航到特定共享(例如\\blah\someShare)时没有问题,但是无法处理主机"目录"本身(例如\\blah).

  • 用户可以通过"网络"shell文件夹,或通过查找特定共享并导航到其父目录,导航到JFileChooser内的此类"目录".调试显示引擎盖下此目录表示为a Win32ShellFolder2.到目前为止,我以编程方式设置currentDirectory的所有尝试都失败了.

  • new File("\\\\blah") 可以创建,但从Java的角度来看实际上并不存在.

解决方案尝试失败

  • chooser.setCurrentDirectory(new File("\\\\blah"));

    失败,因为JFileChooser检查给定目录是否存在,并new File("\\\\blah").exists()返回false.

  • File dir = new File("\\\\blah").getCanonicalFile();

    失败,但有异常:

      java.io.IOException: Invalid argument
      at java.io.WinNTFileSystem.canonicalize0(Native Method)
      at java.io.WinNTFileSystem.canonicalize(WinNTFileSystem.java:428)
      at java.io.File.getCanonicalPath(File.java:618)
      at java.io.File.getCanonicalFile(File.java:643)
    
    Run Code Online (Sandbox Code Playgroud)
  • File dir = ShellFolder.getShellFolder(new File("\\\\blah"));

    失败,但有异常:

      java.io.FileNotFoundException
      at sun.awt.shell.ShellFolder.getShellFolder(ShellFolder.java:247)
    
    Run Code Online (Sandbox Code Playgroud)
  • File dir = new Win32ShellFolderManager2().createShellFolder(new File("\\\\blah"));

    失败,但有异常:

      java.io.FileNotFoundException: File \\blah not found
      at sun.awt.shell.Win32ShellFolderManager2.createShellFolder(Win32ShellFolderManager2.java:80)
      at sun.awt.shell.Win32ShellFolderManager2.createShellFolder(Win32ShellFolderManager2.java:64)
    
    Run Code Online (Sandbox Code Playgroud)
  • Path dir = Paths.get("\\\\blah");

    失败,但有异常:

    java.nio.file.InvalidPathException: UNC path is missing sharename: \\blah
    at …
    Run Code Online (Sandbox Code Playgroud)

java windows filesystems smb jfilechooser

11
推荐指数
2
解决办法
1566
查看次数

Geometry.getarea()和Polygon.getarea()有哪些单位?

因此,我正在处理一组图像,其中角落是地图上的长度.我正在创建多边形以检查给定图像是否重叠.我需要知道与Polygon和几何体的.getarea()方法相关的单位.我正在使用以下对象来创建我的多边形和几何体

http://tsusiatsoftware.net/jts/javadoc/com/vividsolutions/jts/geom/Geometry.html#getArea() http://www.vividsolutions.com/jts/javadoc/com/vividsolutions/jts/geom/Polygon html的

当使用上述两个对象时,我得到一个数字,但是我没有发现与该数字相关的单位是什么的指示.那我们说米,公里,英里?

我正在使用的cordinates样本是+30.658739 -086.345670,+ 30.659997 -086.34002,+ 30.664041 -086.345082,+ 30.662783 -086.342750我正在寻找这4点之间的区域.
我从.getArea()返回的值是1.31039680000139E-5这些点是真正接近所以我想它的米是1310.4米

java jts

3
推荐指数
1
解决办法
3880
查看次数

标签 统计

filesystems ×2

java ×2

windows ×2

.net ×1

c# ×1

jfilechooser ×1

jts ×1

smb ×1