[英]How can i check a Line contains a special chracter?
Hi I have a file stored in Linux system that contains a special character ^C Something like this: 嗨,我有一个存储在Linux系统中的文件,其中包含特殊字符^ C,如下所示:
ABCDEF^CIJKLMN Now i need to read this file in java and detect that there is this ^C to make split. ABCDEF ^ CIJKLMN现在,我需要在Java中读取此文件,并检测是否存在要拆分的^ C。 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.
在UNIX中读取文件的问题。我必须使用cat -v fileName在其他我看不见的地方看到特殊的ch ^ ^ C。 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 \
). 您正在检查该行是否包含字符串 “ ^ C”,而不是字符 “ ^ C”(对应于
0x03
或\
)。 You should search for the character 0x03
instead. 您应该搜索字符
0x03
。 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. ^C
字符以脱字符号显示,并且必须解释为单个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.