J
J
Junior77712021-06-03 11:35:54
Java
Junior7771, 2021-06-03 11:35:54

How to display a list on the screen?

Unable to extract text from file. I read text from a file and convert it to a stream of bytes, after that I want to write it to a list and display it on the screen. But at the output I get a set of numbers.

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        FileInputStream fis= null;
        InputStreamReader str = null;
        int b= 0;
        try {
           fis = new FileInputStream("D:/1.txt");
            str = new InputStreamReader(fis, "UTF-8");
            while ((b = str.read())!=-1) {
             b = (char)b;
              List<?> list = Arrays.asList(b);

                   list.forEach(System.out::println);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                str.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kiryatpechin, 2021-06-06
@Junior7771

FileReader fileReader = new FileReader(filename);
int c;
while((c=fileReader.read())!=-1){
text+=((char)c);
}
fileReader.close();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question