Java provides FileInputStream class for reading data from a file. This class provides read() method to read contents of a file character by character. Create a StringBuffer object and append character by character to it using the append() method. Finally, convert the contents of StringBuffer object to String using toString() method.
import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class StreamToString { public static void main(String args[]) throws IOException { File file = new File("da"); FileInputStream fis = new FileInputStream(file); int size=fis.available(); StringBuffer buffer = new StringBuffer(); for(int i = 0; i< size; i++) { buffer.append((char)fis.read()); } System.out.println(buffer.toString()); } }
Hi how are you