文件阅读器打印出 “?” 而不是文件

huangapple go评论52阅读模式
英文:

File Reader prints out "?" instead of file

问题

每当我点击编译,输出结果都是一个“?”。没有任何错误,只有问号。

以下是我的代码:

import java.io.*;

public class FileReaderExample {

    public static void main(String[] args) {
        try {
            FileReader fileReader = new FileReader("我的文本文件路径");
            int data = fileReader.read();
            while (data != -1) {
                data = fileReader.read();
            }
            System.out.print((char)data);
        } catch (Exception e) {
            System.err.println("发生了一个错误。");
        }
    }
}
英文:

Whenever I hit compile I get "?" as an output. No errors or anything, just the question mark.
Here's my code:

import java.io.*;
    
public class FileReaderExample {
    
    public static void main(String[] args) {        
        try {
            FileReader fileReader = new FileReader("path to my text file");
            int data = fileReader.read();
            while (data != -1) {
                data = fileReader.read();
            }
            System.out.print((char)data); 
        } catch (Exception e) {
            System.err.println("There's been an error.");
        }
    }
}

答案1

得分: 7

当您的程序达到System.out.print((char)data);时,data的值为-1。这不是可打印字符,因此显示?

英文:

When your program reaches System.out.print((char)data); the value of data is -1. This is not a printable character hence the ?.

huangapple
  • 本文由 发表于 2020年10月5日 19:22:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64207648.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定