In my Eclipse project I have a "src" folder that's linked from a one drive folder.
I have some other text files in the linked folder that I want to load with a FileReader.
How would I get this location, optimally in a way that's agnostic to whether the folder is linked or actually in the project folder. I've tried using
MyClass.class.getResource("");
But it returns me a path to the "bin" folder. I'm probably not using it right. The file I want to get is "src/de/lauch/engine/shaders/primitiveTestShader/vertexShader.vsh"
Thanks in advance!
You can create resources folder like that 'src\main\resources' and put the file after that you can run your same code . hopefully it will work.
I solved my particular issue for now but im still open to better solutions :)
public class LinkedResourceLocator {
private static Dictionary<String,String> locations;
public static String getPath(String path) {
if(locations==null) {
File projectLocal = new File(LinkedResourceLocator.class.getClassLoader().getResource("").getPath().replaceAll("%20", " ")).getParentFile();
File dotProject = new File(projectLocal.getAbsolutePath()+"\\.project");
locations = new Hashtable<String,String>();
File[] files = projectLocal.listFiles(new FileFilter(){
#Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
for (int i = 0; i < files.length; i++) {
locations.put(files[i].getName(), files[i].getAbsolutePath());
}
try {
BufferedReader br = new BufferedReader(new FileReader(dotProject));
StringBuilder fileContentBuilder = new StringBuilder();
String line;
while((line = br.readLine()) != null) {
fileContentBuilder.append(line.trim());
}
String fileContents = fileContentBuilder.toString();
Pattern p = Pattern.compile("<link><name>(\\w*)</name><type>\\d*</type><location>([\\w/:]*)</location></link>");
Matcher m = p.matcher(fileContents);
while(m.find()) {
locations.put(m.group(1),m.group(2));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.err.println("Can't locate .project file");
} catch (IOException e) {
e.printStackTrace();
System.err.println("Can't read .project file");
}
}
String locator = path.contains("/")?path.substring(0, path.indexOf("/")):path;
String restPath = path.substring(locator.length());
return locations.get(locator)+restPath;
}
}
This class gets the linked resource locations from the eclipse .project file and then converts project local paths like "src/de/lauch/engine/shaders/primitiveTestShader/vertexShader.vsh" to these linked locations.
Related
I have a directory C:\Test\ that have several files and folder
example :
C:\Test\new1.txt
C:\Test\document.xls
C:\Test\presentation.pdf
C:\Test\pro_country
C:\Test\pro_libs
C:\Test\misc
C:\Test\pro_bin
C:\Test\mug
I want to MOVE (not copy ) all the folders that starts with pro into one folder
so ill have such direcotry
example :
C:\Test\new1.txt
C:\Test\document.xls
C:\Test\presentation.pdf
C:\Test\misc
C:\Test\mug
C:\Test\Newprofolder
I tried this code but I didn't know how to return the result from list files to copy them to the directory
File[] proList = direct.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return name.startsWith("Pro");
}
});
String pFileDest = directory + "//" + "pcore.war";
File filepldst = new File(pFileDest);
File filePortalSrc = new File(pLocation);
try {
FileUtils.copyFile(filePortalSrc, filepldst);
} catch (IOException e) {
e.printStackTrace();
}
Files.move(new File("C:\\projects\\test").toPath(), new File("C:\\projects\\dirTest").toPath(), StandardCopyOption.REPLACE_EXISTING);
Change source and destination path
I have a xml file that I'm getting its full path, and pass it to a function where I add a String to its name. However I'm not being able to use it (the initial fullpath) after adding the string. How can it be done, that after getting the fullpath in search(String dirName), and adding the string in lk(String fullpath), I can still use the path which is returned by search(String dirName).
public String search( String dirName)throws Exception{
String fullPath = null;
File dir = new File(dirName);
if ( dir.isDirectory() )
{
String[] list = dir.list(new FilenameFilter()
{
#Override
public boolean accept(File f, String s )
{
return s.endsWith(".xml");
}
});
if ( list.length > 0 )
{
fullPath = dirName+list[0];
lockFile(fullPath);
return fullPath;
}
}
return "";
}
public void lk( String fullPath) throws Exception {
File f = new File(fullPath);
String fileNameWithExt = f.getName();
try {
File newfile =new File(fileNameWithExt+".lock");
if(f.renameTo(newfile)){
System.out.println("Rename succesful");
}else{
System.out.println("Rename failed");
}
} catch (Exception e) {
e.printStackTrace();
}
}
try this
File originalFile = new File(<file parent path>, "myxmlfile");
File cloneFile = new File(originalFile.getParent(),
originalFile.getName()+"<anything_i_want_to_add>");
Files.copy(originalFile.toPath(),cloneFile.toPath());
//now your new file exist and you can use it
originalFile.delete();//you delete the original file
...
//after you are done with everything and you want the path back
Files.copy(cloneFile.toPath(),originalFile.toPath());
cloneFile.delete();
In your lock method, you are calling renameTo method. Because of that, the original filename is now gone, and is replaced by the new filename that ends with .lock.
The java.io.File class is not a file pointer but an object to hold a filename. Using a file object that still refers to an old filename will cause an error.
To answer your question: If you want the old filename after locking, you must use a different approach in locking your file. For example, MS Access locks their .accdb files by creating a lockfile with the same filename as the opened .accdb file.
You may use this code as a reference:
public boolean fileIsLocked(File file) {
File lock = new File(file.getAbsolutePath() + ".lock");
return lock.exists();
}
public void lockFile(File file) {
if (!fileIsLocked(file)) {
File lock = new File(file.getAbsolutePath() + ".lock");
lock.createNewFile();
lock.deleteOnExit(); // unlocks file on JVM exit
}
}
public void unlockFile(File file) {
if (fileIsLocked(file)) {
File lock = new File(file.getAbsolutePath() + ".lock");
lock.delete();
}
}
My file structure:
This is how it looks using netbeans project:
-src
-images
-*.jpg
-stock
-*.java
-images (exact copy of -images)
and here is my jar
-jar
-images
-*.jpg
-stock
-*.java
-images (folder is created but files don't get copied)
My files imagesCopy is the one that I create and ImagesOrg is the one inside .jar / src
File imagesCopy = new File("images");
File imagesOrg = new File(URLDecoder.decode(getClass().getResource("/images").getPath()));
if (!imagesCopy.exists()) {
imagesCopy.mkdir();
for(final File child : imagesOrg.listFiles()) {
try{
Files.copy(child.toPath(), Paths.get(imagesCopy.getAbsolutePath()+"/"+child.getName()), REPLACE_EXISTING);
}catch(Exception e){
System.out.println(e);
}
}
}
The problem definitely lies with:
File imagesOrg = new File(URLDecoder.decode(getClass().getResource("/images").getPath()));
When compiling it it gives me, which is the proper directory
D:\Code\build\classes\images
which is the right directory, but when using this program from jar file I get:
D:\Code\dist\file:\D:\Code\dist\egz.jar!\images
and I assume that it should just be:
D:\Code\dist\egz.jar!\images
without that first part
Probably the simplest way to do it is like this:
public static void main(String[] args) throws URISyntaxException, IOException {
File imagesCopy = new File("C:\\Users\\<YOURNAMEHERE>\\images");
URI uri = ImageCopy.class.getResource("/images").toURI();
if (!uri.toString().startsWith("file:")) {
Map<String, String> env = new HashMap<>();
env.put("create", "true");
FileSystems.newFileSystem(uri, env);
}
Path imagesOrg = Paths.get(uri);
System.out.println(imagesOrg);
if (!imagesCopy.exists()) {
imagesCopy.mkdir();
try(DirectoryStream<Path> paths = Files.newDirectoryStream(imagesOrg)) {
for (final Path child : paths) {
System.out.println(child);
try {
String targetPath = imagesCopy.getAbsolutePath() + File.separator + child.getFileName().toString();
System.out.println(targetPath);
Files.copy(child, Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
It's not super-pretty, but it works. Might need to fiddle with the code if you have nested directories.
Note that you must create the FileSystem before accessing it (as per the Oracle Docs). I don't know why this is required, but there we go.
I've tested this and it will copy files from inside your JAR to wherever you would like.
Here is a simple code to do it. You can adapt as you need.
package br.com.jjcampos.main;
//imports here
public class CopyImage {
private static ClassLoader loader = CopyImage.class.getClassLoader();
public static void main(String[] args) throws IOException {
InputStream stream = loader.getResourceAsStream("br/com/jjcampos/images/test.jpg");
OutputStream outputStream =
new FileOutputStream(new File("c:/temp/newImage.jpg"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
}
}
Understand that you can't copy a source from a stream (your jar) as a list of files. Unless you want to unpack it first. My suggestion is you to add a txt file with the list of your images then you read this file and use suggested code to copy each one.
Something like this:
public class CopyImage {
private static ClassLoader loader = CopyImage.class.getClassLoader();
public static void main(String[] args) throws IOException {
copyImages("c:/temp/");
}
public static void copyImages(String pathDestiny) throws IOException{
InputStream listOfFiles = loader
.getResourceAsStream("br/com/jjcampos/images/listImages.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(listOfFiles));
String line;
while ( (line = reader.readLine())!=null ){
InputStream stream = loader.getResourceAsStream("br/com/jjcampos/images/"
+ line);
OutputStream outputStream =
new FileOutputStream(new File(pathDestiny + line));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = stream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
}
}
}
And your listImages.txt with
test.jpg
And you should decide if you put the full path on the text file or not to use in your code.
i am trying to get the correct path to the sd card that is inside my samsung s4 android device through my app but when i try the above paths:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
String pathTwo = Environment.getExternalStorageDirectory().getAbsolutePath();
String path3 = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
It gets the private none-writable path of /storage/emulated/0 instead of the correct path of storage/sdcard1
I found the correct path via File explorer using the Android Device monitor but i do not want to hard code the path as the path can be different depending on the device.
kind regards
For all devices
String sSDpath = null;
File fileCur = null;
for( String sPathCur : Arrays.asList( "MicroSD","external_SD","sdcard1","ext_card", "external_sd", "ext_sd", "external", "extSdCard", "externalSdCard")) // external sdcard
{
fileCur = new File( "/mnt/", sPathCur);
if( fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
break;
}
if( sSDpath == null) {
fileCur = new File( "/storage/", sPathCur);
if( fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
break;
}
}
if( sSDpath == null) {
fileCur = new File( "/storage/emulated", sPathCur);
if( fileCur.isDirectory() && fileCur.canWrite())
{
sSDpath = fileCur.getAbsolutePath();
Log.e("path",sSpath);
break;
}
}
}
100% working, tested on multiple device.
Based on a previous answer, the path to external SD card actually varies with different device manufactures.
"Environment.getExternalStorageDirectory() refers to whatever the device manufacturer considered to be "external storage". On some devices, this is removable media, like an SD card. On some devices, this is a portion of on-device flash. Here, "external storage" means "the stuff accessible via USB Mass Storage mode when mounted on a host machine", at least for Android 1.x and 2.x.
But the question is about external SD. How to get a path like "/mnt/sdcard/external_sd" (it may differ from device to device)?
Android has no concept of "external SD", aside from external storage, as described above.
If a device manufacturer has elected to have external storage be on-board flash and also has an SD card, you will need to contact that manufacturer to determine whether or not you can use the SD card (not guaranteed) and what the rules are for using it, such as what path to use for it."
Based on this answer.
So, There is no absolute way to get this path via code.
As gilonm mentioned, external (removable) Sd path varies from device to device, but I wrote a method, which iterates through all the different ext paths that the different manufacturers use, and then it finds the exact match.
It returns empty String if path isn't found. If a path is found, you still need to verify whether the card is inserted or not. (By checking if sub-folders exist on that path)
Note: I used StreamSupport library inside the method, so you'll need to download the jar file and add it to libs folder of your project and that's it, it'll work!
public static String getExternalSdPath(Context context) {
List<String> listOfFoldersToSearch = Arrays.asList("/storage/", "/mnt/", "/removable/", "/data/");
final List<String> listOf2DepthFolders = Arrays.asList("sdcard0", "media_rw", "removable");
final List<String> listOfExtFolders = Arrays.asList("sdcard1", "extsdcard", "external_sd", "microsd", "emmc", "ext_sd", "sdext",
"sdext1", "sdext2", "sdext3", "sdext4");
final String[] thePath = {""};
Optional<File> optional = StreamSupport.stream(listOfFoldersToSearch)
.filter(new Predicate<String>() {
#Override
public boolean test(final String s) {
File folder = new File(s);
return folder.exists() && folder.isDirectory();
}
}) //I got the ones that exist and are directories
.flatMap(new Function<String, Stream<File>>() {
#Override
public Stream<File> apply(final String s) {
try {
List<File> files = Arrays.asList(new File(s).listFiles());
return StreamSupport.stream(files);
} catch (NullPointerException e) {
return StreamSupport.stream(new ArrayList<File>());
}
}
}) //I got all sub-dirs of the main folders
.flatMap(new Function<File, Stream<File>>() {
#Override
public Stream<File> apply(final File file1) {
if (listOf2DepthFolders.contains(file1.getName()
.toLowerCase())) {
try {
List<File> files = Arrays.asList(file1.listFiles());
return StreamSupport.stream(files);
} catch (NullPointerException e) {
return StreamSupport.stream(Collections.singletonList(file1));
}
} else
return StreamSupport.stream(Collections.singletonList(file1));
}
}) //Here I got all the 2 depth and 3 depth folders
.filter(new Predicate<File>() {
#Override
public boolean test(final File o) {
return listOfExtFolders.contains(o.getName()
.toLowerCase());
}
})
.findFirst();
optional.ifPresent(new Consumer<File>() {
#Override
public void accept(final File file) {
thePath[0] = file.getAbsolutePath();
}
});
Log.e("Path", thePath[0]);
try {
ContextCompat.getExternalFilesDirs(context, null);
} catch (Exception e) {
Log.e("PathException", thePath[0]);
}
return thePath[0];
}
P.S. I tested and verified it on a few HTC and Samsung devices.
This function will return the path of SD Card path.
private String getExternalSdCard(){
String finalPath = null;
File sdCardFile = ContextCompat.getExternalFilesDirs(this, null)[1];
String base = String.format("/Android/data/%s/files", getPackageName());
String path = sdCardFile.getAbsolutePath();
if(path.contains(base)){
finalPath = path.replace(base, "");
}
return finalPath;
}
To get all the list of storage. Use Loop
private String[] storages() {
List<String> storages = new ArrayList<>();
try {
File[] externalStorageFiles = ContextCompat.getExternalFilesDirs(this, null);
String base = String.format("/Android/data/%s/files", getPackageName());
for (File file : externalStorageFiles) {
try {
if (file != null) {
String path = file.getAbsolutePath();
if (path.contains(base)) {
String finalPath = path.replace(base, "");
if (validPath(finalPath)) {
storages.add(finalPath);
}
}
}
} catch (Exception e) {
CrashUtils.report(e);
}
}
} catch (Exception e) {
CrashUtils.report(e);
}
String[] result = new String[storages.size()];
storages.toArray(result);
return result;
}
I am new to Java, I tried to practice some example but I am facing issue in the file handling topic.
Following is the example that what I am trying.
T1--> T2--> T3--> T4--> Gan--> q.txt
|
--> Lin-->Img-->s.png
|
--> p.txt
This is the folder structure.
And I want output in the following format.
p.txt
Lin/Img/s.png
Gen/q.txt
That means when the first file is getting in any directory, after that next file will be printed with the path from first file is got.
The above directory structure is not fixed. It may change.
Now following are code that I have did but I am not getting proper output:
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
class FileProgram {
private ArrayList<File> listOfDirectories = new ArrayList<File>();
private ArrayList<File> rawFiles = new ArrayList<File>();
private ArrayList<String> parentDir = new ArrayList<String>();
private ArrayList<String> filesToDisplay = new ArrayList<String>();
private Iterator i, listDir;
private boolean firstFile = false;
private String parents = "";
public void getDetails(File file) {
try {
if (file.exists()) {
File directoies[] = file.listFiles();
if (!rawFiles.isEmpty()) {
rawFiles.clear();
}
for (File f : directoies) {
rawFiles.add(f);
}
i = rawFiles.iterator();
while (i.hasNext()) {
File isFile = (File) i.next();
if (isFile.isFile()) {
displayFiles(isFile);
}
if (isFile.isDirectory()) {
listOfDirectories.add(isFile);
}
}
iterateInnerDirectories();
} else {
System.out.println("Invalid File Path");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
FileProgram ray = new FileProgram();
ray.getDetails(new File("D:\\Temp"));
}
private void iterateInnerDirectories() {
listDir = listOfDirectories.iterator();
while (listDir.hasNext()) {
File isFile = (File) listDir.next();
File f = isFile;
listOfDirectories.remove(isFile);
getDetails(isFile);
}
}
private void displayFiles(File file) {
if (firstFile == false) {
firstFile = true;
String rootPath = file.getParent();
rootPath = rootPath.replace(file.getName(), "");
parentDir.add(rootPath);
parents = file.getParentFile().getName();
System.out.println(file.getName());
filesToDisplay.add(file.getName());
} else {
String rootPath = file.getParent();
rootPath = rootPath.replace(file.getName(), "");
if (parentDir.contains(rootPath)) {
parents = file.getParentFile().getName();
System.out.println(file.getName());
filesToDisplay.add(file.getName());
} else {
System.out.println(file);
}
}
}
}
Please anybody can help me to get proper output that I have mentioned above.
Thanks in advance.
Unless you're using a Java prior to Java 7 I would strongly suggest to use Path.
You can walk a directory recursively using Files.walkFileTree().
Once you encounter a file (!Files.isDirectory()), you can get its parent with Path.getParent(). And you can print the relative path to this parent of all further file using Path.relativize().
Short, Simple Implementation
In this implementation I don't even use Files.isDirectory() because visitFile() is only called for files:
public static void printFiles(Path start) {
try {
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
Path parent;
#Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
if (parent == null)
parent = file.getParent();
System.out.println(parent.relativize(file));
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
And this is how you call it:
printFiles(Paths.get("/path/to/T1"));