Description: File handling on different platforms is slightly different from file management
- Desktop (Windows,linux,mac OS x, and so on): in desktops, the file system is a chunk of memory. The file can be referenced by the current working directory or by an absolute path. Permissions can be ignored, and files and directories can often be read and written by all programs.
- Android:
The situation in Android is a bit complicated. Files can be stored in the app's apk in the form of resources or assets. These files are read-only. LIBGDX only uses the assets mechanism, assets provides access to the bitstream of the original file and is closest to the traditional file system. Resource files are available for Android's regular apps, but there are a lot of problems when using the game. Android can control resource files when loaded, such as automatically resizing pictures. The assets is stored in the assets directory of the Android project and will be packaged into the APK, which cannot be accessed by other apps. Files can also be saved to the internal storage for read and write. Each installed application has a separate directory, and this directory is accessible only to the application. You can treat this storage as a private storage space for your app. Files can also be stored in external memory, like SD cards. External memory are not available at all times. The directory for file storage is not stable, and you need to add relevant permissions in Androidmanifest.xml before using external memory.
1 < android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
- iOS: All file types in iOS are temporarily unavailable.
- JavaScript or WebGL:
An original JavaScript or WEBGL application does not have a traditional file system concept. Resources such as images are implemented via URLs. The browser now natively supports local storage, which is basically similar to a traditional file system. LIBGDX provides you with an abstraction for a read-only file system.
- The file in Libgdx represents an instance of a FileHandle class, and a filehandle has a type that specifies where the file is stored.
Type |
Descrption |
Desktop |
Android |
HTML5 |
Ios |
Classpath |
Read-only |
Yes |
Yes |
NO |
Yes |
Internal |
Read-only |
Yes |
Yes |
Yes |
Yes |
Local |
Read-write |
Yes |
Yes |
No |
Yes |
External |
Read-write |
Yes |
Yes |
No |
Yes |
Absolute |
Absolute path, use as little as possible |
Yes |
Yes |
No |
Yes |
- For absolute paths and classpath, typically used for the path of a desktop program, the order of use should look like this:
- Internal files: All assets that are packaged with the program are internal (Internal files)
- Local files: If you need to write some small files, such as the state of the game, use local files. These are generally private to the application.
- External files: If you want to write some large files, such as or download good files, can be stored in External files, it should be noted that the external files are not stable, users can delete or move the files you write.
- Check storage availability and path
- You can query the available information through the file model:
- You can also query the root path of a file like this:
- Check that the file exists, check if it is a directory:
1 Boolean exists =gdx.files.external ("Doitexist.txt"). exists (); 2 boolean isdirectory =gdx.files.external ("test/"). Isdirectory ();
- Listing file directories is also very simple
1 filehandle[] files =gdx.files.local ("mylocaldir/"). List (); 2 for (FileHandle file:files) {3 // Do something interesting here 4 }
- We can also get a handle to the parent file or child file with the same file
1 filehandle parent =gdx.files.internal ("Data/graphics/myimage.png"). Parent (); 2 filehandle child =gdx.files.internal ("data/sounds/"). Child ("Myaudiofile.mp3");
1 filehandle file =gdx.files.internal ("MyFile.txt" ); 2 String text = file.readstring (); 3 Gets the binary data 4 filehandle file =gdx.files.internal ("Myblob.bin" ); 5 byte [] bytes = File.readbytes ();
- write content to file
-
1 filehandle file =gdx.files.loca L ("myfile.txt" 2 file.writestring ("My God, it's full of stars", ); 3 Binary file 4 filehandle file =gdx.files.local ("Myblob.bin" ); 5 file.writebytes (newbyte[] {3,-2, ten}, );
- Delete, copy, rename, and move files/directories
1 filehandle from =gdx.files.internal ("Myresource.txt"); 2 From.copyto (Gdx.files.external ("Myexternalcopy.txt")); 3 Gdx.files.external ("Myexternalcopy.txt"). Rename ("Mycopy.txt"); 4 Gdx.files.external ("Mycopy.txt"). MoveTo (Gdx.files.local ("Mylocalcopy.txt")); 5 Gdx.files.local ("Mylocalcopy.txt"). Delete ();
The original is written by Bo Master Le Zhi Editor, the copyright belongs to the Bo owner. The original address http://www.dtblog.cn/1134.html reprint please specify the source!
"Open source Java Game Framework libgdx topic" -07-File Processing