Read from a text file char by char in Java
Description
The following code shows how to read from a text file char by char.
Example
/*from ww w. j a va 2s . c om*/
import java.io.FileInputStream;
public class Main {
public static void main(String[] args) throws java.io.IOException {
FileInputStream in = new FileInputStream("a.bat");
StringBuffer buf = new StringBuffer();
int c;
do {
c = in.read();
buf.append( c);
} while (c != -1);
in.close();
System.out.println(buf);
}
}
The code above generates the following result.