I'm not very sure there is any regex to replace thoese things:
This is a string value read from a xml file saved through Linux machine
<pcs:message schema="models/HL7_2.5.model"/>
and this is the one saved in Windows machine
<pcs:message schema="model\HL7_2.5.model"/>
This is why the file getting an error in eclipse while exported in Linux and imported in Windows or vise versa.
Is there any regex to find and replace the value(slash and back slash) within String? (not XML parsing) based on working OS?
Thanks in advance
str = str.replaceAll("\\\\|/", "\\"+System.getProperty("file.separator"))
Use the "file.separator" system property and base your regexp on that.
http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
Also see this: File.separator vs FileSystem.getSeparator() vs System.getProperty("file.separator")?
This should take care of fixing slashes:
String str = xml.replaceAll("\\\\|/", System.getProperty("file.separator"));
Related
I have a simple code
String staticDir = f.getCanonicalPath() + "/src/main/webapp/static/";
On Windows it will return for me "C:\Temp/src/main/webapp/static/".
How to force Java to use "/" instead of "\"?
I have tried
System.setProperty("file.separator", "/");
String staticDir = f.getCanonicalPath() + "/src/main/webapp/static/";
but it doesn't solved issue for me.
Thank you!
Just replace the path separator with the expected one:
String dir = f.getCanonicalPath().replace(System.getProperty("file.separator"), "/")
+ "/src/main/webapp/static/";
If you can, favor Java 7+'s java.nio.file.Path. There, the notion of path separator disappears until you call toString(). But very few libraries use this and still use File or even, worse, String.
If the library you're using still does it by the mean of strings, it probably means they're splitting manually on /. Maybe it's time to tell them to upgrade?
In my case it was a bug in the library, that I use - https://github.com/yui/yuicompressor/issues/111 .
That's why all suggested solution didn't solved issue for me.
We have a folder where we dump lot of files. Our program needs to read one of the specific files with the latest version. The file name would be something like "2016-03-04-12-46-48_ABC_123456_1.xml".
Insted of reading all the files and then iterating to find the exact file i have used following code with a regular expression
File folder = new File("C:\\some_folder")
folder.listFiles((FilenameFilter) new AwkFilenameFilter("(\\d){4}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}_ABC_" + <ID_String> +"_(\\d){1,2}"))
But for some reason the reqular expression is not working. Can someone please help with this?
It seems you are missing the file extension in the regex:
(\\d){4}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}_ABC_" + <ID_String> +"_(\\d){1,2}\\.xml
Try out this one.
"\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}_ABC_"+<ID string>+"_\d{1}.xml"
It works perfectly for me.
I want to create a folder, which has a unicode character in its name, using Java within a Matlab code. I tried both mkdir() of java.io.File and forceMKdir() of org.apache.commons.io.FileUtils. However, the unicode character is replaced with two characters, which are its UTF-8 representation, when the folder is created. Any idea? A sample code is below.
feature('DefaultCharacterSet', 'UTF-8')
u0_filename = 'é'
curre_output_dir = java.lang.String(root_meddir).concat(java.lang.String(relative_output_meddir).concat(u0_filename).concat(java.lang.String(prior.slash_symbol)))
test_dir1 = java.io.File(curre_output_dir)
With mkdir():
test_dir1.mkdir()
with mkdirs():
test_dir1.mkdirs()
with forceMkdir():
javaMethod('forceMkdir', 'org.apache.commons.io.FileUtils', test_dir1)
But, all create a folder with the name 'é', which is incorrect. The file system is Ubuntu.
Thanks.
EDIT:
To use the Files.createDirectory(), as suggested by fge, I followed this steps:
Set the variable MATLAB_JAVA to the path to Java 7 JRE:
$ export MATLAB_JAVA=/usr/lib/jvm/java-7/jre/
Start Matlab, and check the Java version:
>> version -java
Java 1.7.0_55-b13 with Java HotSpot(TM) 64-Bit Server VM mixed mode
However, the methods do not seem to properly work:
>> java.nio.file.Files.createDirectory('test')
No method 'createDirectory' with matching signature found for class 'java.nio.file.Files'.
So I am answering the edit from May 9.
The reason why the code does not work at that point is because the wrong signature is being provided as the error message says. You are passing a String to the method when it requires a Path object.
Now, this tutorial illustrates three different ways of creating a Path object. I will summarize because we hate links.
Path path = FileSystems.getDefault().getPath("test"); seems to do what you wish.
Path path = Paths.get("test"); which also would work in your case as it is a shortcut for the item #1.
File f = new File("test");Path path = f.toPath();
Hi, I have a big problem. I'm making a java program and I have to call an exe file in a folder that have whitespace. This program also has 2 arguments that always have whitspace in the path.
Example:
C:\Users\Program File\convert image\convert.exe C:\users\image exe\image.jpeg C:\Users\out put\out.bmp
I have to do this in Windows but i want generalize it for every OS.
My code is:
Runtime run = Runtime.getRuntime();<br/>
String path_current = System.getProperty("user.dir");<br/>
String [] uno = new String[]{"cmd","/c",path_current+"\\\convert\\\convert.exe",path_current+"\\\f.jpeg", path_current+"\\\fr.bmp"};<br/>
Process proc2 = run.exec(uno);<br/>
proc2.waitFor();<br/>
This does not work. I tried removing the String array and inserting a simple String with "\"" before and after the path but that didn't work. How do I resolve this?
you may want to use :
http://commons.apache.org/io/api-1.4/org/apache/commons/io/FilenameUtils.html#separatorsToSystem(java.lang.String)
see also this answer :
Is there a Java utility which will convert a String path to use the correct File separator char?
Remove "cmd" and "/c", and use a single forward slash instead of your triple backslaches.
I have a property file and under that I have define a property called:
config.folder = C:\myfolder\configfolder
now the problem is that when loading properties, this property returns me the vale like this:
C:myfolderconfigfolder
I want to replace this single forward slash with back slash so it return me the correct directory path. I know this is not compliance with Java.String. If the user use double forward slash I am able to convert but how can I convert single slash.
A better approach is to change the slash from backslash to forward slash, like so:
config.folder = C:/myfolde/configfolder
Java knows how to interpret this structure.
Change it to: config.folder = C:\\myfolder\\configfolder
I will suggest that you start using System Properties for this i.e. file.separator
String fileSeparator = System.getProperty("file.separator");
Now say you got the path as :
String str = "C:/myfolder/configfolder";
String fileSeparator = System.getProperty("file.separator");
str= str.replace("/", fileSeparator);
System.out.println(str);
OUTPUT is :
C:\myfolder\configfolder
This approach might help you implement your program in any OS For Example UNIX with "/" as the file separator for different components of the file path, and for WINDOWS with "\" as the file separator for components of the file path.
Hope this might help in some way.
Regards
the best way to play with the file path literal is to use the system properties i.e.string file separator =System.getProperty ("file.separator") then you can replace it with ur slash to get the file path regards