我想从文件中读取字符串。当找到某个字符串(><
)时,我想开始读取整数,然后将它们转换为二进制字符串。
我的程序正在读取字符串并将其成功保存到ArrayList
中,但是
它不能识别><
符号,因此无法成功读取二进制字符串。
代码
try {
FileInputStream fstream = new FileInputStream(fc.getSelectedFile().getPath());
// Get the object of DataInputStream
DataInputStream ino = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(ino));
String ln;
String str, next;
int line, c =0;
while ((ln = br.readLine()) != null) {
character = ln;
System.out.println(character);
iname.add(ln); // arraylist that holds the strings
if (iname.get(c).equals("><")) {
break; // break and moves
// on with the following while loop to start reading binary strings instead.
}
c++;
}
String s = "";
// System.out.println("SEQUENCE of bytes");
while ((line = ino.read()) != -1) {
String temp = Integer.toString(line, 2);
arrayl.add(temp);
System.out.println("telise? oxii");
System.out.println(line);
}
ino.close();
} catch (Exception exc) { }
我尝试读取的文件例如:
T
E
a
v
X
L
A
.
x
"><"
sequence of bytes.
最后一部分另存为字节,并在文本文件中显示为这样。不用担心,此位有效。所有字符串都保存在新行中。
最佳答案
您应该做的是测试ln是否等于>,然后测试下一个字符是否等于
你必须要小心
关于java - 从字符串文件读取时,如何识别特殊的分隔符字符串?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5354107/