简体   繁体   中英

Reading data from a textfile, processing it, and writing it out to another text file

I am currently writing a program where I read the data (data being grades for a college course from each student) from a textfile into an array/arraylist. I am stuck on how to write the data from the textfile into the arraylist. Here is what I have tried and here is a screenshot of the.txt file.

    List<String> lines = new ArrayList<String>();
    BufferedReader reader=null;

    try {
        reader = new BufferedReader(new FileReader("cmp110.txt"));
        String line;
        while ((line = reader.readLine()) != null) {
            lines.add(line);
        }
        System.out.println(lines);

    } finally {
        reader.close();
    }
}

这是.txt文件的截图

Ok I believe this piece of code has solved my issue, The.txt file is propperly stored in an arraylist.

    List<String> lines = new ArrayList<String>();
        BufferedReader reader=null;
        try {
            reader = new BufferedReader(new FileReader("cmp110.txt"));
            String line;
            int count = 0;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
                while (lines.size() > count){
                    System.out.println(lines.get(count));
                    count++;
                }
            }

        } finally {
            reader.close();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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