1
public class Example {
    public static void main(String[] args) throws IOException {
        byte[] bytes = new byte[100];
        InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream("/Users/deni/Desktop/Input.txt"));
        while (fileInputStream.read() != -1) {
            int i = 0;
            bytes[i] = (byte) fileInputStream.read();
            i++;
        }

        String string = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(string);
    }
}

File contains only just number.But when I run this method I get question mark. What is source of this problem and how to solve it? I am from Russia.I have read some other posts related with this problems but it doesn't help me.I tried write javac -J-Duser.language=en Example java. Thank you.

  • 1
    What are the contents of `Input.txt`? Do you simply want to print the contents of a text file? – Janez Kuhar May 21 '21 at 19:14
  • Do you use eclipse? Do you except special (non-ASCII) characters? – dan1st May 21 '21 at 19:14
  • 2
    You should put `int i = 0;` outside the while-loop. – Daniel May 21 '21 at 19:17
  • Also, why do you use `InputStreamReader` to read binary input and convert it to a UTF-8 string afterwards? – dan1st May 21 '21 at 19:21
  • I use jetBrains,Input.txt just contain number for example(108 22, 3950 11) –  May 21 '21 at 19:23
  • You are reading only 100 bytes. UTF-8 is a variable-byte-length encoding. So a single "symbol" takes more than a single byte. Hence it is very likely that you just cut off the last symbol from the file and because of that it can not be displayed correctly. Like, imaging all symbols except the last need 98 bytes. Then you just read 2 bytes more but a symbol like 你 needs 3 bytes, so you only read "half" of it, hence it is broken. – Zabuzard May 21 '21 at 19:23
  • Does this answer your question? [Java: How to read a text file](https://stackoverflow.com/questions/2788080/java-how-to-read-a-text-file) – Janez Kuhar May 21 '21 at 19:25
  • How to read full length of character if I should convert char of int to String with decoding? –  May 21 '21 at 19:26
  • Not,I didn't have account in this year –  May 21 '21 at 19:27
  • Why are you even reading low level byte by byte? Your approach is super cumbersome, error prone and also inefficient. Just do `Files.readString(path)` and you have the full file content as a `String`, done. – Zabuzard May 21 '21 at 19:27

1 Answers1

2
  1. You either want to use a Reader and read diretly into char[]/String (the preferred method) or use an InputStream, read into a byte[] and transfer that into a String later on. You combine both, effectively double-decoding the input, which might accidentally work for pure ASCII text, but will mangle all other text.

  2. You don't actually fill the byte[] array because you declare int i = 0 inside the loop meaning you'll only ever fill the byte[0] with a value.

  3. You ignore every second read() result by calling read() twice in your loop and checking one return value for -1 and storing the other in bytes[]. Instead, you want to call read() once, assign it to a variable, check that variable for -1 and if it isn't use that variable to store in the target.

  4. Files.readString(Path, Charset) does everything you're trying to do without having to implement it manually.

But in the interest of actually interacting with your code, this is a functioning method with as few changes as possible:

    char[] chars = new char[100];
    InputStreamReader reader = new InputStreamReader(new FileInputStream("/Users/deni/Desktop/Input.txt"), StandardCharsets.UTF_8);
    int i = 0;
    int c;
    while ((c = reader.read()) != -1) {
        chars[i] = (char) c;
        i++;
    }

    String string = new String(chars, 0, i);
    System.out.println(string);

This is still needlessly complicated, breaks if the input is more than 100 chars and produces weird output if it's less than 100. But should demonstrated the necessary changes.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • (note that this implementation still breaks the last symbol, in case its multi-byte and doesnt line up with the 100 byte limit by coincidence) – Zabuzard May 21 '21 at 19:37
  • 1
    @Zabuzard: it still has plenty of problems (given a fixed-sized char array mostly), but how does it break the last character? Edit: I've changed the constructor call to only used actually-written values from `chars`. – Joachim Sauer May 21 '21 at 19:38
  • 1
    @Zabuzard: oh, I think I know what you mean: note that they both the original code and mine us a `Reader`. But OPs code called the variable `fileInputStream`, which is slightly misleading to say the least. – Joachim Sauer May 21 '21 at 19:41