穆穆清雅 2015-05-07 13:27 采纳率: 100%
浏览 2595
已采纳

java读文件后的数据怎么写到一个数组中 数据格式如下

图片说明

  • 写回答

3条回答 默认 最新

  • _昨日重现 博客专家认证 2015-05-07 16:04
    关注

    简单的写了一下,希望对你有帮助:
    public class Test {
    // 数组列数
    private static final int COLUMN_COUNT = 3;

    public static void main(String[] args) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    "d://test.txt")));
            String line = null;
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            System.out.println("=============转一维数组================");
            String[] singleArray = sb.toString().split("\\s+");
            // 遍历一维数组
            for (String str1 : singleArray) {
                System.out.println(str1);
            }
    
            System.out.println("\n=============转二维数组================");
            int rows = singleArray.length / COLUMN_COUNT;// 数组行数
            int num = -1;
            String[][] doubleArray = new String[rows][COLUMN_COUNT];
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < COLUMN_COUNT; j++) {
                    doubleArray[i][j] = singleArray[++num];
                }
            }
            // 遍历二维数组
            for (int i = 0; i < rows; i++) {
                for (int j = 0; j < COLUMN_COUNT; j++) {
                    System.out.print(doubleArray[i][j] + "\t");
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                    br = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    

    }

    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器