My require file in sd card has path
Android/data/mypakagename/android/myfile/abc/myfile.txt
i have a url from where i will download this myFile.txt and place it in this location.
Right now I have directory Android/data/mypakagename/ created in sdCard.
Now i am using following code.
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
file.mkdirs();
This creates all folders however it also create myfile.txt as a folder.I want to create all missing directories except last one. How can i do that?
File file = new File("Android/data/mypakagename/android/myfile/abc/myfile.txt
");
File parent = file.getParentFile();
parent.mkdirs();
String filename = "myfile.txt"
File dir = new File ("Android/data/mypakagename/android/myfile/abc");
dir.mkdirs();
File file = new File(dir, filename);
Related
I currently have a simple code in which i save a CSV file in a folder, according to my android studio monitor i am successfully saving that file in a folder i can't access which is "/storage/emulated/0/Download/myfolder/552.csv".
File path =new
File(Environment.getExternalStoragePublicDirectory(Environment. +
DIRECTORY_DOWNLOADS),"myfolder");
System.out.println(path);
if (!path.exists()){
path.mkdir();
}
File mypath=new File(path,(editSesion.getText().toString()+".csv"));
FileWriter mFile = new FileWriter(mypath,true);
CSVWriter writer = new CSVWriter(new FileWriter(mypath));
System.out.println(mypath);
Is there any way to obtain the path to a more commonly accesable folder?
/storage/emulated/0/Download/ is the Downloads folder of your phone storage.
How much common directory are you expecting than this?
Please add / (Forward slash) like this myfolder/
File(Environment.getExternalStoragePublicDirectory(Environment. +
DIRECTORY_DOWNLOADS),"myfolder");
change into
File(Environment.getExternalStoragePublicDirectory(Environment. +
DIRECTORY_DOWNLOADS),"myfolder/");
My requirement is to replace few files in a zip file.
The zip file in turn have multiple zip files and folder within it. It goes upto 4 or more levels of zip files.
I have a set of files in a different source directory. I would like to copy these files and replace within the zip file, by matching the file name in the source directory with file name inside the zip file.
Could someone help here please.
Thanks,
Deleep
One way is use temp files to store the ZIPs and open them as FileSystems, so you dont need to extrat all files and rezip everything
FileSystem firstZip = FileSystems.newFileSystem(URI.create("jar:file:/root/firstZip.zip"),
new HashMap<>(), null);
//Get a zip inside the first one
Path path = firstZip.getPath("FOLDER/ZIP_NAME.zip");
//Get the second zip input stream and store the zip in a temp file
InputStream in = firstZip.provider().newInputStream(path);
Path secondPath = Paths.get("/root/temp/tempFile.tmp");
Files.copy(in, secondPath);
//open the second zip
FileSystem secondZip = FileSystems.newFileSystem(new URI("jar:" + secondPath.toUri().toString()),
new HashMap<>(), null);
//iterate files in the second zip
DirectoryStream<Path> ds = Files.newDirectoryStream(secondPath);
for(Path p: ds){
//something
}
//delete or update files inside the second zip
//Files.delete(seconZip.getPath("aFile.txt"));
//Files.copy(anInputStream, secondZip.getPath("destFoldet/aFile.txt"));
//clse the seconzip
secondZip.close();
//update the second zip inside first one
Files.copy(secondPath, firstZip.provider().newOutputStream(path));
//delete temp file
Files.delete(secondPath);
//close first zip
firstZip.close();
Other idea is jboss-vfs. Never tried it, but i think that could help.
I am trying to create a folder by running java. As of now I have this and it works.
File f = new File ("/Users/myName/Desktop/nameOfDir");
f.mkdirs();
The question is what happens when I send this code to my friend? Would he have to change the code to the below for it to work?
File f = new File ("/Users/myFriendsName/Desktop/nameOfDir");
f.mkdirs();
how can I make the program find the correct path and create the folder where I want it (the desktop), regardless of who the user is?
Also, after creating the folder I will have to create a .txt in the folder. I can do this now, but same problem arise concerning different user names.
try using the following:
String userName = System.getProperty("user.name"); //platform independent
File f = new File ("/Users/" + userName + "/Desktop/nameOfDir");
f.mkdirs();
You can get the path to a user's home directory in a platform-independent way using:
System.getProperty("user.home");
So:
File f = new File(System.getProperty("user.home"), "Desktop/nameOfDir");
f.mkdirs();
I've been trying to save a plain text file into a specific folder in Google Drive on Android.
So far using the Documentation and QuickStart Guide on Google Drive I have been able to do a few things that are going in the right direction, first I was able to create a plain text file:
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
File file = service.files().insert(body, textContent).execute();
I have been able to create a new folder in the base directory of Google Drive with:
File body = new File();
body.setTitle("Air Note");
body.setMimeType("application/vnd.google-apps.folder");
File file = service.files().insert(body).execute();
I have also been able to list all of the folders in the user's Google Drive account with:
List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
for (File f : files) {
System.out.println(f.getTitle() + ", " + f.getMimeType());
}
However I am a bit stuck on how to save a text file into a folder within Google Drive.
You need to use the parent parameter to put a file in a folder using insert. More details at https://developers.google.com/drive/v2/reference/files/insert
Something like this
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));
File file = service.files().insert(body, textContent).execute();
If you want to INSERT a file in specific folder in Google Drive then follow these steps. Lets assume that we have retrieved all folder from the Drive and now i will INSERT an empty file in the first folder from the list, So
//Getting Folders from the DRIVE
List<File> files = mService.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
File f =files.get(1)//getting first file from the folder list
body.setTitle("MyEmptyFile");
body.setMimeType("image/jpeg");
body.setParents(Arrays.asList(new ParentReference().setId(f.getId())));
com.google.api.services.drive.model.File file = mService.files().insert(body).execute();
Now this will create an empty file in the Folder which are at the top in the retrieve file list.
Step1 : Create a folder
File body1 = new File();
body1.setTitle("cloudbox");
body1.setMimeType("application/vnd.google-apps.folder");
File file1 = service.files().insert(body1).execute();
Step2 : Insert your file
File body2 = new File();
body2.setTitle(fileContent.getName());
body2.setMimeType("text/plain");
body2.setParents(Arrays.asList(new ParentReference().setId(file1.getId())));
File file2 = service.files().insert(body2, mediaContent).execute();
I know how to create a temporary directory in Java, but is there an easy way to copy files in Java from the jar file to this directory?
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
File helpDir = new File(tmpDir, "myApp-help");
helpDir.createNewFile(); // oops, this isn't right, I want to create a dir
URL helpURL = getClass().getResource("/help-info");
/* ???? I want to copy the files from helpURL to helpDir */
Desktop desktop = Desktop.getDesktop();
URI helpURI = /* some URI from the new dir's index.html */
desktop.browse(helpURI);
Apache's org.apache.commons.io.FileUtils can do that for you
To create directory use File.mkdir();
Convert URL to File with org.apache.commons.io.FileUtils.toFile(URL)
use org.apache.commons.io.FileUtils.copyFile() to copy.
You can make use of the command jar tf [here your jarfile]. This will list the contents of the JAR Archive with their full path, relative to the jarfile (1 line = 1 file). Check if the line starts with the path of the directory you want to extract, and use Class.getResourceAsStream(URL) for the matching lines and extract them to your temporary folder.
Here is an example output of jar tf:
META-INF/MANIFEST.MF
TicTacToe.class
audio/
audio/beep.au
audio/ding.au
audio/return.au
audio/yahoo1.au
audio/yahoo2.au
images/
images/cross.gif
images/not.gif