I am trying to load some data saved in my property file in an Android Application.
I have put my property file under the src folder. Every time I try to load data from my file it keeps telling me FileNotFoundException open failed ENOENT (No such file or directory).
My Code is as follows:
This code is to save the file (new create)
File file = new File("src/com/example/testphonegap/SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
This code is to load data
properties = new Properties();
InputStream is = null;
// First try loading from the current directory
try {
File f = new File("src/com/example/testphonegap/SilverAngel.properties");
is = new FileInputStream(f);
// Try loading properties from the file (if found)
properties.load(is);
GetPersonaliseSettings();
GetUserSettings();
GetFavSettings();
}
catch ( Exception e ) {
is = null;
}
Can you tell me what I am doing wrong please? Is it where the file is saved or am I missing something in my code?
It's an Android Application, so the file is stored on the Android device and not on your computer.
If you want to save the file on the SD card you can write the following:
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/subfolderForYourApp");
dir.mkdirs();
This will create a subfolder for your application on the SD card.
To create a new file in this directory:
File file = new File(dir, "SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
Don't forget to add the permission to the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
try this
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/example/testphonegap/SilverAngel.properties");
properties.load(inputStream);
make sure you don't add "src" before property file name.
Put it in the assets folder and load it from there.
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("SilverAngel.properties");
properties.load(inputStream);
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!
How to upload files to my Box Sub-Folder using either by subfolder name or ID
Example say I have 2 subfolders(subfolder1 and subfolder2) in my Box, How to upload files to subfolder2 using java sdk.
Can we upload using any new methods.
Successful in uploading files to Box root folder using the code below
BoxFolder bfolder = BoxFolder.getRootFolder(api);
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
bfolder.uploadFile(stream, fileName);
You probably need to enumerate the folders till you find subfolder1, then create a new BoxFolder from that. Something like this (edit for compile errors):
BoxFolder bfolder = BoxFolder.getRootFolder(api);
Iterator<BoxFolder.Info> it = bfolder.getChildren().iterator();
for(BoxFolder.Info i : it){
if(i.getName().equals(subfolder1)){
BoxFolder folder = new BoxFolder(api, i.getID());
FileInputStream stream= null;
filePath = "c:\\UploadFile.txt";
stream = new FileInputStream(filePath);
fileName = FilenameUtils.getBaseName(filePath.toString());
folder.uploadFile(stream, fileName);
break;
}
}
I created a file in internal memory in an activity and want to write on it again on next activity but am getting this error.
in first activity:
String id_file = "tt_id";
String key_file = "tt_key";
FileOutputStream outputStream1;
FileOutputStream outputStream2;
try {
outputStream1 = openFileOutput(id_file, Context.MODE_PRIVATE);
outputStream2 = openFileOutput(key_file, Context.MODE_PRIVATE);
outputStream1.write(id.getBytes());
outputStream2.write(key.getBytes());
outputStream1.close();
outputStream2.close();
but in second activity:
FileOutputStream outputStream1;
FileOutputStream outputStream2;
String id="dfg";
String key="khdfks";
outputStream1 = openFileOutput("tt_id",Context.MODE_PRIVATE);
outputStream2 = openFileOutput("tt_key", Context.MODE_PRIVATE);
outputStream1.write(id.getBytes());
outputStream2.write(key.getBytes());
outputStream1.close();
outputStream2.close();
I've just started with android app development, so any help is appreciated. Thank you in advance
I think you need to specify the created file location before read/write. Also make sure the file exists. Else it will throw a file not found exception. To do so try this,
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.txt");
file.createNewFile();
if(file.exists())
{
OutputStream outputStream1 = new FileOutputStream(file);
String id="dfg";
outputStream1.write(id.getBytes());
outputStream1.close();
}
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
I am trying to have my app export a .csv file that can be picked up in the download APP. The way I am doing this is through fileoutput
I get remotedir By calling Environment.getExternalStorageDirectory().getAbsolutePath()
* Download CSV
*/
public static void downloadCSV(String filename, String remoteDir) throws Exception{
File dir = new File( remoteDir + "/download/");
dir.mkdirs();
//FileWriter f = new FileWriter("Download/" + filename + ".csv");
File fileObject = new File(dir, filename);
fileObject.delete();
fileObject.createNewFile();
ObjectOutputStream objectOut = null;
FileOutputStream stream = new FileOutputStream(fileObject);
objectOut = new ObjectOutputStream(new BufferedOutputStream(stream));
/* Irrelevant code */
String csv = Helper.getCSV(table.getColumnList(), view);
objectOut.writeChars(csv);
objectOut.close();
}
Whenever I test it on my phone(HTC One S) I don't see the file anywhere. I want my csv file to pop up in the Downloads app, but I'm not sure which directory that represents.
Thanks
You might have to scan the file with the MediaScanner before it will show up, at least that's how you'll be able to see it from your desktop for finding/debugging. See the MediaScanner docs for details.