- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
FileChannel.open(path)
方法返回什么样的FileChannel
对象?
像下面这样还允许随机访问吗?
RandomAccessFile ra = new RandomAccessFile("RandomIndeed","rw");
FileChannel fc1 = ra.getChannel();
fc1
和下面的实例fc
有什么区别:
FileChannel fc = FileChannel.open(path);
基本上我想知道上面创建的两个对象之间的区别是什么,因此 fc1
和 fc
提前致谢。
最佳答案
从 RandomAccessFile
实例得到的 FileChannel
实例携带了它所创建的对象的随机访问行为,在这种情况下 fc1
是同步的ra
对象。可以看到在javadoc中有描述
Changing the channel's position, whether explicitly or by reading or writing bytes, will change the file position of the originating object, and vice versa. Changing the file's length via the file channel will change the length seen via the originating object, and vice versa. Changing the file's content by writing bytes will change the content seen by the originating object, and vice versa.
但是使用 FileChannel.open()
创建的 FileChannel
实例是 fc
没有这种行为。这对于您从 Streams 获得的 FileChannel
实例来说是正确的。它只保证文件的 View 在同一程序创建的对象之间是一致的。希望这对您有所帮助。
关于java - RandomAccessFile 与 FileChannel.open(path);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16465088/
我正在使用谷歌翻译器,我希望这个问题很好理解。 有一件事我不明白随机访问文件。不明白这个程序是如何工作的,但它确实有效。 这是我的程序: // ----------------------------
我目前正在使用 RandomAccessFile 并遇到一个奇怪的现象。我正在访问一个 1.1TB 大的文件,并且仅包含等于 00000000 的字节。 我通过以下方式实现了 RandomAccess
这是一段行为异常的代码。 private void saveToFile() throws IOException { JFileChooser fileChooser = new J
我正在尝试通过 RandomAccessFile 进行查找,作为算法的一部分,我必须读取一行,然后从该行末尾向后查找 例如 String line = raf.readLine(); raf.seek
查看 RandomAccessFile 的构造函数对于模式,它显示“rws”文件已打开以进行读写。文件内容或元数据的每次更改都必须同步写入目标设备。 这是否意味着“rw”模式是异步的?如果我需要知道文
我有两种方法,一种将 10 个整数写入 randomAccessFile,另一种读取这 10 个整数。我相信 writer 方法没有按预期运行。 这是我写入随机文件的方法: try{
简介 我通过这个项目挑战了自己。 我需要修改 .txt 上特定行的数据而不执行接下来的事情: 加载内存中的所有内容( map 、列表、数组...) 必须复制文件(例如 -> this ),从而占用计算
我正在开发一个使用 RandomAccessFile 的项目。我遇到的最大问题是,即使我在访问文件后关闭文件,文件也不会关闭,直到整个应用程序退出。这是标准行为还是有人知道发生了什么?代码基本上如下所
我不明白有什么区别 RandomAccessFile raf = new RandomAccessFile(file, "rw"); int offset = getOffset(); byte[]
我有一个固定格式的文件。 我想根据行号访问该文件中的特定行。 例如读取第 100 行 每行长度为200字节。 因此使用 RandomAccessFile 直接将光标移动到第 100 行就像: File
我正在编写一个程序,允许用户使用 randomaccessfile 写入文本文件。用户输入姓名、年龄、地址,每一项都是20字节,所以记录长度是60字节。当用户想要查找记录时,输入记录号,程序转到see
我有一个包含以下内容的文本文件: Ehsan,12345,11111,1000 maryam,147258,222,5000 reza,758694,abcd,4600 Ali,564789,kfcg
我正在尝试了解 RandomAccessFile,但在创建测试程序后,我得到了一些奇怪的输出。 import java.io.File; import java.io.IOException; imp
我有一个任务,我需要显示 200 个随机字符,然后询问使用他们想要替换的字母,然后替换所有这些字母。我生成了随机字符,但在替换字母时遇到了问题。有人可以帮助我朝着正确的方向前进吗?以下是我的一些其他问
每次打开我的程序时,我都想写入一个名为 myFile.txt 的文件。如果存在以前运行该程序时具有该名称的文件,我希望将其覆盖。我该如何做到这一点?我读过: RandomAccessFile f =
我有一个 jar 文件,其中包含 /dict/ 文件夹中的一堆文件。所以我将它添加为 Maven 依赖项。然后,为了为文件创建 RandomAccessFile ,我将其作为资源读取,放入 File
这个问题已经有答案了: Does RandomAccessFile in java read entire file in memory? (2 个回答) 已关闭 6 年前。 我正在尝试从一个非常大(
我必须从字节、整数和长数据大小的二进制文件中读取数据。我用 RandomAccessFile 的方法 readInt、readLong、readByte 读取它。问题是系统的字节序(Windows 8
读过这篇oracle java link我想知道这个类的作者对术语“随机”的确切含义是什么,如果缓冲区有自己的位置、限制、容量指示器。随机会做什么?我认为我只是在这种情况下误解了“随机”这个词。有人能
这是一种使用二分搜索在 RandomAccessFile 中搜索目标号码的方法。它专门处理整数。我已经完成了所有设置,但我得到了错误的数字。由于 raf 包含字节,而整数包含 4 个字节,因此我认为只
我是一名优秀的程序员,十分优秀!