Android create directory hierarchy - java

I created a directory programmatically and i inserted photos :
File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(dirGallery, photo);
...
This is working !
But now i want to create a directory in my existing directory "Gallery" and insert other photos.
I tried :
File dirGallery = context.getDir("Gallery/Gallery2", Context.MODE_PRIVATE);
But i get "File ... contains a path separator".
I also tried :
File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
dirGallery.mkdir();
File dirGallery2 = new File(dirGallery,"Gallery2");
dirGallery2.mkdir();
File fileWithinMyDir = new File(dirGallery2, nomPhoto);
And when i get my file :
File dirGallery = context.getDir("Gallery", Context.MODE_PRIVATE);
File dirGallery2 = new File(dirGallery,"Gallery2");
File[] listImages = dirGallery2.listFiles(filter);
But listImages is empty. Where did i fail ?
TY

Instead of writing this:
File dirRecipe = context.getDir("Gallery/Gallery2", Context.MODE_PRIVATE);
try this:
File dirRecipe = context.getDir("Gallery"+File.separator+"Gallery2", Context.MODE_PRIVATE);
this should create the folder inside of folder as you want.

You can not pass a directory structure (e.g. a/b/c) to GetDir(), the following will work however:
File dir = getFilesDir();
File dir2 = new File(dir, "test1/test2");
dir2.mkdirs();
this will create the directory structure
/data/data/com.somename.someclass/files/test1/test2

Related

Java creating a file in a certain directory

How do I get a json file I am creating to go into another directory. I am using a method that gives a file name and use this code to create it, its not the full code but Im pretty sure this is the only part I need to modify.
FileInputStream in = new FileInputStream(fileName);
JSONObject obj = new JSONObject(new JSONTokener(in));
right now it puts it in a directory but I want to put it in a folder in that directory.
this also does not work
FileInputStream in = new FileInputStream("/A/Ab/src/serv"+fileName);
///////////////////////////
File cDir = new File("");
FileInputStream in = new FileInputStream(cDir.getAbsoluteFile() +fileName);
Try this:
File dir = new File("/A/Ab/src/serv");
if (!dir.exists()) dir.mkdirs();
then:
FileInputStream in = new FileInputStream("/A/Ab/src/serv/" + filename);`enter code here`
If the directory already exists and you just want to create a new file, then simply do:
FileInputStream in = new FileInputStream(new File("/A/Ab/src/serv/" + filename))

Not able to access file in eclipse gives : FileNotFoundException

In eclipse i am trying to give path of one configuration.json file but it is not taking it .
System.getProperty("user.dir")
gives C:\Users\cmalik\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse
and
try {
File f = new File("."); // current directory
File[] files = f.listFiles();
for (File file : files) {
if (file.isDirectory()) {
System.out.print("directory:");
} else {
System.out.print(" file:");
}
System.out.println(file.getCanonicalPath());
}
gives
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\Eclipse Jee Neon.lnk
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid10180.log
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid9728.log
And my project structure is
ProjectName
---src
---- com.abc.def.ghi.jkl.WebServices
------ myService.java
---configuration.json
I tried code for accessing file is :
FileReader file = new FileReader("configuration.json");
or
FileReader file = new FileReader("projectName\\configuration.json");
It is not able to access as output for System.getProperty("user.dir") is eclipse folder not my current projectName folder.
How can i get my files please let me know.
You can use either of the below two:
String location=System.getProperty("user.dir");
or
String location= new java.io.File(".").getCanonicalPath();
And then try to access the file using:
FileReader file = new FileReader(location+"\\configuration.json");
Hope it helps.

How to create File from folder in android assets?

I want to list all files in folder which is in assets. But new File("/android_asset/instagram").exists() always returns false.
File instagram = new File("/android_asset/instagram")
for (File lookUpFile : instagram.listFiles()) {
String filterName = FileUtils.removeExtension(lookUpFile.getName());
filterName = Strings.capitalizeAndCopy(filterName);
GPUImageLookupFilter lookupFilter = new GPUImageLookupFilter();
lookupFilter.setBitmap(BitmapFactory.decodeFile(lookUpFile.getAbsolutePath()));
filters.addFilter(filterName, lookupFilter);
}
Try to access the file using asset manager. Try to get the list of files and make sure its working.
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");

access File present in classpath servlet

In DynamicWebProject - Eclipse, How to get File Object for file present in src folder, i mean to say class path.
Below program works fine but new update to file is present in wtpwebapps, instead i want to get changed in file present in src folder of Eclipse only(How to get FileOutputStream for file present in classpath).
String filename1 = "/WEB-INF/classes/server123.properties";
ServletContext context = req.getSession().getServletContext();
String path1 = context.getRealPath(filename1);
System.out.println("PATH 1 IS :"+path1);
FileInputStream fis1 = new FileInputStream(new File(path1));
int ch1;
while ((ch1 = fis1.read())!=-1) {
System.out.print((char)ch1);
}
fis1.close();
FileOutputStream fos = new FileOutputStream(path1);
fos.write("jayesh".getBytes());
fos.close();
you can use getServletContext()/filename in your code i.e.
properties.load(new FileInputStream(getServletContext().getRealPath("/filename")));
or
properties.load(new FileInputStream(getServletContext().getRealPath(/filename")));

Deleting random access file in java

I've created a random access file as follows:
RandomAccessFile aFile = null;
aFile = new RandomAccessFile(NetSimView.filename, "rwd");
I want to delete the file "afile". can anyone suggest me how to do it?
You can do that:
File f = new File(NetSimView.filename);
f.delete();
Edit, regarding your comment:
The parameter NetSimView.filename seems to be a File and not a String that contains the path to the file. So simply do:
NetSimView.filename.delete();

Categories