randomaccessfile

how to write UTF8 data to xml file using RandomAccessFile?

笑着哭i 提交于 2021-01-28 05:30:36
问题 When trying to write some UTF8 data to a file, I end up with some garbage in the file. The code is as follows public static boolean saveToFile(StringBuffer buffer, String fileName, ArrayList exceptionList, String className) { log.debug("In saveToFile for file [" + fileName + "]"); RandomAccessFile raf = null; File file = new File(fileName); File backupFile = new File(fileName+"_bck"); try { if (file.exists()) { if (backupFile.exists()) { backupFile.delete(); } file.renameTo(backupFile); } raf

DataInputStream,DataOutputStream读写UTF8原理

大憨熊 提交于 2020-02-29 21:43:17
今晚上写代码玩,用到 java.io.RandomAccessFile.writeUTF(String) 函数,而文件默认保存为gbk,显然是乱码。突然想起来去看看存储编码规则,就去找了些文章了解writeUTF(String)的原理,在此记录。 首先需要弄明白unicode与utf8的表示规则,搜到@Feng哥的一篇文章《 字符编码笔记:ASCII,Unicode和UTF-8 》,写的很明白,在此招录一段: | Unicode符号范围 | UTF-8编码方式 | 0000 0000-0000 007F | 0xxxxxxx | 0000 0080-0000 07FF | 110xxxxx 10xxxxxx | 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx | 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 下面,还是以汉字"严"为例,演示如何实现UTF-8编码。 已知"严"的unicode是4E25(100111000100101),根据上表,可以发现4E25处在第三行的范围内(0000 0800-0000 FFFF),因此"严"的UTF-8编码需要三个字节,即格式是"1110xxxx 10xxxxxx 10xxxxxx"。然后,从"严"的最后一个二进制位开始

Is it possible to have random access writes to a Google Drive file from the Android API?

笑着哭i 提交于 2020-01-16 01:53:09
问题 I'm trying to record audio and create a wav file in Google Drive with the Android API. I have a first pass where I write the header with a file length of 0 bytes because I don't know how long the recorded audio will be and I don't want to keep it all in memory. Once the record is finished I seek back to byte 4 and write the length of the file. This works great when using a RandomAccessFile but I can't figure out how to do something using the Google Drive API. https://developers.google.com

Clear contents of a file in Java using RandomAccessFile

倾然丶 夕夏残阳落幕 提交于 2019-12-31 05:29:15
问题 I am trying to clear the contents of a file I made in java. The file is created by a PrintWriter call. I read here that one can use RandomAccessFile to do so, and read somewhere else that this is in fact better to use than calling a new PrintWriter and immediately closing it to overwrite the file with a blank one. However, using the RandomAccessFile is not working, and I don't understand why. Here is the basic outline of my code. PrintWriter writer = new PrintWriter("temp","UTF-8"); while

Java RandomAccessFile.java under linux not working correctly

邮差的信 提交于 2019-12-31 04:20:10
问题 Im trying to implement the simple tail -f linux command in java. Here is my code. try { // position within the file File file = new File("/home/curuk/monitored/log.txt"); RandomAccessFile raFile = new RandomAccessFile(file, "r"); long last = file.lastModified(); // The last time the file was checked for changes long position = file.length(); while (true) { if (file.lastModified() > last) { last = file.lastModified(); readFromFile(raFile, (int) position, (int) (file.length() - position));

Does RandomAccessFile in java read entire file in memory?

£可爱£侵袭症+ 提交于 2019-12-29 06:50:05
问题 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. 回答1: 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

Troubles Finishing First Assignment - Python [closed]

与世无争的帅哥 提交于 2019-12-23 05:34:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . I'm doing my first programming course and I'm stuck already. I'm trying to make a program that will tell you how many toy's you can make given how many pieces the user has. To create the toy, you need to have 5 upper pieces and 2 lower pieces. I've assigned the upper pieces to the

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

空扰寡人 提交于 2019-12-19 16:47:12
问题 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);

How do I fetch specific bytes from a file knowing the offset and length?

我的梦境 提交于 2019-12-18 08:58:21
问题 I have a file, and the first 4 bytes of the file are the magic such as LOL . How would I be able to get this data? I imagined it would be like: byte[] magic = new byte[4]; RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.read(magic, 0, magic.length); System.out.println(new String(magic)); Output: LOL Sadly this isn't working for me. I can't find a way to fetch specific values. Does anyone see any way to solve this issue? 回答1: Use RandomAccessFile.seek() to position to where you

Does RandomAccessFile.read() from local file guarantee that exact number of bytes will be read?

隐身守侯 提交于 2019-12-13 19:35:48
问题 Currently my code works fine but should I replace raf.read() to raf.readFully() in order to ensure that all bytes will be read? raf = new RandomAccessFile(doc.getFilePath()+"/"+doc.getName(),"r"); raf.seek((partNumber-1)*partitionSize); byte[] buf = new byte[partitionSize]; int bytesRead = raf.read(buf); //ensure myself by readFully or not? System.out.println("expected="+partitionSize+" readed="+bytesRead); My suggestion is the following - when reading from local resource like file one read()