从字符串文件读取时如何识别特殊分隔符字符串?
我想从文件中读取字符串。当找到某个字符串 (><
) 时,我想开始读取整数,并将它们转换为二进制字符串。
我的程序正在成功读取字符串并将它们保存在 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.
最后一部分保存为字节并在文本文件中显示为这样。不用担心,这一点有效。所有字符串都保存在新行中。
I want to read strings from a file. When a certain string (><
) is found, I want to start reading integers instead, and convert them to binary strings.
My program is reading the strings in and saving them in an ArrayList
successfully, but
it does not recognise the ><
symbol and therefore the reading of the binary strings is not successful.
The Code
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) { }
The file I'm trying to read is for example:
T
E
a
v
X
L
A
.
x
"><"
sequence of bytes.
Where the last part is saved as bytes and in the textfile appears like that. no worries this bit works. all the strings are saved in a new line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该做的是测试 ln 是否等于 >然后再次测试下一个字符是否等于 < 。如果两个测试都通过,则跳出循环。
你必须要小心
What u should do is test if ln equals > and then another test if the next character equals < . If both test pass then break out of the loop.
you will have to becarefull
使用扫描程序。它允许您指定分隔符,并具有将输入标记读取为 String 或 int 的方法。
Use a Scanner. It allows you to specify a delimiter, and has methods for reading input tokens as String or int.
你能不能做类似的事情:
如果你不想将魔法符号添加到你的ArrayList中,这会起作用。您的代码示例不完整 - 如果您仍然遇到问题,则需要发布整个课程。
Could you not do something like:
This would work if you didn't want to add the magic symbol to your ArrayList. Your code sample is incomplete - if you're still having trouble you'd need to post the whole class.