Snippet. Java. Get String from a File Input Streams Stream

Reading a text file with Java and getting its content as String to further work with it is a custom everyday task in Java developer's day. Here is a quick and easy function to do just that.

FileInputStream fis = new FileInputStream(f);
public String stream_to_string(FileInputStream fis){
    byte[] buffer = new byte[f.length()];
    int read = 0;
    while(read > 0){
        read += fis.read(buffer, read, buffer.length - read);
    }
    return new String(buffer);
}

Updated on: 21 Dec 2024