Hi I have a file stored in Linux system that contains a special character ^C Something like this:
ABCDEF^CIJKLMN Now i need to read this file in java and detect that there is this ^C to make split. The problem that to read the file in UNIX.I must use cat -v fileName to see the special chracter ^C elsewhere i can't see it. This is my sample code.
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(this),
Charset.forName("UTF-8"));
BufferedReader br = new BufferedReader(inputStreamReader);
String line;
while ((line = br.readLine()) != null) {
if (line.contains("^C")) {
String[] split = line.split("\\" + sepRecord);
System.out.println(split);
}
You are checking if the line contains the String "^C", not the character '^C' (which corresponds to 0x03
, or \u0003
). You should search for the character 0x03
instead. Here's a code example that would work in your case:
byte[] fileContent = new byte[] {'A', 0x03, 'B'};
String fileContentStr = new String (fileContent);
System.out.println (fileContentStr.contains ("^C")); // false
System.out.println (fileContentStr.contains (String.valueOf ((char) 0x03))); // true
System.out.println (fileContentStr.contains ("\u0003")); // true, thanks to @Thomas Fritsch for the precision
String[] split = fileContentStr.split ("\u0003");
System.out.println (split.length); // 2
System.out.println (split[0]); // A
System.out.println (split[1]); // B
The ^C
character is displayed in Caret Notation, and must be interpreted as a single character.