teavm
teavm copied to clipboard
Java API RandomAccessFile("path","rw") mode "rw" can't read file
test code Unable to read file in 'rw' mode version:0.7.0
val tempFile = File("test.apk" )
tempFile.createNewFile()
val randomAccessFile = RandomAccessFile(tempFile, "rw")
randomAccessFile.write(byteArray)
randomAccessFile.close()
println("download success")
println(tempFile.length())
val file = RandomAccessFile("test.apk", "r")
println("raf len:${file.length()}")
println("raf data:${file.readLine()}")
val file1 = RandomAccessFile("test.apk", "rw")
println("file1 len:${file1.length()}")
println("file1 data:${file1.readLine()}")
Feel free to contact me if you need any additional information
Additional description: When reading a file using rw mode, the file length is 0
Can you please provide more information? Are you trying to execute this code in a browser? Does it work in some cases?
I'm not sure if the standard Java file APIs are implemented in TeaVM. Since the JavaScript from TeaVM usually executes in a browser, it is easier to use APIs like LocalStorage and SessionStorage to store data in the browser, or use Flavour's REST APIs to communicate with a server. Code running in a browser can't access the host filesystem directly.
Can you please provide more information? Are you trying to execute this code in a browser? Does it work in some cases?
I'm not sure if the standard Java file APIs are implemented in TeaVM. Since the JavaScript from TeaVM usually executes in a browser, it is easier to use APIs like LocalStorage and SessionStorage to store data in the browser, or use Flavour's REST APIs to communicate with a server. Code running in a browser can't access the host filesystem directly.
@ScraM-Team It was clear to me that I should use LocalStorage in the browser, but the migration was too costly because the old project had to force me to use RandomAccessFile.
My guess: it should be that rw mode does not load the existing file content when the file already exists (equivalent to re-creating the file anyway)
kotlin code
@JvmStatic
fun main(args: Array<String>) {
val tempFileName = "test.txt"
val file = File(tempFileName)
println(file.createNewFile())
file.writeBytes("Hello World".toByteArray())
val readOnlyAccessFile = RandomAccessFile(tempFileName, "r")
println("readOnlyAccessFile length: " + readOnlyAccessFile.length())
println("readOnlyAccessFile readLine: " + readOnlyAccessFile.readLine())
readOnlyAccessFile.close()
println("rw mode RandomAccessFile Open file content still exists")
println("check file exists: " + File(tempFileName).exists())
println("check file len: " + File(tempFileName).length())
println("-----------")
val rwAccessFile = RandomAccessFile(tempFileName, "rw")
println("rwAccessFile length: " + rwAccessFile.length())
println("rwAccessFile readLine: " + rwAccessFile.readLine())
rwAccessFile.close()
println("rw mode RandomAccessFile Content disappears when opening file")
println("check file exists: " + File(tempFileName).exists())
println("check file len: " + File(tempFileName).length())
println("-----------")
val readOnlyAccessFile1 = RandomAccessFile(tempFileName, "r")
println("readOnlyAccessFile1 length: " + readOnlyAccessFile1.length())
println("readOnlyAccessFile1 readLine: " + readOnlyAccessFile1.readLine())
readOnlyAccessFile1.close()
println("Can't read anything now")
println("check file exists: " + File(tempFileName).exists())
println("check file len: " + File(tempFileName).length())
}
config
<configuration>
<!-- <targetDirectory>${project.build.directory}/generated/js/teavm</targetDirectory>-->
<mainClass>org.example.HelloWorld</mainClass>
<minifying>false</minifying>
<sourceFilesCopied>false</sourceFilesCopied>
<sourceMapsGenerated>false</sourceMapsGenerated>
<debugInformationGenerated>false</debugInformationGenerated>
<optimizationLevel>ADVANCED</optimizationLevel>
<targetType>JAVASCRIPT</targetType>
<properties>
<!-- <java.util.TimeZone.autodetect>true</java.util.TimeZone.autodetect>-->
<java.util.Locale.available>zh_CN</java.util.Locale.available>
</properties>
</configuration>
I have also prepared a Java version of the code for your testing.
public class HelloWorldD {
public static void main(String[] args) throws IOException {
Intrinsics.checkNotNullParameter(args, "args");
String tempFileName = "test.txt";
File file = new File(tempFileName);
System.out.println(file.createNewFile());
try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
fileOutputStream.write("Hello World".getBytes(Charsets.UTF_8));
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
RandomAccessFile readOnlyAccessFile = new RandomAccessFile(tempFileName, "r");
System.out.println("readOnlyAccessFile length: " + readOnlyAccessFile.length());
System.out.println("readOnlyAccessFile readLine: " + readOnlyAccessFile.readLine());
readOnlyAccessFile.close();
System.out.println("rw mode RandomAccessFile Open file content still exists");
System.out.println("check file exists: " + (new File(tempFileName)).exists());
System.out.println("check file len: " + (new File(tempFileName)).length());
System.out.println("-----------");
RandomAccessFile rwAccessFile = new RandomAccessFile(tempFileName, "rws");
System.out.println("rwAccessFile length: " + rwAccessFile.length());
System.out.println("rwAccessFile readLine: " + rwAccessFile.readLine());
rwAccessFile.close();
System.out.println("rw mode RandomAccessFile Content disappears when opening file");
System.out.println("check file exists: " + (new File(tempFileName)).exists());
System.out.println("check file len: " + (new File(tempFileName)).length());
System.out.println("-----------");
RandomAccessFile readOnlyAccessFile1 = new RandomAccessFile(tempFileName, "r");
System.out.println("readOnlyAccessFile1 length: " + readOnlyAccessFile1.length());
System.out.println("readOnlyAccessFile1 readLine: " + readOnlyAccessFile1.readLine());
readOnlyAccessFile1.close();
System.out.println("Can't read anything now");
System.out.println("check file exists: " + (new File(tempFileName)).exists());
System.out.println("check file len: " + (new File(tempFileName)).length());
}
}
My code runs in Chrome for testing