从字符串文件读取时如何识别特殊分隔符字符串?

发布于 2024-10-23 00:28:11 字数 1383 浏览 2 评论 0原文

我想从文件中读取字符串。当找到某个字符串 (><) 时,我想开始读取整数,并将它们转换为二进制字符串。

我的程序正在成功读取字符串并将它们保存在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み零 2024-10-30 00:28:11

<是两个字符,而 iname.get(c) 只有一个字符。

你应该做的是测试 ln 是否等于 >然后再次测试下一个字符是否等于 < 。如果两个测试都通过,则跳出循环。

你必须要小心

< is two characters and iname.get(c) is only one character.

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

临风闻羌笛 2024-10-30 00:28:11

使用扫描程序。它允许您指定分隔符,并具有将输入标记读取为 String 或 int 的方法。

Use a Scanner. It allows you to specify a delimiter, and has methods for reading input tokens as String or int.

久而酒知 2024-10-30 00:28:11

你能不能做类似的事情:

while ((ln = br.readLine()) != null){
            character=ln;
            System.out.println(character);

            //
            // Look for magic characters >< and stop reading if found
            //

            if (character.indexOf("><") >= 0) {
               break;
            }

            iname.add(ln);
    }

如果你不想将魔法符号添加到你的ArrayList中,这会起作用。您的代码示例不完整 - 如果您仍然遇到问题,则需要发布整个课程。

Could you not do something like:

while ((ln = br.readLine()) != null){
            character=ln;
            System.out.println(character);

            //
            // Look for magic characters >< and stop reading if found
            //

            if (character.indexOf("><") >= 0) {
               break;
            }

            iname.add(ln);
    }

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文