Use PrintWriter to recreate a text file in /res/raw/ with android? - android

I am creating an app where the configuration file needs to be re-written on startup and would like to use PrintWriter to create it. The problem is PrintWriter takes a file as a parameter. is there any way to reference a text document in /res/raw/ as File object in order to fit the parameters for PrintWriter?
I don't know if this will help but here is some code to better explain my question:
(its the basic idea but doesnt work...)
PrintWriter writer = new PrintWriter(new File("res/raw/config.txt"));
/*(I need to acces my config file... here ) */

You can't write to the res/raw directory because it is part of your apk file. You will need to either save the configuration a preference file using PreferenceManager.getDefaultSharedPreferences(Context context) or write a file to your app private directory. You can create a file like this:
PrintWriter writer = new PrintWriter(context.openFileOutput("config.txt", MODE_PRIVATE));

Related

Deleting content of Internal Storage file in android

I have created a file for storing important data from my application , at the press of a button i want to clear data from that file without deleting it;I am using MODE_APPEND to append the data from my application.Please do suggest.
Try this
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
this will print empty string in the file

Add a file to project

I need to write an application that modify a "PDF template" file that contain some form inside.
My application should make a copy of that template, fill form inside and save it with an arbitrary name (keeping the original template for other tasks).How can I add that file into my project, and retrieve it inside my code?
Usually such files are put in the asset folder of your project.
Then you can access the file by using
getAssets().open("filename");
in the case of opening a text file with a stream reader use
BufferedReader reader = new BufferedReader(
new InputStreamReader(getAssets().open("filename.txt")));
I don't know the 'protocol' required to open/edit pdf files.

can't find file created in dir returned by getFilesDir()

One part of my app needs to write a data file out.
I use getFilesDir() and it gives me a path like this (after adding a file name):
/data/data/com.casadelgato.zillaconfigdroid/files/Log.1378357559316.csv
I create a PrintWriter to the file, write a bunch of stuff to it, and close it.
No errors.
The problem is that I can't find the file anywhere on my Android device.
mention the file name in the path
FileOutputStream fos = context.openFileOutput(path,
Context.MODE_PRIVATE);
write the file into the stream...and you get the file by passing the same file name
FileInputStream openFileInput = context.openFileInput(path);
read the input stream

how to create .json file and how to write json object into json file ...In android

I am using this code for creating the new file and write the json object in test.json file but I am unable to create file.Please find mistake and give the best result
One things I have also write the code for permission in manifest file..
Thanks,
String fileName = "test";
File file = new File("/data/data/packagename/test.json");
file.createNewFile();
DataOutputStream fos = new DataOutputStream(openFileOutput(fileName , Context.MODE_PRIVATE));
if(file.exists()){
fos.write(jsString.getBytes());
fos.close();
}
This will not land on C:/ anyway. At best at your SD card or some other folder, and since android is a unix based system. you should be careful on how you use "slashes"

Android files in intenal storage and local directory

Android gives you getDir (I assume this means I would have myappspace/somedirectory) to create a directory in you application space. But how do you read or write to a file in that directory when android gives you an error if you have the path separator in the openFileOutput/Input call it gives you an error?
getDir returns a File object. To manipulate the directory and file structure, you continue to use File objects. For example
File dir = getDir("myDir", Context.MODE_PRIVATE);
File myFile = new File(dir, "myFile");
the openFileOutput simply returns a FileOutputStream based on some text criteria. All we have to do is create the object
FileOutputStream out = new FileOutputStream(myFile);
From here, you continue as normal.
String hello = "hello world";
out.write(hello.getBytes());
out.close();
FileInputStream would follow the same guidelines.
The point is that openFileInput() and openFileOutput() work with files in that directory directly so they don't need an absolute pathname.
EDIT: More accurately, they work with files in the directory returned by getFilesDir() rather than getDir() which is normally the package root.
If you want to create custom directories relative to getDir(), then you'll need to use classes/methods other than openFileInput() and openFileOutput() (such as using InputStream and OutputStream and relevant file 'reader' / 'writer' classes).

Categories

Resources