java 使用 ascii 代码检查空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15588574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:10:07  来源:igfitidea点击:

check spaces using ascii code

javacharascii

提问by Sara Kholusi

I'm trying to remove spaces from a file while reading it. I do this by checking the ascii code of the char .. if it is not 127 (space) then print it out.

我试图在阅读文件时从文件中删除空格。我通过检查字符的 ascii 代码来做到这一点 .. 如果它不是 127(空格),则将其打印出来。

What is a better way to think of it? or how to fix it this way?

什么是更好的思考方式?或者如何解决这个问题?

private FileInputStream sc;
private static char input;

public void openFile(){
try{
    sc = new FileInputStream(new File ("D:\Empty.txt"));
    input = (char) sc.read();
    if(input != 127){
        System.out.println(input);
    }
}catch(FileNotFoundException e){
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
}

public static void main(String[] args) {

    ParsingStrings ps = new ParsingStrings();
    ps.openFile();

}

回答by JB Nizet

Why don't you make it much clearer (and obviously correct) by using a char literal?

为什么不通过使用字符文字使它更清晰(并且显然是正确的)?

if (input != ' ')

Also, Streams must be used to read binary data. To read text, you should use a Reader. Read the Java IO tutorial.

此外,必须使用流来读取二进制数据。要阅读文本,您应该使用阅读器。阅读Java IO 教程

And of course, your method shouldn't eat exceptions like that. It should throw them, or at least wrap them in another exception and throw this exception.

当然,你的方法不应该吃这样的异常。它应该抛出它们,或者至少将它们包装在另一个异常中并抛出这个异常。

Finally, the Reader should be a local variable of the openFile()method and not a field. And it should be closed in a finally block, or by using the Java 7's try-with-resources construct.

最后,Reader 应该是方法的局部变量openFile()而不是字段。它应该在 finally 块中关闭,或者通过使用 Java 7 的 try-with-resources 构造来关闭。

回答by Vincent Ramdhanie

You can use a character stream such as FileReader wrapped in a BufferedReader because it reads character by character. I suspect that you will have some issues reading a byte and casting as a character the way you are doing it.

您可以使用包装在 BufferedReader 中的诸如 FileReader 之类的字符流,因为它会逐个字符地读取。我怀疑您在读取字节并按照您的方式将其转换为字符时会遇到一些问题。

    String s;
    BufferedReader in = new BufferedReader(new FileReader("file.txt"));
    while ((s = in.readLine()) != null) {
        for (char c : s.toCharArray()) {
            if (c != ' ') {
                System.out.println(c);
            }
        }
    }

回答by Evgeniy Dorofeev

ASCII space is 32, 127 is DEL. See http://en.wikipedia.org/wiki/ASCII

ASCII 空格是 32,127 是 DEL。见http://en.wikipedia.org/wiki/ASCII

回答by mthmulders

You have two options:

您有两个选择:

  1. If you want to remove spaces from input, take a look at the FilterInputStreamclass. Override the read()method: if it reads a space character, let it read one character further.

  2. If you want to remove spaces from output, take a look at the FilterOutputStreamclass. Override the write(int)method: if it is called to write a space character, let it do nothing.

  1. 如果要从输入中删除空格,请查看FilterInputStream类。重写read()方法:如果它读取一个空格字符,让它再读取一个字符。

  2. 如果要从输出中删除空格,请查看FilterOutputStream类。重写write(int)方法:如果调用它写一个空格字符,让它什么都不做。

In your particular case, I'd opt for the latter option, as I think it's a bit easier to implement, but the choice is yours.

在您的特定情况下,我会选择后一个选项,因为我认为它更容易实现,但选择权在您。

回答by JohnStrong

Use of a BufferedReader and InputStreamReader would help you. This way you can using the .readLine() to read in each line in the file and then use .charAt() to read each character from that line.

使用 BufferedReader 和 InputStreamReader 会帮助你。这样您就可以使用 .readLine() 读取文件中的每一行,然后使用 .charAt() 读取该行中的每个字符。

hope this helps

希望这可以帮助

回答by Achintya Jha

sc = new FileInputStream(new File ("D:\Empty.txt"));

while(sc.hasNextLine()){
    input = sc.nextLine().replace(" ","");
    System.out.println(input);
}

I think instead of checking only one character you want to remove space from whole file and print it...yes?

我认为与其只检查一个字符,不如从整个文件中删除空格并打印出来……是吗?