将文本中的数据存储到变量中

时间:2011-05-04 10:32:47

标签: java text-files

我正在尝试从文本文件中读取数据并将各种数据类型存储到变量中。 假设txt文件采用以下形式;

1000 1    b 2000 2
   c 3000 3

现在,我正在尝试将字符存储到单独的变量中,将整数存储到单独的变量中。

到目前为止,我的尝试涉及将文本文件读入字符串,然后使用字符串标记生成器将每个元素存储到数组列表中。我对如何做到这一点有一个大概的想法;检查列表中的元素是否为字符,如果是,则将其存储到字符变量中,否则如果是整数,则将其存储到int中。  但是,我不熟悉识别某些东西是字符串还是整数的方法,比如isString,isInteger等等。有人可以给我一些关于如何做这个的建议吗?

我的代码如下:

public class copyToString {


    public static void main(String[] args) {

        String fileSpecified = args[0];

        fileSpecified = fileSpecified.concat(".txt");
        char [] content = new char[1024];

        System.out.println ("file Specified = " + fileSpecified);

        String container;
        ArrayList <String> words = new ArrayList<String> ();


        try {
            FileReader fr = new FileReader (fileSpecified);
            BufferedReader br = new BufferedReader (fr);
            StringBuilder builder = new StringBuilder();

            int read = 0;
            while ((read = br.read(content, 0, content.length)) > 0) {
                builder.append(content, 0, read);
            }

            container = builder.toString();

            StringTokenizer tokenizer = new StringTokenizer (container);
            while (tokenizer.hasMoreTokens()) {
                words.add(tokenizer.nextToken());
            }
            fr.close();
        } catch (IOException e) {
            System.out.println (e.getMessage());
        }

        for (int i = 0; i < words.size(); i++) {
            System.out.println ("words = " + words.get(i));


        }



    }



}

由于

2 个答案:

答案 0 :(得分:2)

我会非常推荐Apache Commons Lib。因为这个lib有一个

  • StringUtils类,它具有用于检查字符串
  • 的内容的大型方法基础
  • IOUtils类可以轻松阅读文件

此外,我会使用简单的正则表达式组来识别您的文本部分。

有关详细信息,请参阅Pattern和Matcher类。 (单词的正则表达式:“\ w”数字“\ d”)

答案 1 :(得分:1)

如果你真的不知道每种类型可能是什么,你必须将每个字段存储为字符串,如1000之类的数字可以是short,int,long,float,double或String。 1是数字,字符串还是字符“1”?没有上下文,您无法知道每种类型是什么。 abc可以是十六进制数字。 ;)


我需要更长时间才能说出我会做的不同,而不是重写代码。 ;)

public class CopyToString {
    static class Line {
        String word;
        int num1, num2;

        Line(String word, int num1, int num2) {
            this.word = word;
            this.num1 = num1;
            this.num2 = num2;
        }

        @Override
        public String toString() {
            return "Line{" + "word='" + word + '\'' + ", num1=" + num1 + ", num2=" + num2 + '}';
        }
    }

    public static void main(String... args) throws IOException {
        String fileSpecified = args[0] + ".txt";
        System.out.println("file Specified = " + fileSpecified);
        BufferedReader br = new BufferedReader(new FileReader(fileSpecified));

        List<Line> lines = new ArrayList<Line>();
        for (String line; (line = br.readLine()) != null;) {
            Scanner scanner = new Scanner(line);
            lines.add(new Line(scanner.next(), scanner.nextInt(), scanner.nextInt()));
        }
        br.close();

        for (Line l : lines)
            System.out.println(l);
    }
}

打印

file Specified = text.txt
Line{word='a', num1=1000, num2=1}
Line{word='b', num1=2000, num2=2}
Line{word='c', num1=3000, num2=3}