M
M
MaxLich2017-04-01 15:41:31
Java
MaxLich, 2017-04-01 15:41:31

How to convert a char (in the form of a character) to a string in Java?

I am using FileReader to read from a file. Using this class, you can read information character by character. The read() method reads a character from the file, but returns it as an int. And how to get the character itself from this character code and write it to string?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
gleendo, 2017-04-01
@evgeniy8705

public class App {
    public static void main(String[] args) throws FileNotFoundException {
        try (FileReader in = new FileReader("c://file.txt")) {
            String value = new String(new char[] { (char) in.read() }); // Первый символ из текстового файла
        } catch (IOException err) {
            err.printStackTrace();
        }
    }
}

M
MaxLich, 2017-04-01
@MaxLich

Thank you all for your efforts. I figured out what my problem is. I missed a little. I did not set the type char to the variable into which I write the data from the file. I had int. Therefore, he wrote the codes to me in a line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question