在构造函数中引用现有文件时 Windows Java 文件锁定?

发布于 2024-09-09 02:25:44 字数 373 浏览 0 评论 0原文

假设我在 java 中对保持打开状态的进程执行以下操作:

import java.io.File;
import java.util.Date;
public class LogHolder {
    public static void main(String[] args) {
        File file1 = new File("myLogFile.log");        
        while (true) {
            System.out.println("Running " + new Date());
        }
    }
}

我是否以其他 Windows 进程无法写入日志文件的方式锁定了该文件?

Suppose I do the following in java for a process that stays open:

import java.io.File;
import java.util.Date;
public class LogHolder {
    public static void main(String[] args) {
        File file1 = new File("myLogFile.log");        
        while (true) {
            System.out.println("Running " + new Date());
        }
    }
}

Have I locked this file in a way that other windows processes can't write to the log file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梓梦 2024-09-16 02:25:44

这可能对您有帮助: FileLock。

This might help you: FileLock.

紅太極 2024-09-16 02:25:44

不,您还没有锁定文件。以下是 Java 文档总结 java.io.File 用途的方式:

文件和目录路径名的抽象表示

(换句话说,new File() 甚至不打开文件。)

您可以在这里找到其余内容:http://java.sun.com/javase/6/docs/api/java/io/文件.html

No, you haven't locked the file. Here's how the Java documentation summarizes the purpose of java.io.File:

An abstract representation of file and directory pathnames

(In other words, new File() doesn't even open the file.)

You can find the rest here: http://java.sun.com/javase/6/docs/api/java/io/File.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文