Question:
How can I get the first character of each line in a file?
Answer:
See code.
Code:
import java.io.*; public class ReadFile { public static void main(String[] args) { try { FileInputStream is = new FileInputStream("file.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str; while ((str = br.readLine()) != null) { char first = str.charAt(0); System.out.println(first); } } catch (Exception e) { } } }
Output:
$ java ReadFile a b c d $ cat file.txt a bbb ccc ddd bb ccc dddd eeee cccc dd ee f dd eeeeee fff ggg