由线到数组存储文本文件内容行 [英] Store text file content line by line into array

查看:151
本文介绍了由线到数组存储文本文件内容行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一切,我现在面临着存储文本文件中的内容放入数组不知道的问题。
情况是这样,文本文件的内容:

  ABC1
XYZ2
rxy3

我想用线将它们存储到阵列产品线,这可能吗?我希望是这样的:

 改编[0] = ABC1
改编[1] = XYZ2
ARR [2] = rxy3

我已经尝试这样的事情,但似乎没有为我工作。如果有人能帮助我,真的非常感谢。

在code是:

 中的BufferedReader =新的BufferedReader(新的FileReader(路径/ /文));
        字符串str;        而((海峡= in.readLine())!= NULL){
            的String [] = ARR str.split();
            的for(int i = 0; I< str.length();我++){
                改编[I] = in.readLine();
            }
        }


解决方案

我会推荐使用的ArrayList ,用于处理动态调整大小,而数组将需要一个定义的大小前面,你可能不知道。您可以随时关闭该列表返回到一个数组。

 中的BufferedReader =新的BufferedReader(新的FileReader(路径/ /文));
        字符串str;        清单<串GT;名单=新的ArrayList<串GT;();
        而((海峡= in.readLine())!= NULL){
            list.add(STR);
        }        的String [] = stringArr list.toArray(新的String [0]);

all,I'm now facing the problem of no idea on storing the content in text file into the array. The situation is like, text file content:

abc1
xyz2
rxy3

I wish to store them into array line by line, is that possible? What I expect is like this:

arr[0] = abc1
arr[1] = xyz2
arr[2] = rxy3

I've try something like this, but seem like not work for me. If anyone can help me, really thanks a lot.

The code is:

BufferedReader in = new BufferedReader(new FileReader("path/of/text"));
        String str;

        while((str = in.readLine()) != null){
            String[] arr = str.split(" ");
            for(int i=0 ; i<str.length() ; i++){
                arr[i] = in.readLine();
            }
        }

解决方案

I would recommend using an ArrayList, which handles dynamic sizing, whereas an array will require a defined size up front, which you may not know. You can always turn the list back into an array.

        BufferedReader in = new BufferedReader(new FileReader("path/of/text"));
        String str;

        List<String> list = new ArrayList<String>();
        while((str = in.readLine()) != null){
            list.add(str);
        }

        String[] stringArr = list.toArray(new String[0]);

这篇关于由线到数组存储文本文件内容行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆