I need to convert the file path in windows say C:\Documents and Settings\Manoj\Desktop for java as C:/Documents and Settings/Manoj/Desktop .
Is there any utility to convert like this.?
String path = "C:\\Documents and Settings\\Manoj\\Desktop";
path = path.replace("\\", "/");
// or
path = path.replaceAll("\\\\", "/");
Find more details in the Docs
String path = "C:\\Documents and Settings\\Manoj\\Desktop";
String javaPath = path.replace("\\", "/"); // Create a new variable
or
path = path.replace("\\", "/"); // Just use the existing variable
Strings are immutable. Once they are created, you can't change them. This means replace returns a new String where the target("\\") is replaced by the replacement("/"). Simply calling replace will not change path.
The difference between replaceAll and replace is that replaceAll will search for a regex, replace doesn't.
Java 7 and up supports the Path class (in java.nio package).
You can use this class to convert a string-path to one that works for your current OS.
Using:
Paths.get("\\folder\\subfolder").toString()
on a Unix machine, will give you /folder/subfolder. Also works the other way around.
https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html
Just check
in MacOS
File directory = new File("/Users/sivo03/eclipse-workspace/For4DC/AutomationReportBackup/"+dir);
File directoryApache = new File("/Users/sivo03/Automation/apache-tomcat-9.0.22/webapps/AutomationReport/"+dir);
and same we use in windows
File directory = new File("C:\\Program Files (x86)\\Jenkins\\workspace\\BrokenLinkCheckerALL\\AutomationReportBackup\\"+dir);
File directoryApache = new File("C:\\Users\\Admin\\Downloads\\Automation\\apache-tomcat-9.0.26\\webapps\\AutomationReports\\"+dir);
use double backslash instead of single frontslash
so no need any converter tool just use find and replace
"C:\Documents and Settings\Manoj\Desktop"
to
"C:\\Documents and Settings\\Manoj\\Desktop"
String path = "C:\\Documents and Settings\\someDir";
path = path.replaceAll("\\\\", "/");
In Windows you should use four backslash but not two.
Related
I need to know the location of my jar file , so I tried
String path = MigrationsApplication.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String decodedPath = URLDecoder.decode(path, "UTF-8");
And this returned
file:/C:/Users/fabio/OneDrive/Ambiente de Trabalho/MigrationTool/colbi-migration-tool/target/migrations-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/
Although I think I am capable of getting the location of my jar by doing a split by ! is there a better solution for this?
The final directory that I want in this specific case is target , so can after the split can I use the method getParent?
I am trying to d file = new file (location) while location of file is absolute path with somethign like this : \\test\hold\REPO/TEST/Letter/123.pdf
I am getting file not found exception while files are there in this path. what could be going wrong? can i have path with both forward and backward slash?
String separator = System.getProperty("file.separator");
So location can be rewritten to
location=separator+"test"+separator+"hold"+separator +"REPO"+separator "TEST"+separator+"Letter"+separator+"123.pdf";
In this case no need to think about underlying OS
You need two slashes in a string literal to represent one in the filename. Try
"\\\\test\\hold\\REPO/TEST/Letter/123.pdf"
or better still
"//test/hold/REPO/TEST/Letter/123.pdf"
There is never a need to use a backslash in a filename in Java.
Try adding quotes around the filename.
You can write your code without single backward slashes.if you want to use back-word slashes use \\ instead of \ .A single backward slashes create a problem if you put it inside the String literal.So you can write you code in multiple ways to avoid your exceptions.
1) File f=new File("\\test/hold/REPO/TEST/Letter/123.pdf");
2) File f=new File("\\test\\hold\\REPO/TEST/Letter/123.pdf");
3) File f=new File("\\test\\hold\\REPO\\TEST\\Letter\\123.pdf");
4) File f=new File("/test/hold/REPO/TEST/Letter/123.pdf");
you can use :-
InputStream input = new URL("\\test\hold\REPO/TEST/Letter/123.pdf").openStream();
or
File file = new File(location);
where location=\\test\hold\REPO/TEST/Letter/123.pdf; and Check Using SOP statement whether URL is Call properly or not . hope it will help you to fine better solution
I have string:
String s = "~/abc/d.png"
How can I get absolute path of this file?
I've tried:
File file = new File(s);
String absolutePath = file.getAbsolutePath();
But it can't reslove ~ symbol.
Java isn't going to know what the ~ means since it's a shell expansion for your home directory. You can do this before handing it off to File:
s = s.replace("~",System.getProperty("user.home"));
I am trying to get the absolute path of working directory, and pass it to a method.
I am getting the path in the form
C:\eclipse\workspace\file.txt
but eclipse must receive it in the form:
C:\\eclipse\\workspace\\file.txt
So I am doing a replace, but it works only for one char, how to modify it?
String path = new File("").getAbsolutePath();
path = path.replace( '\\', '\\\\' );
something = method.read(path+"\\file.txt");
Change this:
path = path.replace( '\\', '\\\\' );
To this:
path = path.replaceAll( "\\", "\\\\" );
try using the replaceAll method.
Just use double quotes to represent more than one char. It's then called a String.
path = path.replace("\\", "\\\\");
Note that this requires a minimum of Java 1.5 to work.
See also:
The Java Tutorials - Learning the language - Strings
You should use:
public String replaceAll(String regex,
String replacement)
Replaces each substring of this string
that matches the given regular
expression with the given replacement.
If you just want the current working directory, the simplest approach is this:
String currentdir = System.getProperty("user.dir");
you can use path = path.replaceAll( '\\', '/' ); it works
Not sure if this will help.. I ran into the same issue with the application I am building.
In my app, I basically use JFileChooser to graphically select the file I want. Long story short, the absolute path of the selected file comes back as "C:\Documents and Settings....\xCBL.xml"
The path was then passed to another class for manipulation, however it failed because of the single '\'. The fix -
String absPath = file.getAbsolutePath();
System.out.println("File Path:"+absPath);
String filePath = absPath.replaceAll("\\\\", "\\\\\\\\");
System.out.println("New File Path:"+filePath);
Output:
File Path:C:\Documents and Settings\tanzim.hasan\workspace\XML to Java\xcbl.XML
New File Path:C:\\Documents and Settings\\tanzim.hasan\\workspace\\XML to Java\\xcbl.XML
Hope the above helps.
I've this brief snippet:
String target = baseFolder.toString() + entryName;
target = target.substring(0, target.length() - 1);
File targetdir = new File(target);
if (!targetdir.mkdirs()) {
throw new Exception("Errore nell'estrazione del file zip");
}
doesn't mattere if I leave the last char (that is usually a slash). It's done this way to work on both unix and windows. The path is actually obtained from the URI of the base folder. As you can see from baseFolder.toString() (baseFolder is of type URI and is correct). The base folder actually exists. I can't debug this because all I get is true or false from mkdir, no other explanations.The weird thing is that baseFolder is created as well with mkdir and in that case it works.
Now I'm under windows.
the value of target just before the creation of targetdir is "file:/C:/Users/dario/jCommesse/jCommesseDB"
if I cut and paste it (without the last entry) in windows explore it works...
The path you provide is not a file path, but a URI.
I suggest you try the following :
URI uri = new URI("file://c:/foo/bar");
File f = new File(uri).
It looks, to me, as if the "file:/" at the beginning is the problem... Try getAbsolutePath() instead of toString().
The File constructor taking a String expects a path name. A path name is not an URI.
Remove the file:/ from the front of the String (or better yet, use getPath() instead of toString()) to get to the path you need.