i have a very small problem. I am writing a file to sdcard. I'l show you two code, one code works and other doesn't. It looks like this:
First one,
new FileOutputStream("/sdcard/HelloWorld.txt")
This works fine and creates a HelloWorld.txt file in sdcard.
Now second one,
new FileOutputStream(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest" + java.io.File.separator + "HelloWorld.txt")
This throws error "/mnt/sdcard/filetest/HelloWorld.txt (No such file or directory)".
I want to know why because i have mnt/sdcard path on my device, is it that it cannot find filetest folder if yes then isn't it supposed to create filetest folder if its not created before.
Thanks.
First Make a directory of filetest if its not available,
File file = new File(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest");
file.mkdir();
Then execute your code...
OR
File f = new File(android.os.Environment.getExternalStorageDirectory()+java.io.File.separator + "filetest" + java.io.File.separator + "HelloWorld.txt");
if (!f.getParentFile().exists());
{
f.getParentFile().mkdir();
}
Yes........ filetest folder are not there so you need to create it manually or programatically.and try that code...so you get success.
you can also create dir like this ::
File wallpaperDirectory = new File("/sdcard/filetest/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
Use Permission :::
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Related
When downloading a pdf file using DownloadManager, I want to delete it if it already exists in Environment.DIRECTORY_DOWNLOADS.
I check if file exists and delete it using these code:
private boolean fileExists(String fileName) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
return file.exists();
}
private boolean removeFile(String fileName) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
return file.delete();
}
removeFile() says that file is deleted, by returning true, and when I look into File Browser only one file is showing up. But when I delete that file it reappers on top of old one. What's wrong?
if i'm not wrong the File file= new File(...) will be your problem.
Because you are using the File class constructor, which is creates a new file to the exact location (File class documentation is here documentation).
Try to give your removeFile(String)method a File type variable, that should do the trick (or use some File 'getting' method from the documentation).
I am making a android app in which i am going to use File handling but to do that first i need to create it but by using following code:
File logf = new File("log1.txt");
Boolean bb = logf.exists();
if(!bb)
try {
bb = logf.createNewFile();
} catch (IOException e) {
msg.setText("not able to create file.");
}
'msg' is the TextView object which i am using to display error, and when i run this app.. it goes into IOException e catch.. Please tell me if i am doing something wrong or what?.. besides i am using Android 2.2 for my app.
In app storage, please try this:
File file = new File(getFilesDir(), "file.txt");
Or if you want to append file name, try this:
File file = new File(getFilesDir() + File.separator + "file.txt");
you need to provide absolute path of file like below.
File file = new File(/data/packagename/files/ + "log1.txt");
Okay, so in my project I am trying to serialize a chess game by writing to a folder named data. I did this in eclipse, and it was able to work. However, when I brought it into android studios I got the error of trying to get the length of a null array. Here is my method:
public static void writeData() throws IOException {
System.out.println("WRiting data");
File folder = new File("data" + File.separator);
folder.mkdir();
String[] directories = folder.list();
for (String name : directories) { //error here
File ff = new File(folder + File.separator + name);
if (ff.isDirectory()) {
deleteDirectory(ff);
}
}
//add all user data
for (game u : info.games) {
File f = new File("data" + File.separator + u.name); //make a file with user name
f.mkdir(); //make the file a directory
ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream(f + File.separator + "game-state"));
//create stream with file name at the end
oos.writeObject(u);
//write objects
oos.close();
}
}
Also, I looked at previous questions and changed my manifest file to allow for the permissions of writing and reading data.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The app does not crash when I read data. However, it crashes when I try to write data. Going crazy over here. Thank you for your help.
This may be due to marshmellow permission request ,you have to call permission request at runtime .
go through link
https://developer.android.com/training/permissions/requesting.html
When I call this :
File directory = new File("file:///android_asset/");
then this :
directory.isDirectory()
Always returns false.
Am I doing something wrong here ?
(Sorry if it is a dumb question)
Edit:
I store several images in the assets folder and I wanted to load them in my app (like an image gallery).
Call
directory.mkdirs();
after you created the File object
Edit:
in your case try to change file url as below:
new File("/mnt/external_sd/");
or find the true path by looking it in eclipse File explorer.. do not start it with file://
May be you are searching for this:
String fullPath = "/data/data/" + this.getPackageName() + "/" + path;
File dir = new File(fullPath);
if (!dir.exists())
dir.mkdir();
I want to check if a text file exists on the SD card. The file name is mytextfile.txt. Below is the code:
FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE);
How can I check whether this file exists?
This should do the trick, I've replaced the hard coded SDcard reference to the recommended API call getExternalCacheDir():
File file = new File(getExternalCacheDir(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
See this file System in android : Working with SDCard’s filesystem in Android
you just check
if(file.exists()){
//
}
*Using this you can check the file is present or not in sdcard *
File file = new File(sdcardpath+ "/" + filename);
if (file.exists())
{
}
You have to create a file, and set it to be the required file, and check if it exists.
String FILENAME="mytextfile.txt";
File fileToCheck = new File(getExternalCacheDirectory(), FILENAME);
if (fileToCheck.exists()) {
FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE);
}
Have Fun!
The FileOutputStream constructor will throw a FileNotFound exception if the specified file doesn't exist. See the Oracle documentation for more information.
Also, make sure you have permission to access the SD card.
check IF condition with Boolean type like
File file = new File(path+filename);
if (file.exists() == true)
{
//Do something
}