randomaccessfile

How to properly close MappedByteBuffer?

偶尔善良 提交于 2019-12-01 19:23:05
This is the code I'm running: import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void main(String[] args) throws Exception { String filePath = "D:/temp/file"; RandomAccessFile file = new RandomAccessFile(filePath, "rw"); try { MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 128); // Do something buffer.putInt(4); } finally { file.close(); System.out.println("File closed"); } System.out.println("Press any key..."); System.in.read(); System.out.println("Finished"); } }

How to properly close MappedByteBuffer?

假装没事ソ 提交于 2019-12-01 17:54:19
问题 This is the code I'm running: import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void main(String[] args) throws Exception { String filePath = "D:/temp/file"; RandomAccessFile file = new RandomAccessFile(filePath, "rw"); try { MappedByteBuffer buffer = file.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 128); // Do something buffer.putInt(4); } finally { file.close(); System.out.println("File closed")

How can I write to a specific line number in a txt file in Java

[亡魂溺海] 提交于 2019-12-01 16:24:08
I'm currently writing my project for school in which requires me to read and write to txt files. I can read them correctly but I can only write to them at the end from an appended FileWriter. I would like to be able to overwrite things in my txt files on line numbers by first deleting the data on the line and then writing in the new data. I attempted to use this method... public void overWriteFile(String dataType, String newData) throws IOException { ReadFile file = new ReadFile(path); RandomAccessFile ra = new RandomAccessFile(path, "rw"); int line = file.lineNumber(path, dataType); ra.seek

Does RandomAccessFile in java read entire file in memory?

房东的猫 提交于 2019-11-29 06:14:15
I need to read last n lines from a large file (say 2GB). The file is UTF-8 encoded. Would like to know the most efficient way of doing it. Read about RandomAccessFile in java, but does the seek() method , read the entire file in memory. It uses native implementation so i wasn't able to refer the source code. Evgeniy Dorofeev RandomAccessFile.seek just sets the file-pointer current position, no bytes are read into memory. Since your file is UTF-8 encoded, it is a text file. For reading text files we typically use BufferedReader, Java 7 even added a convinience method File.newBufferedReader to

How to get random access to a file on SD-Card by means of API presented for Lollipop?

◇◆丶佛笑我妖孽 提交于 2019-11-28 21:41:50
My application uses Java class RandomAccessFile to read/write bytes to a file on SD card randomly by means of realization of SeekableByteChannel interface. Now I need rewrite it for Android 5.0 with new Lollipop API. I have found the only way to read: InputStream inputStream = getContentResolver().openInputStream(uri); and write: ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w"); FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); from/to a file in new API. I would like to have an ability to set channel in some random

How to get random access to a file on SD-Card by means of API presented for Lollipop?

折月煮酒 提交于 2019-11-27 14:01:56
问题 My application uses Java class RandomAccessFile to read/write bytes to a file on SD card randomly by means of realization of SeekableByteChannel interface. Now I need rewrite it for Android 5.0 with new Lollipop API. I have found the only way to read: InputStream inputStream = getContentResolver().openInputStream(uri); and write: ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w"); FileOutputStream fileOutputStream = new FileOutputStream(pfd