I have the below code and I am trying to extract all the pieces of data from the file "file.txt". Currently this file has only one line:
id-123:value 123
package demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class s {
public static void main(String[] args) throws Exception {
final String regex = ":[^\\d].*";
File file = new File("C:\\Users\\user\\Desktop\\file.txt");
String text, id;
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
text = line.replaceAll("(^id-\\d*):+", "");
id = line.replaceAll(":\\S.*", "");
System.out.println(text);
System.out.println(id);
}
}
}
I am able to read the file and get this line correctly, but when i print it on console, I get the below output:
?id-123:value 123
?id-123
Where does the question mark come from? The text file is saved as UTF-8 file, and reading is also UTF-8. Trying to run it in eclipse.
Also, while running this line of code, I get proper output value 123
:
System.out.println(string.replaceAll("(^id-\\d*):+", ""));
But the same regex for same line when read from file, it prints:
?id-123:value 123