S
S
SABY2015-05-18 21:54:38
Java
SABY, 2015-05-18 21:54:38

How to convert character code to character in java?

There is the following code

package test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {

  static FileInputStream in = null;
  static BufferedInputStream bin = null;
  
  public static void main(String[] args) throws IOException {
    try {
      in = new FileInputStream(new File("res/text.txt"));
      bin = new BufferedInputStream(in);
      boolean eof = false;
      
      while (!eof) {
        int byteValue =  bin.read();
        System.out.println((char) byteValue +"");
        if (byteValue == -1) {
          eof = true;
        }
      }
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      in.close();
      bin.close();
    }
  }
}

It reads the file and outputs the character codes from the file. Do you get question marks when you try to cast byteValue to char?
How to convert character code to character?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SABY, 2015-05-18
@SABY

package test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

  static BufferedReader bf;
  
  public static void main(String[] args) throws IOException {
    
    try {
    bf = new BufferedReader (
                new InputStreamReader(
                    new FileInputStream( "res/text.txt" ), "UTF-8"
                )
            );
      String line = "";
      while (line != null) {
        line = bf.readLine();
        System.out.println(line);
      }
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      bf.close();
    }
    
  }
}

decided if anyone is interested

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question