逐个字符地读取文本文件

时间:2016-11-07 18:07:49

标签: java arrays

我需要帮助从文本文件中读取字符。 我想逐个字符地阅读并获得ecpace的id

这是我的代码:

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);//
            } 
        } catch (FileNotFoundException ex) {
            System.err.println("Unable to find the file name ");
        }
        catch (IOException e) {
            System.err.println("Unable to read find the file name ");
        }
}}

2 个答案:

答案 0 :(得分:1)

逐行读取文件,因为每一行都是一个字符串,所以在它上面加上字符进行for循环...

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();
}
}

答案 1 :(得分:0)

Reader.read是你的朋友。 Javadoc记录了它:http://docs.oracle.com/javase/7/docs/api/java/io/Reader.html#read()