I serialization object into new file in local storage by this mean:
ObjectOutputStream output=new ObjectOutputStream(openFileOutput("settings.dat", Context.MODE_PRIVATE));
output.writeObject(this);
output.close();
But in another function I must check file for exist:
File file=new File("settings.dat");
if (file.exists()) Toast.makeText(this, "yes", Toast.LENGTH_LONG).show();
file.exists() returne false always. Help me please.
Use:
File file = new File(getFilesDir() + "/settings.dat");
http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29
Related
I have this code here.
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
dir.mkdirs();
File file = new File(dir, "GIFName_" + System.currentTimeMillis() +".gif");
try{
FileOutputStream f = new FileOutputStream(file);
f.write(generateGIF(list));
}catch(Exception e){
e.printStackTrace();
}
my app basically converts images to .GIFS, and right now it saves it on the sd card, but I want to save it to gallery. Is there any way to do this easily? I know you can do it for images, but can you for .GIFS that are created?
Create folder in internal memory like this
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(mydir, "myfile");
FileOutputStream f = new FileOutputStream(fileWithinMyDir);
And than while saving file in the folder,give path to this directory.
Hope it helps!
I have created a file using FileOutputStream in my app,
fos = this.openFileOutput("foo.txt", Context.MODE_PRIVATE);
Now, I want to check if the file exists, using
File file = new File("foo.txt");
if (file.exists()) {
file.delete();
Log.d("tag", "im here");
}
If the file exists I want to delete it. But my code does not seem to reach "im here". Is my approach wrong? How can I correct it? Thanks
You need to specify the directory in which to look for the file. This can be achieved through the getFilesDir() method.
File file = new File(getFilesDir(), "foo.txt");
if (file.exists()) {
file.delete();
Log.d("tag", "im here");
}
When I create a file in java servlet, I can't find that file for opening. This is my code in servlet:
FileOutputStream fout;
try {
fout = new FileOutputStream("title.txt");
new PrintStream(fout).println(request.getParameter("txttitle"));
fout.close();
System.out.println(request.getParameter("txttitle"));
} catch (Exception e) {
System.out.println("I can't create file!");
}
Where I can find that file?
if you create file first as in
File f = new File("title.txt");
fout = new FileOutputStream(f);
then you use getAbsolutePath to return the location of where it has been created
System.out.println (f.getAbsolutePath());
Since you have'nt specified any directory for the file, it will be placed in the default directory of the process that runs your servlet container.
I would recommand you to always specify the full path of your your file when doing this kind of things.
If you're running tomcat, you can use System.getProperty("catalina.base") to get the path of the tomcat base directory. This can sometimes help.
Create a file object and make sure the file exists:-
File f = new File("title.txt");
if(f.exists() && !f.isDirectory()) {
fout = new FileOutputStream(f);
new PrintStream(fout).println(request.getParameter("txttitle"));
fout.close();
System.out.println(request.getParameter("txttitle"));
}
If the servlet cannot find the file give the full path to the file specified, like new File("D:\\Newfolder\\title.txt");
you should check first if the file doesn't exist ,create it
if(!new File("title.txt").exists())
{
File myfile = new File("title.txt");
myfile.createNewFile();
}
then you can use FileWriter or FileOutputStream to write to the file i prefer FileWriter
FileWriter writer = new FileWriter("title.txt");
writer.write("No God But Allah");
writer.close();
simply simple
Where should the CSV output text file from my Android App (writing in Eclipse) be stored. I currently use:
> File peopledetail_file = new File(this.getFilesDir(),
> "PeopleDetailsFile");
and
InputStream filestream = openFileInput("PeopleDetailsFile");
to access my file. This clearly does not work, as I get an error, saying it can't be found at this location
/data/data/com.example.partyorganiser/files/PeopleDetailsFile
For Creating a file you must call peopledetail_file.createNewFile() then write your file
File peopledetail_file = new File(this.getFilesDir(), "PeopleDetailsFile");
peopledetail_file.createNewFile();
FileOutputStream fos = new FileOutputStream(peopledetail_file);
// do writing ...
PrintWriter pw = new PrintWriter(fos);
pw.print("Hello");
pw.flush
fos.flush();
fos.close();
If you are going to use openFileInput(), use openFileOutput().
If you are going to use getFilesDir() for output, use getFilesDir() for input.
I tried creating a pdf file into my device out of a file in my local drive. But File.isFile() method returns false.It returns true if i compile the program as a simple java file. Is tht android would not locate a file in the local by reading the path or I/o operations in android are totally different to java i/o.How to make android recognise the file in the path mentioned. Any suggestions?
String path = "D:\\priya_Docs\\Android pdfs\\Professional_Android_Application_Development.pdf";
File file = new File(path);
System.out.println("Located a file " + file.isFile());
String filesArray = file.getPath();
File getFile = file.getAbsoluteFile();
FileInputStream fis = new FileInputStream(getFile);
FileOutputStream fos = (FileOutputStream) openFileOutput(
"Androiddoc.pdf", Context.MODE_PRIVATE);
System.out.println("File Created");
byte[] buff = new byte[1024];
int len;
while ((len = fis.read(buff)) >= 0) {
fos.write(buff, 0, len);
}
fis.close();
fos.close();
You android device (emulator or phone alike) cannot access you local drive.
It can only access its own system, and generally you can access only some of the files.
Check using .exists like if(myFile.exists())
You don't show your code, so I have to guess. I surmise you are doing File.isFile("/non/existant/path/file.pdf");. In that case, isFile() will return false, ofcourse.