Regular expression and AwkFilenameFilter - java

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.

Related

Issues writing text file to IFS though jt400

All I'm trying to do is to drop a log on IFS
Here is my code:
def write(target_filename, data)
stream = com.ibm.as400.access.IFSFileOutputStream.new(AS400.sys, target_filename)
stream.write(data.to_java_bytes)
stream.flush
stream.close
end
When i read it though the jt400 library, it comes out ok.
But when i go thought the qShell or wrklnk the file seems empty.
Any ideas why? Is it the CCID?
Found the issue. I was using IFSFileOutputStream to write binary stream of text. By switching it to IFSTextFileOutputStream, problem was resolved.

Java regex to replace file path based on OS

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"));

Java - How to get the name of a file from the absolute path and remove its file extension?

I have a problem here, I have a String that contains a value of C:\Users\Ewen\AppData\Roaming\MyProgram\Test.txt, and I want to remove the C:\Users\Ewen\AppData\Roaming\MyProgram\ so that only Test is left. So the question is, how can i remove any part of the string.
Thanks for your time! :)
If you're working strictly with file paths, try this
String path = "C:\\Users\\Ewen\\AppData\\Roaming\\MyProgram\\Test.txt";
File f = new File(path);
System.out.println(f.getName()); // Prints "Test.txt"
Thanks but I also want to remove the .txt
OK then, try this
String fName = f.getName();
System.out.println(fName.substring(0, fName.lastIndexOf('.')));
Please see this for more information.
The String class has all the necessary power to deal with this. Methods you may be interested in:
String.split(), String.substring(), String.lastIndexOf()
Those 3, and more, are described here: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
Give it some thought, and you'll have it working in no time :).
I recommend using FilenameUtils.getBaseName(String filename). The FilenameUtils class is a part of Apache Commons IO.
According to the documentation, the method "will handle a file in either Unix or Windows format". "The text after the last forward or backslash and before the last dot is returned" as a String object.
String filename = "C:\\Users\\Ewen\\AppData\\Roaming\\MyProgram\\Test.txt";
String baseName = FilenameUtils.getBaseName(filename);
System.out.println(baseName);
The above code prints Test.

BaseX Database Add in Java

I am quite new to developing using the BaseX api, and I keep running into some trouble with one of the methods. I have a database created and opened, but when I try to use the Add method it throws an exception. See this page for an example from BaseX.
I have created the Database in another section of the code, and the file I am trying to add also exists on my computer. My snippet of code looks like this:
//Opens the database
new Open(databaseName).execute(context);
//adds file to database
new Add("", directoryPath + indexName + "/" + catalog.getInternalID() + ".Catalog.xml").execute(context);
The error I am getting is this:
org.basex.core.BaseXException: "~/cdsp.Catalog.xml" (Line 1):
whitespace expected, attribute name found.
I do not really know what this means, when I try to add other xml files to the database they work and I have not found much googleing this exception. Any help would be much appreciated. Thanks!
I am not sure if you are still running in this problem.
Anyway it looks like your XML file contains an attribute where white space is expected.
You could try opening cdsp.Catalog.xml in an editor of your choice and see what is there at line 1.
Hope this helps,
Michael

Apache VFS resolveFile with regex

If I have a directory called temp with the following files:
a_file1.jpg
a_file2.jpg
b_file1.jpg
b_file2.jpg
It's possible to get all files like this:
VFS.getManager().resolveFile("temp").getChildren();
But, what I actually want to do is get a_file1.jpg and a_file2.jpg. Maybe like:
VFS.getManager().resolveFile("temp/a*").getChildren();
But this throws an exception:
org.apache.commons.vfs.FileSystemException: Could not list the contents of "temp/a*" because it is not a folder.
So, does anyone know how to resolve a set of files based on a regex with VFS?
You could use the findFiles method, with a FileFilterSelector.
You'll need to create your own FileFilter that accepts the files that match your desired regex.

Categories