File之读取文件内容

如果是单个文件:

    public static void fileToData() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader
                    (new FileInputStream(new File
                            ("E:\\test\\aaa.txt")
                    ), "GBK"));//UTF-8
            String str = null;
            int i = 0;
            while ((str = br.readLine()) != null) {
                String[] v1 = str.trim().split("\\s+"); //剔除调前、后、中间所有的空格
                System.out.println(str);
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

如果是文件夹,则取出来fileList,然后for循环去解析,切记,每次循环里面都要记得close()

File file = new File("E:\test");
File[] tempList = file.listFiles();
for (File fileItem : tempList) {
    ......
}

 

你可能感兴趣的:(Java,java)