regex for getting the fileName - java

I am having a file name like suppose abcde-1_Transformed.xml.
Now if I have many number of files suppose in the above file name the '1' which is present has files like
abcde-2_Transformed.xml.
abcde-3_Transformed.xml.
abcde-4_Transformed.xml.
till abcde-1966_Transformed.xml.
How to get the file names which are not present in the total number of files using regex ?

I think rather than making a regex you can just read the file in the current directory using this link Read all files in a folder
Then parse out the number from file name and check if it's present or not.

Related

URI for a directory path only?

Is it possible to define a URI representing a directory path only (i.e. with no actual file terminating the URI)? Example: file:///path/to/files.
I have the need to specify paths to directories to a Java program and would like to specify them as a URI rather than just a String directory path. The program receiving this information would treat the URI as a directory specifier and expect to read whatever files are contained within. It does not care what the file names are.
UPDATE:
If this is allowed, would I be able to "open" the URI in order to get the listing of files within, and then be able to open them for reading?
Yes it is possible.
here's an exemple
lets say myfolder is a folder that contains 3 files (file1.txt, file2.txt, file3.txt).
URI uri = new URI("file:///c:/myfolder/");
File folder =new File(uri);
for(File file : folder.listFiles()){
System.out.println(file.getName());
}
the output will be :
file1.txt
file2.txt
file3.txt

Java File - Append pre-defined filename to a user-defined directory

I have a program that creates multiple output files e.g. daily_results.txt, overall_results.txt etc.
I want to allow the user to specify the directory that these files will be saved to using JFileChooser.
So if the user selected the directory they wanted their output to be saved to as "C:\temp\". What is the best way to append daily_results.txt to that file object. Is there a more elegant way to do this other than:
File file = new File(userDirectory.getPath() + "daily_results.txt");
Any ideas?
Apologies!
I think this can quite easily be accomplished with the JFileChoosers setSelectedFile method.

java FilenameFilter regex (look ahead)

I am trying to filter files using FilenameFilter to grep files files in a directory.
% ls -1
DirFilter.class
DirList.class
DirList.java
doctors.txt
node.l
rels.l
I am trying to filter node.l and rels.l. Filter should succeed if and only if both files are present.
I tried my regex on debuggex.com and it seems to work as expected :
http://www.debuggex.com/embed/CZgVeUE2iWsNfRNG
my regex : (?s)node.l.?(?=(rels.l))
but when I run it through DirList.java filter it doesn't work..
% java DirList "(?s)node.l.?(?=(rels.l))"
<no-output>
Now I am using DirList.java from Thinking in Java
http://www.cs.odu.edu/~cs476/tijava2/c10/DirList.java
Any ideas?
DirList is evaluating your regex against each file name separately, not as a single \n delimited directory listing string as returned by ls. Your regex will never match under those conditions since it never sees more than one file name at a time.
FilenameFilter works for single file not for groups of files so regex will be applied only to that single file. Instead of regex try maybe this way:
take name of file
if it is node.l check if new File(currentDir+"/rels.l").exists().
if it is rels.l check if new File(currentDir+"/node.l").exists().

Writing string to a file in java

I have a file which contains sql statements.
Now in this file, I want to add a word "collate" after every 'char()' and 'varchar()' in the file.
How do you do that?
Iterate through the file line by line. On each String do two replaceAll( ... ) using your Strings above. Then write each line into a new File. When done, rename the original file to some back-up name and rename the new file to the original file's name.
Edit 1
I just noticed your javascript tag. So what type of problem is this, Java or Javascript?

How to save a file with an incrementing suffix - file1, file2, etc

Scenario:
I save my drawing with a file name as picture. After a while I made some changes on the file picture and save it again.
Since both file have the same name, is it possible that the new file automatically saved as picture1 without need to manually change the file name in the program? ... I means automatically add number at the end of the file name if the file have the same name?
so at the end if I made changes on the files so many times, I will have many files named as picture, picture1, picture2, picture3...
You could use the create temp file method for this, use:
as prefix the basename of your file, in this case it would be "picture"
as suffix the image type, for instance ".png"
The file created will be unique by definition.
Another way is to create a unique filename based on the current time, as in:
SimpleDateFormat fmt = new SimpleDateFormat("picture_yyyyMMdd_HHmmss.png");
String filename = fmt.format(new Date());
This would give you meaningfull filenames with regards to edit history.
Sure, if you program it so. If your desired filename exists check to see if a file with the same name, with an increasing integer starting at 1, exists. Once you find one that doesn't exist, use it as the name. Make sure to do the right thing with file extensions (you probably want file2.txt, not file.txt.2).
if filename exists
{
loop suffix from 1 to some limit
{
if filename + suffix doesn't exist
{
exit loop and use this name
}
}
}

Categories

Resources