문제

I am doing a method involving BufferedReader and I to have use it as an input argument, can someone tell me how to use it as an input argument but initialize it outside the method?

Other thing is, how do I get the buffer to read special characterS? (eg: ´, ~)

public static List<Pacote<Pair<String, Double>>> create(
BufferedReader fileReader, int capacidadePacotes)
throws IOException {
        List retorno = new ArrayList <> (6);
        String s;
        while ((s=fileReader.readLine())!=null){
            retorno.add(parseItem(s));
        }
        return retorno;

    }

It basically reads a file and sends it to another function that treats the text and creates objects based on that, I'm just not clear on the whole using BufferedReader as an input argument, have just used it inside the method before so I'm unclear on how to initialize it properly, probably a dumb question but I would like to know how to do it properly

도움이 되었습니까?

해결책

You can initialize the BufferedReader object as follows if you are trying to read a file.

public static void main(String[]args) {
    BufferedReader rdr = new BufferedReader(new FileReader("filepath"));
    int capacidadePacotes = 10;
    create(rdr, capacidadePacotes);
}
//urcode for create

The buffered reader can read line by line using the readLine() method. If you read null that means you reached the end of the file. A more readable way to use the buffered reader would be the following:

String s = rdr.readLine();
while(s != null) { //while u didn't reach the end of the file
    //your code
    s = rdr.readLine();
}

다른 팁

If you want to initialize it "outside" the method, why not hand it over like that:

create(new BufferedReader(reader, 3));

Or how exactly do you want it to have instantiated? For the instantiation, you need a Reader, which can be handed over. If you want to create a Reader from a file, the answer is also in the following link.

How to read special characters with a BufferedReader:

Read special characters in java with BufferedReader

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top