Java - 如何同时读写文件? [英] Java - How to read and write a file simultaneously?

查看:263
本文介绍了Java - 如何同时读写文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java中是否有任何方法可以读取文件的内容,在关闭之前由另一个处理程序更新?

Is there any way in java to read a file's content, which is being updated by another handler before closing it?

推荐答案

这取决于操作系统。

传统上,POSIX-y操作系统(Linux,Solaris,...)对两个文件都打开完全没有问题读取和写入,甚至是通过单独的进程(它们甚至支持在读取和/或写入文件时删除文件)。

Traditionally, POSIX-y operating systems (Linux, Solaris, ...) have absolutely no problem with having a file open for both reading and writing, even by separate processes (they even support deleting a file while it's being read from and/or written to).

在Windows中,更常见的方法是单独打开文件(与普通人相反,Windows 支持非独占文件访问,它很少被应用程序使用)。

In Windows, the more common approach is to open files exclusively (contrary to common believe, Windows does support non-exclusive file access, it's just rarely used by applications).

Java无法指定您想要访问文件的方式 * ,因此使用平台默认值(Linux / Solaris上的共享访问权限,Windows上的独占访问权限)。

Java has no way* of specifying what way you want to access a file, so the platform default is used (shared access on Linux/Solaris, exclusive access on Windows).

* 对于Java 7中的NIO和新NIO,这可能是错误的,但我不是NIO的大专家。

* This might be wrong for NIO and new NIO in Java 7, but I'm not a big NIO expert.

这篇关于Java - 如何同时读写文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆