为什么FileInputStream read()方法在置入无限循环时错误地读取问号(ascii:63)? [英] Why is FileInputStream read() method wrongly reading question mark (ascii: 63) when put into infinite loop?

查看:634
本文介绍了为什么FileInputStream read()方法在置入无限循环时错误地读取问号(ascii:63)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站上有一些类似的问题,但都已在不同场景中使用过。所以,我在这里问:

There are some similar questions on the site but all have been used in different scenario. So, I'm asking it here:

package Assign6B;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOpsDemo {
    public static void main(String[] args) throws IOException 
    {

        FileInputStream inputFile = null;
        FileOutputStream outputFile = null;

        try
        {
            inputFile = new FileInputStream("s:/inputFile.txt");
            outputFile = new FileOutputStream("s:/outputFile.txt");
            char c;
            while(( c = (char) inputFile.read()) != -1)
            {
                System.out.println((char)c);
                outputFile.write(c);
            }

            System.out.println("File transfer complete!");
        }

        finally
        {
            if (inputFile != null)
                inputFile.close();

            if (outputFile != null)
                outputFile.close();
        }
    }
}

这是我的代码。在while循环条件中,首先我将其设置为通过read()将int输出强制转换为char。结果是它进入了一个无限循环,所有字符都被转换为'?'(ascii:63)。然后我意识到我的char转换错误并改变了它。

This is my code. In the while loop condition, first I had set it to typecast the int output by read() into a char. The result was that it went into an infinite loop with all characters being converted to '?' (ascii: 63). Then I realized my mistake of char conversion and changed it.

但当我将我的while条件改为= -2(没有char转换)时(这个条件会永远不会发生,因此将其置于无限循环中)。在这里,即使没有字符转换,文件的第一个(例如10个)有效字符仍然被转换为?。 (在达到EOF之后,所有无效的字符变成'?' - 我假设已经给出了这个。)

But when I changed my while condition to "=-2" (without the char conversion) (this condition will never occur and hence put it into infinite loop). Here too, even if there is no char conversion the first (say, 10) valid characters of the file are still being converted into '?'. (After it reaches EOF, all invalid chars become '?' - I'm assuming this is given).

为什么会发生这种情况?至少应该正确读取文件的有效字符,直到它遇到EOF并开始输入无效字符!

Why is this happening? At least the valid characters of the file should be read properly until it encounters the EOF and starts feeding on invalid chars!

推荐答案


为什么会这样?

Why is this happening?

问题在于这一行:

 while(( c = (char) inputFile.read()) != -1)

您正在执行以下操作:


  1. 从文件中读取一个字节。这给你一个 int ,这是一个0到255范围内的字节,或-1。

  1. Reading a byte from the file. This gives you an int which is either a byte in the range 0 to 255, or -1.

您正在将该值转换为 char 。对于该字节,它给出了一个 char 值,范围为0到255.对于 -1 ,演员表会给你'\\\￿'

You are casting that value to a char. For the byte, that gives a char value in the range 0 to 255. For -1 the cast will give you '\uffff'.

您将该值分配给 c

然后根据 -1 测试该值。这是它出错的地方。如果读取返回 -1 ,您现在将评估此'\ uffff'== -1 。 LHS转换为 int 值... 0x0000ffff ...并将其与<$ c $进行比较C>的0xffffffff 。它们是不同的。

You then test the value against -1. This is where it goes wrong. In the case where read returned -1, you will now be evaluating this '\uffff' == -1. The LHS is converted to an int value ... 0x0000ffff ... and that is compared to 0xffffffff. They are different.

然后你打印'uffff' ...当您作为默认字符集中的字符输出时,正在转换为'?

Then you print 'uffff' ... which is being converted to a '?' when output as a character in your default charset.

代码中有两个主要错误。首先,转换 int - > char - > int 不会起作用;见上文。

There are two major mistakes in the code. First, the conversion int -> char -> int is not going to work; see above.

其次,更重要的是:


  • 你不应该尝试使用InputStream(面向字节)将数据作为字符读取,并且

  • you should not be trying to use an InputStream (which is byte oriented) to read data as characters, and

您应该尝试将字符数据写入OutputStream 。

you should be trying to write character data to an OutputStream.

根据您在这里实际尝试的内容,您应该:

Depending on what you actually trying to achieve here, you should either:


  • 读取和写入字节...中间没有虚假的转换到 char ,或者

使用 FileReader FileWriter 来为平台默认字符集进行正确转换。

use a FileReader and FileWriter to do the conversions properly for the platform default characterset.

(还有一些其他要点这可能是关于缓冲,选择替代字符集等,但这个答案已经太长了。)

(There are some other points that could be made about buffering, choosing an alternate charset, etc, but this Answer is already getting too long.)

这篇关于为什么FileInputStream read()方法在置入无限循环时错误地读取问号(ascii:63)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆