-2

次の内容を含むテキスト ファイルがあります


この .txt ファイルを読み取り、そこから整数を取得したいと考えています。整数変数を arrayList に格納します。

public class dedede {
    public static void al() throws Exception {

        File f = new File("C:\\users\\sony\\Desktop\\456.txt");

        try {
            FileReader islem = new FileReader(f);

            char data[] = new char[(int) f.length()];

            islem.read(data);
            String metin = new String(data);

            ArrayList<Integer> yapi = new<Integer> ArrayList();

            StringTokenizer s = new StringTokenizer(metin);

            String dizi[] = new String[s.countTokens()];

            int i = 0;
            while (s.hasMoreTokens()) {
                dizi[i] = s.nextToken();
                i++;
            }

            for (int c = 0; c < dizi.length; c++) {
                if (Integer.parseInt(dizi[c]) > 0) {
                    yapi.add(Integer.parseInt(dizi[c]));
                }

            }

            System.out.println(yapi);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        al();
    }
}
4

1 に答える 1