ingenieur logiciel ingenieur logiciel - 1 month ago 18
Java Question

Read text file character by character

I need help reading a character from a text file.
I want to read character by character and get the id of ecpace

Here is my code:

public class GenieL {
String Nom_of_TACHE;
int ID ;
int time;
Array table[];
public static void main(String[] args) throws IOException {
BufferedReader br=null;
String strLine="";

try {
br=new BufferedReader (new FileReader("my.txt"));

while((strLine=br.readLine())!=null){
System.out.println(strLine);
Nom_of_TACHE=parseint(strLine);//
}//br.close();
} catch (FileNotFoundException ex) {
// Logger.getLogger(GenieL.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Unable to find the file name ");
}
catch (IOException e) {
// Logger.getLogger(GenieL.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Unable to read find the file name ");
}
}}

Answer

Read the file line by line and since every line is a String, do a for loop with the chars on it...

public static void main(String[] args) {
try {
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("./Details.txt"), "UTF8"));

    String line;
    while ((line = br.readLine()) != null) {
    for (int i = 0; i < line.length(); i++) {
        System.out.println("Char at: " + line.charAt(i));
    }
    System.out.println("Another line");
    }
    br.close();

} catch (IOException e) {
    e.printStackTrace();
}
}