I have a .sh script with a property=value. Let it be:
some_property="some value"
The value of the property is used along the script and script is launched in Java code. I want to dynamically change this property's value. I tried to use replaceFirst() method, but I don't know the actual value of "some_property" to replace it correctly using regexp.
How can I edit a .sh file with replacing a single line that starts with "some_property=" by some_property=my_value? By the way there're several places in a file where pattern "some_property=" can be met, so I need to change the first occurrence.
You should be fine with a regex:
line.replaceFirst("some_property=.*$", "some_property=\"" + your_value + "\"");
You can get position of the = and \n using String.indexOf(int) and then replace the string between = and \n using, for example, replace(CharSequence, CharSequence).
Btw - some_property=(.+) (and replacing $1) wouldn't be okay? :)
Related
I want some code in my program to run only if the user has input the character '*' at the command-line as a command-line argument. This is the code I've used:-
//myfile.java
import java.io.*;
public class myfile {
public static void main(String[] args) {
if(args[0].equals("*")){
//do stuff
System.out.println(args[0]);//added this line to see what exactly was being passed
}
}
}
When this program is executed at the command-line by entering:-
java myfile *
the output I'm expecting to see on the screen is the asterisk character, instead the output displayed is 'myfile.class'. Where am I going wrong? Why does Java change the asterisk to the .class file?
Also, note that the program worked perfectly the first four times I executed it and then started doing this!
Thanks in advance for your help.
Where am I going wrong?
The star character needs to be quoted or escaped. Run your java program like this:
java myfile "*"
or
java myfile \*
Why does Java change the asterisk to the .class file?
It doesn't. It is your shell that is doing it. It is shell file expansion ... or "globbing" as it is also called.
Run "ls *" or "echo *" and you will see that the same thing happens.
The command terminal already replaces the asterisk and java already gets the value that you see. I'd use any other character, that has no special meaning to the command terminal or otherwise you must escape the asterisk in your command.
Actually escaping arguments on Windows and especially in cmd.exe is non-trivial. This nice article explains it in detail: Everyone quotes command line arguments the wrong way :
the takaway for your case is: surround the asterisk with quotes.
Answer to your question in the comment:
Using the escape character worked! But I still don't get why it worked without the escape character the first few times
I am not sure, but maybe you run into this behavior: It makes a difference if the pattern can be expanded or not. For example, when I pass Test* as argument, then there are 2 cases to consider:
in the current folder there is a file called Test1.txt: then your java program will get Test1.txt as argument
when there are no matching files, your program will get Test* as argument
However, I am not sure, how this would apply to your case, since you only pass *: that should only work in an empty directory.
The return value of a value from getPath() from the File Class is something like this
"C:\Users\Daniel\Desktop\ASDF.mp3".
To use the Desktop class from java to play a file, the path would have to be fed into a file, with a path similar to
"C:\\Users\\Daniel\\Desktop\\ASDF.mp3"
Since the \ is a reserved character(From my understanding) to make a new file you must use a double backslash to dictate that it is a file. My problem is that when I try to get the path I need to transform it into a double slash version. The .replaceAll() method doesn't allow for '\' since it's a reserved character but the .replace() method does.
To work around this would I just have to loop through to find all instances and replace them one at a time? Or is there a simpler work around? Also I would like to know if I am receiving this error due to it being a reserved character, or if I am completely wrong.
The two strings above are actually exactly the same.
When Java or other language outputs a string it only displays one slash '\', but when you are typing the string into double quotes you need double backslash '\\' so the Java parser knows it's one slash. Backslash is used for many other escape characters, so this is only way parser will know.
(Even when typing this answer, I needed 4 backslashes to make only 2!
i want to extract these Strings (XXXXX,GGGGG,PPPPP) from this Strings:
COPY XXXXX,PFX='PPPPP';
COPY XXXXX,PFX='PPPPP',GRUPPE='GGGGGG';
COPY XXXXX;
COPY XXXXX,'PPPPP';
COPY 'XXXXX','PPPPP','GGGGG';
COPY 'XXXXX','PPPPP',SUPPR='YES';
COPY XXXXX,PPPPP,GGGGG;
My problem is, that all these strings are different and i can't extract them. For every singel string i can do a regex, but not for all in one method.
xxxx can be e.g. TWT000
PPPP can be e.g. TWS000
GGGG can be e.g. TWSOOO
Any chance of getting all string types in one method to extract XXXX,PPPP,GGGG?
Split every string by space, then split by coma(,). For example(COPY XXXXX,PFX='PPPPP',GRUPPE='GGGGGG';):
first step:
COPY
XXXXX,PFX='PPPPP',GRUPPE='GGGGGG';
next step:
XXXXX
PFX='PPPPP'
GRUPPE='GGGGGG';
each line is a cell in array after splitting. Use some ifs to make regexp check or something if you must, for example in SUPPR='YES'; you don't want to parse YES.
Now extract everything inside '' quotations.
You have successfully extract your data.
use batch files and cmd commands you will find alot of helpful functions there
#echo off
findstr /m /c:%1 %2
if %errorlevel%==0 (
echo found
)
%1 is the string you are looking for
%2 is the text file you are searching into
and you can output the result into another file with ">"
Can anyone tell me how to cope with illegal file names in java? When I run the following on Windows:
File badname = new File("C:\\Temp\\a:b");
System.out.println(badname.getAbsolutePath()+" length="+badname.length());
FileWriter w = new FileWriter(badname);
w.write("hello world");
w.close();
System.out.println(badname.getAbsolutePath()+" length="+badname.length());
The output shows that the file has been created and has the expected length, but in C:\Temp all I can see is a file called "a" with 0 length. Where is java putting the file?
What I'm looking for is a reliable way to throw an error when the file can't be created. I can't use exists() or length() - what other options are there?
In that particular example, the data is being written to a named stream. You can see the data you've written from the command line as follows:
more < .\a:b
For information about valid file names, look here.
To answer your specific question: exists() should be sufficient. Even in this case, after all, the data is being written to the designated location - it just wasn't where you expected it to be! If you think this case will cause problems for your users, check for the presence of a colon in the file name.
I would suggest looking at Regular Expressions. They allow you to break apart a string and see if certain characteristics apply. The other method that would work is splitting the String into a char[], and then processing each point to see what's in it, and if it's legal... but I think RegEx would work much better.
You should take a look at Regular Expressions and create a pattern which will match any illegal character, something like this:
String fileName = "...";
Pattern pattern = Pattern.compile("[:;!?]");
Matcher matcher = pattern.match(fileName);
if (matcher.find())
{
//Do something when the file name has an illegal character.
}
Note: I have not tested this code, but it should be enough to get you on the right track. The above code will match any string which contains a :, ;, `!' and '?'. Feel free to add/remove as you see fit.
You can use File.renameTo(File dest);.
Get the file name first:
String fileName = fullPath.substring(fullPath.lastIndexOf('\\'), fullPath.length);
Create an array of all special chars not allowed in file names.
for each char in array, check if fileName contains it. I guess, Java has a pre-built API for it.
Check this.
Note: This solution assumes that parent directory exists
I'm currently writing something which is validating our vbscript files. Right at the start I wish to remove all lines of code which are comments. I was expecting to be able to use the "'" (comment symbol in vbscript) and '\n'. However, when I write the content of the file to screen, the new lines are not formatting. Does this mean there are actually no new lines in the original vbscript file and if not, how could I remove comments?
first read whole file in string example
then use regex or simply substring for removing extra syntax
How are you parsing the file? Are you also taking the '\r' into consideration when removing the comments? Or maybe you are accidentally removing all newline characters.
I would create some state flags to tell the parser when I was in a comment or not.