Where does Gdx search when i use Gdx.files.local(path)? - java

I'm using libgdx for a game. I want to read/write text files to update highscores, very simple.
I have three files already created placed in "$ANDROID-PROJECT/assets/data" , but Gdx seems to not find them when i use Gdx.files.local("data/file.txt"), but it does when i use Gdx.files.internal("data/file.txt"). Because i need to also write the file, i'm forced to use Gdx.files.local.
Can anyone tell me Where does Gdx search when i use Gdx.files.local(path) ? what path should i specify ?
code:
//for read
FileHandle file = Gdx.files.local("data/scoresCla.txt");
clasico = Float.parseFloat(file.readString());
//for write
FileHandle file = Gdx.files.local("data/scoresCla.txt");
file.writeString(String.valueOf(res), false);

if anybody have this problem, i solved by changing Gdx.files.local for Gdx.files.external. This way the files are stored in the SD Card, it has the same functionality of R/W files as local but in the SD Card.

Related

Detecting Audio File Bit Rate - Processing / Java

Trying to build a little app for sorting through audio files based on some of their properties. Have managed to grab the Sample Rate and Bit Depth using Minim but can't find anything anywhere for getting the Bit Rate?
Happy to look at taking the program to Javascript if needed but just desperate to find a method for detecting bit rate of a given file.
EDIT: Attempted to try and form an equation based off file size but cannot find a method for detecting MP3 file size either.
You can use jaudiotagger
You will need to download the jar, I managed to get it from maven central
Go to Sketch -> Add File... and select the downloaded jar, it should be added in a folder named code within your sketch folder.
Assuming you have placed an mp3 file in your data folder named audio.mp3 the following code should work, printing out the bit rate in the terminal.
import org.jaudiotagger.audio.mp3.*;
import org.jaudiotagger.audio.AudioFileIO;
void setup() {
File f = new File(dataPath("audio.mp3"));
try {
MP3File mp3 = (MP3File) AudioFileIO.read(f);
MP3AudioHeader audioHeader = mp3.getMP3AudioHeader();
println("" + audioHeader.getBitRate());
}
catch(Exception e) {
e.printStackTrace();
}
}
JAudiotagger supports a variety of file formats and you can use the relevant classes and methods for each one of these.
I suggest you take a look at the javadoc. Be careful of the examples though, the one I used in order to answer your question seems to be faulty, as you can see I had to swap getAudioHeader with getMP3AudioHeader.

How to remove A file from removable/secondary SD card

I want to delete a file from the removable sd card, I tried many ways but nothing did the job.
Tried:
file.delete();
and
File file = new File(selectedFilePath);
boolean deleted = file.delete();
and
DocumentFile documentFile = DocumentFile.fromFile(file);
documentFile.delete();
and
DocumentsContract.deleteDocument(context.getContentResolver(),
Uri.fromFile(file );
none of which deletes the file
I want to delete a file from the removable sd card
In general, you can't.
If you put the file in one of the Context locations (getExternalFilesDirs(), etc.), then you should be able to delete it, using delete() on a File object.
If this is some other file, you do not have permission to do anything with it, including delete it.
And on Android Q, you will not have much access to external or removable storage at all.
You are welcome to use the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT, ACTION_OPEN_DOCUMENT_TREE) and work with content that way.
i am trying to work on file explore
Android Q is severely restricting that entire app category. I recommend that you build something else.

Groovy: Save gform input type file to the assets pipeline (or similar)

(Sorry if this is simple; this is my first post)
Is the groovy/grails asset pipeline modifiable at runtime?
Problem: I am creating an application where users create the objects. The objects are stored as text files so that only the necessary objects are built at runtime. Currently, the text file includes a string which represents the filename of the image. The plan was to have these images stored in assets/images/ as this works best for later displaying the object. However, now I am running into issues with saving files to assets/images/ at run time, and I can't even figure out if this is possible. *Displaying images already works in the way I require if I drag and drop the images into the desired folder, however I need a way for the controller to put the image there instead. The relevant section of controller code:
def folder = new File("languageDevelopment/grails-app/assets/images/")
//println folder
def f = request.getFile('keyImage');
if (f.empty)
{
flash.message = 'file cannot be empty'
render(view: 'create')
return
}
f.transferTo(folder)
The error I'm receiving is a fileNotFoundException
"/var/folders/9c/0brqct9j6pj4j85wnc5zljvc0000gn/T/languageDevelopment/grails-app/assets/images (No such file or directory)"
on f.transferTo(folder)
What is the section it is adding to the beginning of my "folder" object?
Thanks in advance. If you need more information or have a suggestion to a different route please let me know!
new File("languageDevelopment/grails-app/assets/images/")
This folder is present only in your sources
After deployment it will looks like "/PATH-TO-TOMCAT/webapps/ROOT/assets/" if you use tomcat.
Also asset/images, asset/font etc. will be merged to assets folder.
If you'd like to store temporary files you can create some directory under src/resources folder.
For example "src/resources/images"
And you can get access to this folder from classloader:
this.class.classLoader.getResource('images/someImage.png').path

how to write tags,meta data to a file with android java(when we right click on windows>properties)

I want to add a comment to a file(video,txt,etc...). right click on a file on windows the comment should appear.
File tempf= new File (Environment.getExternalStorageDirectory().getAbsolutePath()+"/picedittemp");
tempf.setcommentOrTittle("mycomment to file");
Not all files contain a comment field so there is no standard way of doing it. Each file type however will have a library which will allow you to modify its tags. An mp3 for example has the JAudioTagger library which allows you to set tags like this.
//No guarantee this will work (just an example)
File track = new File("C:/path/to/song.mp3");
AudioFile f = AudioFileIO.read(track);
f.getTag().setField(FieldKey.COMMENT,"My Comment");

Extracting files from res folder in Executable JAR (txt files to be specific)

I would like to ask if its possible to put text files into my jar, I use them to make my map in my game, but users can get Highscores. now I want to save the Highscores with the map, so I have to save the map on the user their PC. Is there any way how I could do this? I've searched the internet for some ideas but I could not find anything that even came close to what I've wanted. I only had 3/4th of a year java so I don't know much about these things, everything that happens outside the debug of eclipse are problems for me(files are mainly one of those things, null exceptions, etc).
The main question now.
Is it possible to do? If yes, do you have any terms I could search on, or some sites/guides/tutorials? If no, is there any other way how I could save the highscores?
EDIT:
to make clear
Can I get the text file (the text inside the file) to be extracted to a different file in like the home directory of my game (where I save the settings and stuff) the basic maps are inside the jar file, so I want them to be extracted on the first start-up of the program
Greetings Carolien
"extracted to a different file in like the home directory of my game (where i save the settings and stuff) the basic maps are inside the jar file, so i want them to be extracted on the first startup of the program"
You can get the URL by using getClass().getResource()
URL url = getClass().getResource("/res/myfile.txt");
Then create a File object from the URI of the URL
File file = new File(url.toURI());
Then just perform your normal file operations.
if (file.renameTo(new File(System.getProperty("user.home") + "\\" + file.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
Assuming your file structure is like below, it should work fine
ProjectRoot
src
res
myfile.txt
Note: the above is moving the entire file. If you want to extract just the data inside the file, then you can simple use
InputStream is = getClass().getResourceAsStream("/res/myfile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The just do normal IO operation with the reader. See here for help with writing the file.

Categories

Resources