zoukankan      html  css  js  c++  java
  • 从文件中读取数组数据————Java

    自己总结一下Java文件的读取类似数组数据的方法,自己可以快速查看。

    一、规整化数据:

    对于数组数据是一一对应的情况

            ArrayList<String> arrayList = new ArrayList<>();
            try {
                File file = new File(path);
                InputStreamReader input = new InputStreamReader(new FileInputStream(file));
                BufferedReader bf = new BufferedReader(input);
                // 按行读取字符串
                String str;
                while ((str = bf.readLine()) != null) {
                    arrayList.add(str);
                }
                bf.close();
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // 对ArrayList中存储的字符串进行处理
            int length = arrayList.size();
            int width = arrayList.get(0).split(" ").length;
            int array[][] = new int[length][width];
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    String s = arrayList.get(i).split(" ")[j];
                    array[i][j] = Integer.parseInt(s);
                }
            }
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < width; j++) {
                    System.out.print(array[i][j] + " ");
                }
                System.out.println();
            }

     

    二、非规整化数据:

        private static void file(String fileName) throws Exception {
            int array[][] = new int[1024][1024];
    
            File file = new File(fileName);
            Scanner sc = new Scanner(file);
    
            String[] odd = new String[40]; // 奇数行数据
            String[] even = new String[40]; // 偶数行数据
    
            int length = 0;
            int column = 0;
            int rows = 1;
            while (sc.hasNextLine()) {
                if ((rows % 2) == 1) { // 奇数行
                    odd = sc.nextLine().split("\s{1,}");  // split("\2{1,}");不论字符中间有多少个空格都当作一个空格处理
                    for (int i = 0; i < odd.length-1; i++) {
                        array[length][i] = Integer.parseInt(odd[i]);
                    }
                } else if ((rows % 2) == 0) { // 偶数行
                    even = sc.nextLine().split("\s{1,}");
                    for (int i = 0; i < even.length-1; i++) {
                        array[length][i] = Integer.parseInt(even[i]);
                    }
                }
                if (rows == 2) {
                    column = even.length;
                }
                length++;
                rows++;
            }
            sc.close();
            for (int i = 0; i < length; i++) {
                for (int j = 0; j < column; j++) {
                    System.out.print(array[i][j] + " ");
                }
                System.out.println();
            }
        }
  • 相关阅读:
    NTDS活动目录数据库维护--碎片整理、移动数据库文件、日志文件
    管理活动目录数据库
    NTDS活动目录数据库维护--碎片整理、移动数据库文件、日志文件
    IIS上的反向代理
    java.lang.OutOfMemoryError: Metaspace 的解决
    WPF分页控件
    C#学习笔记之.Static关键字
    C#学习笔记之简说参数
    C#学习笔记之Winform登录注册
    C#学习笔记之XML工具类
  • 原文地址:https://www.cnblogs.com/future-dream/p/10675342.html
Copyright © 2011-2022 走看看