preface

One day, this is the second section introduces mentioned in the second quarter of the com. Badlogic. Etf has. Files. FileHande class

FileHande

Com. Badlogic. Etf has. Files. FileHande store a file reference. FileHande’s constructors are:

  • FileHande(java.lang.string) : Pass in an absolute path (don’t use backslashes!!) Or relative path (under project file) to path file (folder)
  • FileHande(FileHande) : Passing in a FileHande object and pointing to the file it points to (folder)

Of course you also can use com. Badlogic. Etf has. Etf has. The input for Filehande:

  • internal( String ) :Obtain the file (folder) in the path (relative path under assets), read-only.Generally read as pictures, etc
  • local( String ) :Get local files (application private directory), readable and writable.Used to save game data
  • external( String ) :Get external files (C:\Users\ current user \ or SD card), readable and writable.Used to save large data or export data, etc

Tips: Files (folders) obtained via external(String) must be under C:\Users\ user. If not, use the FileHande(String) constructor.

Use FileHande DE

FileHande does more than just provide images for textures, it contains various operations on files (including folders) :

// Get the file name, with the suffix
String fileName= file.name() ;
// Get the file path with the suffix
String filePath= file.path() ;
// Check whether the file exists
boolean isFileExists= file.exists() ;
// Get the parent folder of the file
FileHande parentFile= file.parent();
// Get the length of the file
long length= file.length();
// Check whether the file is a folder
boolean isDirecroty= file.isDirecroty();
// Read the contents of the file
String readStr= file.readString();
// Read binary content
byte[] readBytes= file.readByte();
final boolean APPEND=true,NO_APPEND=false;
// Write a string
file.writeString("hello,world",APPEND);
file.writeString("hello,World",NO_APPEND);
// Write binary contentFile. WriteBytes (bytes, APPEND);// Copy the file to fileHande
file.copyTo(fileHande);
// Move the file to fileHande
file.moveTo(fileHande);
// Delete files
file.delete();
// Delete the folder
file.deleteDirecroty();
Copy the code

Preferenecs key-value pairs

Besides FileHande, libGDX also provides com. Badlogic. Etf has. Preferenecs class to store key-value pairs, Using com. Badlogic. Etf has. Etf has. App. GetPreferenecs (String arg0) to obtain the prefs file:

Preferenecs prefs=Gdx.app.getPreferenecs("myPreferenecs.prefs");
Copy the code

Tips: Preferenecs will actually be stored in C:\Users\ (user)\.prefs\

Use Preferenecs DE

// Change the value of key to value
prefs.put("key",value);
// To actually write the result to the file, called after put(). Tips: Try to call after all put()
prefs.flush();
// Get the value of key
prefs.get("key");
// If the key does not exist, defaultValue is returned
prefs.get("key",defaultValue);
Copy the code

try

Next, we’ll make a test application for file manipulation:

package com.libGDX.test;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.files.FileHandle;

public class Test extends ApplicationAdapter {
	
	@Override
	public void create(a) {
		super.create();
		FileHandle file=new FileHandle("C://test/test.txt");
                // If the file does not exist, the file and folder will be created. False indicates that the file will not be appended, but the original file content will be overwritten
		file.writeString("hello,libGDX FileHande!!!".false);
		System.out.println(file.readString());
                // If you delete the parent folder of a file, the file will also be deleted
		file.parent().delete();
		
		Preferences prefs=Gdx.app.getPreferences("test.prefs");
                // If the file does not exist, the file will be created
		prefs.putInteger("name".25041);/*25041:D*/
                // Write to the file
		prefs.flush();
		System.out.println(prefs.getInteger("name")); }}Copy the code

Running results: