Cannot replace ${} variable in Java - java

I have a function in which I am trying to replace ${schema} in a file with a variable pass into the parameter.
So for example
sample.sql
insert into ${schema}.table values (1,2,3);
What i want to achieve when running this function is
sample.sql
insert into public.table values (1,2,3);
So my function does below
public static void setvariable_java(String scriptName, String replaceString){
File ftmp = new File("./src/tempsqlStatements/" + scriptName + ".sql");
String pattern = "\\$\\{schema\\}";
try{
BufferedReader br = new BufferedReader(new FileReader(ftmp));
String ln;
while((ln = br.readLine()) != null)
{
ln = ln.replace("${schema}", "");
System.out.println(ln);
}
}catch (Exception s){
s.printStackTrace();
}
}
And I am running the function
setvariable_java("sample","public")
However, in my sql file, the ${schema} is not getting replaced by sample and it is still giving me when i println.
insert into ${schema}.table values (1,2,3);
Is there anything Im doing wrong?
Edit: I am able to successfully write to the file using filewriter.
public static void setvariable_java(String scriptName, String searchString, String replaceString){
FileWriter writer = null;
String oldContent = "";
try{
BufferedReader br = new BufferedReader(new FileReader(ftmp));
String ln;
while((ln = br.readLine()) != null)
{
ln = ln.replace("${schema}", replaceString);
//System.out.println(ln);
oldContent = oldContent + ln + System.lineSeparator();
}
System.out.println(oldContent);
writer = new FileWriter (ftmp);
writer.write(oldContent);
writer.close();
}catch (Exception s){
s.printStackTrace();
}
}

Do not use regex replace when you don't need regex!
ln = ln.replace("${schema}", replaceString);
Notes:
replace() still replaces all instances, but does a plain text match (not a regex match)
you must assign the result of the replace to the variable. Strings are immutable - replace() (or replaceAll()) returns a new String.

Related

File to String Null Pointer Exception

I'm trying to pass the contents of a file into a method as a String and encountering a Null pointer exception. I'm converting the file into a String like so:
import java.io.*;
public class FileHandler {
String inputText = null;
public String inputReader() {
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("in.txt"))));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
String inputText = sb.toString();
//System.out.println(inputText);
}
br.close();
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
}
return inputText;
}
}
This is working fine for me in terms of converting the file to a String, but when I try and pass the output of this into another method I get a null pointer exception here:
char[][] railMatrix = new char[key][inputText.length()];
I copied the contents of the file and passed it in as a normal String like so;
String plain = "The text from the file"
int key = 5;
int offset = 3;
String encrypted = rf.encrypt(plain, key, offset);
System.out.println(encrypted);
String unencrypted = rf.decrypt(encrypted, key, offset);
System.out.println(unencrypted);
And it worked fine. But
String plain = fh.inputReader();
Does not.
So inputReader() seems to work, the methods it's being passed into work, but I'm clearly missing something.
A nod in the right direction would be greatly appreciated, thanks friends.
Your result is stored in a local variable "inputText" and you are returning instance level variable which is null and is never reassigned. Remove String type as below and it should work:
while ((line = br.readLine()) != null) {
sb.append(line);
inputText = sb.toString();
//System.out.println(inputText);
}

Java - write string to file line by line vs one-liner / cannot convert String to String[]

Relatively new to programming. I want to read a URL, modify the text string, then write it to a line-separated csv textfile.
The read & modify parts run. Also, outputting the string to terminal (using Eclipse) looks fine (csv, line by line), like this;
data_a,data_b,data_c,...
data_a1,data_b1,datac1...
data_a2,data_b2,datac2...
.
.
.
But I'm unable to write the same string to file - it just becomes a one-liner (see my below for-loops, attempts no. 1 & 2);
data_a,data_b,data_c,data_a1,data_b1,datac1,data_a2,data_b2,datac2...
I guess I'm looking for a way to, in the FileWriter or BufferedWriter loops, convert the string finalDataA to array string (i.e. include the string suffix "[0]") but I have not yet found such an approach that would not give errors of the type "Cannot convert String to String[]". Any suggestions?
String data = "";
String dataHelper = "";
try {
URL myURL = new URL(url);
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
if (myConnection.getResponseCode() == URLStatus.HTTP_OK.getStatusCode()) {
BufferedReader in = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));
while ((data = in.readLine()) != null) {
dataHelper = dataHelper + "\n" + data;
}
in.close();
String trimmedData = dataHelper.trim().replaceAll(" +", ",");
String parts[] = trimmedData.split(Pattern.quote(")"));// ,1.,");
String dataA = parts[1];
String finalDataA[] = dataA.split("</PRE>");
// parts 2&3 removed in this example
// Console output for testing purpose - This prints out many many lines of csv-data
System.out.println(finalDataA[0]);
//This returns the value 1
System.out.println(finalDataA.length);
// Attempt no. 1 to write to file - writes a oneliner
for(int i = 0; i < finalDataA.length; i++) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(pathA, true))) {
String s;
s = finalDataA[i];
bw.write(s);
bw.newLine();
bw.flush();
}
}
// Attempt no. 2 to write to file - writes a oneliner
FileWriter fw = new FileWriter(pathA);
for (int i = 0; i < finalDataA.length; i++) {
fw.write(finalDataA[i] + "\n");
}
fw.close();
}
} catch (Exception e) {
System.out.println("Exception" +e);
}
Create the BufferedWriter and the FileWriter ahead of the for loop, not every time around it.
From your code comments, finalDataA has one element, so the for-loop will be executed only once. Try splitting finalDataA[0] into rows.
Something like this:
String endOfLineToken = "..."; //your variant
String[] lines = finalDataA[0].split(endOfLineToken)
BufferdWriter bw = new BufferedWriter(new FileWriter(pathA, true));
try
{
for (String line: lines)
{
bw.write(line);
bw.write(endOfLineToken);//to put back line endings
bw.newLine();
bw.flush();
}
}
catch (Exception e) {}

How to remove the duplicate string?

In my code I have two files in my drive those two files have some text and I want to display those string in the console and also remove the repeated string and display the repeated string once rather than displaying it twice.
Code:
public class read {
public static void main(String[] args) {
try{
File file = new File("D:\\file1.txt");
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while((line = br.readLine()) != null){
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Contents of file1:");
String first = stringBuffer.toString();
System.out.println(first);
File file1 = new File("D:\\file2.txt");
FileReader fileReader1 = new FileReader(file1);
BufferedReader br1 = new BufferedReader(fileReader1);
StringBuffer stringBuffer1 = new StringBuffer();
String line1;
while((line1 = br1.readLine()) != null){
stringBuffer1.append(line1);
stringBuffer1.append("\n");
}
fileReader1.close();
System.out.println("Contents of file2:");
String second = stringBuffer1.toString();
System.out.println(second);
System.out.println("answer:");
System.out.println(first+second);
}catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
Output is:
answer:
hi hello
how are you
hi ya
i am fine
But I want to compare both the strings and if the same string repeated then that string should be displayed once.
Output I expect is like this:
answer:
hi hello
how are you
ya
i am fine
Where the "hi" is found in both the strings so that I need to delete the one duplicate string.
How can I do that please help.
Thanks in advance.
You can pass your lines through this method to parse out duplicate words:
// store unique previous words
static Set<String> words = new HashSet<>();
static String removeDuplicateWords(String line) {
StringJoiner sj = new StringJoiner(" ");
// split on whitespace to get distinct words
for (String word : line.split("\\s+")) {
// try to add word to the set
if (words.add(word)) {
// if the word was added (=not seen before), append to the result
sj.add(word);
}
}
return sj.toString();
}

Replace a String inside a file using java

I have a TXT file in which I'd like to change this String
<!DOCTYPE Publisher
PUBLIC "-//Springer-Verlag//DTD A++ V2.4//EN" "http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd">
into this one <!DOCTYPE Publisher> using Java.
I wrote the following function but it seems not to be working.
public void replace() {
try {
File file = new File("/home/zakaria/Bureau/PhD/test2/file.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
while((line = reader.readLine()) != null) {
oldtext += line + "\n";
}
reader.close();
String newtext = oldtext
.replaceAll("<!DOCTYPE Publisher\nPUBLIC \"-//Springer-Verlag//DTD A++ V2.4//EN\" \"http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd\">",
"<!DOCTYPE Publisher>");
FileWriter writer = new FileWriter("/home/zakaria/Bureau/PhD/test2/file.txt");
writer.write(newtext);
writer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
What did I do wrong?
Try this simple code:
public static void replace() {
try {
File file = new File("resources/abc.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "", oldtext = "";
boolean found = false;
while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("<!DOCTYPE Publisher")) {
found = true;
}
if (line.trim().endsWith("A++V2.4.dtd\">")) {
oldtext += "<!DOCTYPE Publisher>";
found = false;
continue;
}
if (found) {
continue;
}
oldtext += line + "\n";
}
reader.close();
FileWriter writer = new FileWriter("resources/file.txt");
writer.write(oldtext);
writer.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
You are fortunate to start with that it didn't change anything at all.
Otherwise you'd have lost your original file...
Never modify a file in place!!
Create a temporary file where you write the modified content, and only then rename to your original file.
Also, the string you want to replace is pretty complicated, and you don't want to use .replace() since this will replace all occurrences.
Do like this:
final String quoted
= Pattern.quote("<!DOCTYPE Publisher\nPUBLIC \"-//Springer-Verlag//DTD A++ V2.4//EN\" \"http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd\">");
final Pattern pattern = Pattern.compile(quoted);
final Path victim = Paths.get("/home/zakaria/Bureau/PhD/test2/file.txt");
final Path tmpfile = Files.createTempFile("tmp", "foo");
final byte[] content = Files.readAllBytes(victim);
final String s = new String(content, StandardCharsets.UTF_8);
final String replacement = pattern.matcher(s).replaceFirst("<!DOCTYPE Publisher>");
try (
final OutputStream out = Files.newOutputStream(tmpfile);
) {
out.write(replacement.getBytes(StandardCharsets.UTF_8));
out.flush();
}
Files.move(tmpfile, victim);
If the text you want to eliminate is on the second and subsequent lines, as in your demo-input
<!DOCTYPE Publisher
PUBLIC "-//Springer-Verlag//DTD A++ V2.4//EN"
"http://devel.springer.de/A++/V2.4/DTD/A++V2.4.dtd">
and no lines between the first and last in the tag contain a closing >, then you can do the following:
while(more lines to process)
if "<!DOCTYPE Publisher" is not found
read line and output it
else
//This is the first line in a <!DOCTYPE tag
read the line and output it, appending '>' to the end
while the next line does NOT end with a '>'
discard it (don't output it)
Try with this regexp:
String newtext = oldtext.replaceAll(
"<!DOCTYPE Publisher\nPUBLIC \"-\\/\\/Springer-Verlag\\/\\/DTD A[+][+] V2[.]4\\/\\/EN\"[ ]\"http:\\/\\/devel[.]springer[.]de\\/A[+][+]\\/V2[.]4\\/DTD\\/A[+][+]V2[.]4[.]dtd\">", "<!DOCTYPE Publisher>");
The only changes are escaping forward slashes and putting dots and plus signs between square brackets.

Java String Replacement

I have a string like this in a file
<script>
Evening</script>
I have written a code to replace this string but it's not identifying the newline character
i,e. I want to replace above string with:
<h1>Done</h1>
code goes like this:
package stringreplace;
import java.io.*;
import org.omg.CORBA.Request;
public class stringreplace {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr = null;
BufferedReader br = null;
try
{
fr = new FileReader("G://abc.html");
br = new BufferedReader(fr);
String newtext="";
String line="";
String matchExist1 = "<script>\r\nEvening</script>";
String newpattern = "<h1>Done</h1>";
String matchExist2 = "</body>";
String newpattern2 = "<script>alpha</script></body>";
StringBuffer sb = new StringBuffer();
while((line=br.readLine())!=null)
{
int ind2 = line.indexOf(matchExist1);
System.out.println(ind2);
int ind3 = line.indexOf(matchExist2);
if((ind2==-1) || (ind3==-1))
{
line = line.replaceFirst(matchExist1,newpattern);
line = line.replaceFirst(matchExist2,newpattern2);
sb.append(line+"\n");
}
//sb.append(line+"\n");
else if((ind2!=-1) || (ind3!=-1))
{
String tag = "</body>";
line = line.replaceFirst("</body>",tag);
sb.append(line+"\n");
}
}
br.close();
FileWriter fw = new FileWriter("G://abc.html");
fw.write(sb.toString());
fw.close();
System.out.println("done");
System.out.println(sb);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
But it is not identifying newline character.
Since you are reading only one input line at a time you can hardly expect to match a pattern that spans two lines.You must first fix your read to have a least two lines in it. Once you've done that, #sterna's answer will do the trick
I think you can't be sure about how your newline looks like. So I would not match for a specific sequence instead use \s+ this is at least one whitespace character and all newline characters are included.
String matchExist1 = "<script>\\s+Evening</script>";
Edit:
Of course, you have to fix at first the problem mgc described (+1). And then you can make use of my answer!

Categories

Resources