My Android application needs to read a CSV file which is copied to the root directory on the device (device is NOT rooted so I guess it's not the real root directory, just the directory you see when opening the device in Windows explorer).
I wonder if this is possible?
When I do this:
File file = new File("/data.csv");
if (file.exists()) {
System.out.println("File exists!");
} else {
System.out.println("File does NOT exist");
}
I get: "File does NOT exist"
Try this:File file = new File(Environment.getExternalStorageDirectory()+ "/data.csv");Make sure you include the proper READ permissions(depending on the API level you are targeting) in your AndroidManifest.xml
Related
The code below is not deleting the file using API 33, but it's saying the file does exist though. I am not sure why the file is not deleting from external storage path?
Here is the code that I have...
File file = new File (Environment.getExternalStorageDirectory().toString() + "/Pictures/IMG_20230211_060830.jpg");
if (file.exists()) {
file.delete();
}
The file did not delete from file path, but it seems to work fine with API 31.
Whenever I open my .jar file, the sound will not work unless the files are in a folder titled "res" in the same directory as the .jar file. Now it will now work regardless of where the res folder is
Here's my code
try {
InputStream defaultSound = Game.class.getResourceAsStream("/TrailsGameMusic.wav");
// getClass().getSy.getResource("/images/ads/WindowsNavigationStart.wav");
System.out.println("defaultSound " + defaultSound); // check the URL!
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (Exception ex) {
ex.printStackTrace();
}
It's probably a problem with absolute path:
If you declare it like that: "/res/TrailsGameMusic.wav", you are referring to absolute path - it works. If you will write only "TrailsGameMusic.wav", it will be a relative path - it will not work.
You can configure your resources using for example this link:
How do I add a resources folder to my Java project in eclipse
I am trying to use a simple FileReader and yet, Android Studio keeps telling me that the file in question does not exist. I am using Apache POI if that makes any difference; regardless, the directory I am using is 100 percent correct and I have turned on hidden extensions, so that isn't the problem.
Any help would be appreciated,
Jacob
My java (where ncaa.xlsx exists):
FileInputStream file = new FileInputStream(new File("C:\\AdwCleaner\\ncaa.xlsx"));
Your Android application is being run on a virtual or physical device, that has a completely separate file system.
There are several ways to transfer a file to the the device.
For example, you can push a file to the device using adb push or using the file explorer in DDMS.
If you copy the file to a folder in the SD card, you can open it using Environment.getExternalStorageDirectory() (see example here):
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "<path to your file>");
You can also place the file in the assets folder, so it will be automatically available on the device (see this for creating the folder and this for reading the file from it):
InputStream file_stream = getAssets().open("myfile.xslx")
You can not open a file existing in PC file system to an Android device.
You have to push it first either by using command line adb push command or by using GUI DDMS feature.
After that, you can use Android API's to open the file. For example:
Environment.getExternalStorageDirectory()
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "file path");
Could it be a virus scanner? Perhaps try disabling the virus scanner temporarily (with your network connection physically disconnected), and run the Java app again.
I am using the JLayer(JZoom) API to play an mp3 file in my java application.
This is the code I implemented, I created a method and then call it later which works fine:
public void playMusic() {
try{
FileInputStream fis = new FileInputStream("myfilepath/file.mp3");
Player playMP3 = new Player(fis);
playMP3.play();
}
catch(Exception exc){
exc.printStackTrace();
System.out.println("Failed to play the file.");
}
}
When I export a runnable JAR file, the file is not included. I have also included the MP3 file in an assets folder located in the SRC folder of my application.
What can I do different in order to include the MP3 file when exporting to a runnable JAR file ?
You cannot use a FileInputStream to load a resource from a JAR file, because a resource in a JAR file is not a file.
Use:
InputStream in = YourClassName.class.getClassLoader().getResourceAsStream("myfilepath/file.mp3");
assuming the path inside the JAR file is myfilepath/file.mp3, and YourClassName is the name of any class inside the JAR file.
I am trying to work through Spring tutorials on file uploads. What I'm trying to do is have the file be saved to a folder within the project. The folder is called "files" and is separate from the src folder.
|bin
|build
|src
|files
I have this code which accepts a file upload:
public #ResponseBody String handleFileUpload(#RequestParam("name") String name,
#RequestParam("file") MultipartFile file){
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
//file.transferTo(); help?
return "You successfully uploaded " + name + "!";
What I want to do is use transferTo()to move the file to the "files" directory. When I try the true path, or try some sort of relative path I get this error which is created in the web window I am uploading files in.
Failed to upload file => "uploadedFileName/directory" does not exist
I am not sure why the file name is being appended to the directory path. Any assistance on this is much appreciated.
When you are trying to save the uploaded file to the file "uploadedFileName/directory", you are using relative file path. It's relative to the current working directory of the java process (java process, running your Tomcat Application Server or whatever appserver you are using). And that current working directory is not your project root. The following code:
System.out.println(new File(name).getAbsolutePath())
will print you the exactly path where you are trying to save your uploaded file.
To fix that issue you have to explicitly specify your project root:
File rootDir = new File("C:/Projects/myTestProject");
File uploadedFile = new File(rootDir, name);
file.transferTo(uploadedFile);
In real project you will want not to hardcode that rootDir path, but to retrieve it from some configuration file.
Don't rely to the current working directory of the application server. It could point to any directory.
PS that's out of scope of this question, but please be careful with saving data to the user-provided file names. Malicious user could post file with name "../../../../../../../../SomeSensitiveDirectory/SomeFile" and will overwrite that file unless you explicitly check that input parameter name for bad characters.
When you upload a file, you have to save it to another file.
You should use File#createTempFile() which takes a directory instead.
<p style="border-style:solid; border-color:#FFFFFF;">
<br>
File file = File.createTempFile("upload-", ".bin", new File("/path/to/your/uploads"));
<br><br>
item.write(file);
<br><br></p>