简体   繁体   English

如何将由ENTER键(在.txt中)分隔的行(从.txt中读取)添加到字符串arrayList的单独元素中?

[英]How do I add lines (read from a .txt) separated by AN ENTER KEY (in .txt) into separate elements of string arrayList?

I'm reading .txt file into my program and am adding lines of the .txt into a String arrayList. 我正在将.txt文件读入程序,并将.txt行添加到String arrayList中。 How do I add lines DELINEATED BY AN ENTER KEY (in .txt) into separate elements of the arrayList? 如何将按Enter键(在.txt中)描述的行添加到arrayList的单独元素中? Right now if I had the following written in text: 现在,如果我有以下书面文字:

this is a test 这是一个测验

test 测试

test test 测试一下

It would output: 它会输出:

this is a testtesttest test 这是一个testtesttest测试

What I want it to do is read things on a per line basis, and put it into different elements of the stringArrayList. 我想要它做的是逐行读取内容,并将其放入stringArrayList的不同元素中。 So I want "this is a test" to be an element, and "test" , and then finally "test test" . 所以我希望“这是一个测试”成为一个元素,然后“测试” ,最后是“测试”

My code is really ugly, but right now all I want to do is get it to work for my purpose. 我的代码确实很丑陋,但是现在我要做的就是让它按我的目的工作。 My first purpose is getting to read a .txt by line. 我的第一个目的是开始逐行读取.txt。 My second purpose is going to be parsing an element for a particular substring (a URL), connecting that URL to the internet, and then comparing a part of that page source of the webpage (parsing for a particular keyword) to the line ABOVE the substring I desire. 我的第二个目的是解析一个特定子字符串(URL)的元素,将该URL连接到互联网,然后将该网页的页面源的一部分(解析一个特定的关键字)与上一行进行比较。我想要的子字符串。 But that's a question for another time :^) 但这是另一个问题:^)

import java.io.*;
import java.util.*;


public class Test {
    public static void main(String [] args) {

        // The name of the file to open.
        String fileName = "test.txt";
        List<String> listA = new ArrayList<String>();

        // This will reference one line at a time
        String line = null;

        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) != null) {                
                System.out.println(line);
                listA.add(line);
                //*** THIS IS WHERE THE MAGIC HAPPENS ***\\ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            }   

            // Always close files.
            bufferedReader.close();         
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open da file ofheee hah. '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
            // Or we could just do this: 
            // ex.printStackTrace();
        }

        System.out.println();
        System.out.println("array FOr loop thingy incoming:");
        System.out.println();


        for (int i = 0; i < listA.size(); i++) {
            System.out.print((listA.get(i)).toString());
            }
        }
}

You just have to use println instead of print: 您只需要使用println而不是print即可:

System.out.println((listA.get(i)).toString());

Alternatively, you can add the line break character \\n 或者,您可以添加换行符\\n

Your code seems to be working so far. 到目前为止,您的代码似乎可以正常工作。 If you just want to see what elements are in listA , just print it out: 如果您只想查看listA元素, listA其打印出来:

System.out.println(listA);

Output: 输出:

[this is a test, , test, , test test, ]

Note that the extra lines in your input file are also being stored in listA . 请注意,输入文件中的多余行也存储在listA I'm not sure if that's the behavior you want. 我不确定这是否是您想要的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM