自己总结一下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();
}
}

最新文章

  1. [LeetCode] Factor Combinations 因子组合
  2. CSS3实现动画
  3. NGUI ScrollView总结
  4. Asp.Net 上传图片并生成高清晰缩略图(转)
  5. ApacheBench~网站性能测试工具
  6. Android服务之PackageManagerService启动源码分析
  7. 2014年3I工作室成员的正式名单
  8. appium+robotframework的简单实例
  9. C#SMTP发邮件
  10. mac中配置jdk环境
  11. Linux环境下使用tcpdump抓包与下载
  12. python之操作系统介绍,进程的创建
  13. POJ 2393 Yogurt factory【贪心】
  14. Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
  15. MySQL子查询,派生表和通用表达式
  16. PHP 官方发行版扩展下载地址
  17. openssl 基本加密
  18. java页面url传值中文编码&amp;解码
  19. hdu 4494 Teamwork (可行流的最小流)
  20. 【Properties】Properties的load方法

热门文章

  1. 用原生js简单模仿angular的依赖注入
  2. Django应用app创建及ORM
  3. Chrome添加Axure RP插件
  4. SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传
  5. 利用ajax与input 上传与下载文件
  6. Java框架spring 学习笔记(四):BeanPostProcessor接口
  7. python 多进程/多线程/协程 同步异步
  8. yum与rpm常用命令
  9. SpringCloud微服务负载均衡与网关
  10. jmeter在几个固定的字符串中,随机取其中之一的方法