Engineeringserver.com

A community for computer science students & developers about software development, game development, game design, games, anime preview & reviews and more!


Read a textfile into an ArrayList

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
514 views
JAVA:
  1. /*--------------------------------------------------------------------------
  2. ScanToArrayDemo class
  3. *****************
  4. By: HappyFace   http://www.engineeringserver.com
  5. Contact:        info [@] engineeringserver.com
  6. Version:        10/September/2008
  7. "*****************
  8. Note: Read a textfile into an ArrayList
  9. //----------------------------------------------------------------------*/
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.Scanner;
  15.  
  16. public class ScanToArrayDemo {
  17. public static void main(String[] args){
  18. File fInput     = new File("c:\\input.txt");
  19. ArrayList textHolder = new ArrayList();
  20. String tmp = "";
  21. try {
  22. Scanner scInput = new Scanner(fInput);
  23. while (scInput.hasNextLine()){
  24. tmp = scInput.nextLine();
  25. textHolder.add(tmp);
  26. }
  27. }
  28. e.printStackTrace();
  29. }
  30. catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. //display the content of the array
  34. System.out.println(textHolder.get(3));
  35. }
  36. }


Your Ad Here

How to read a textfile and output it , BufferedReader & Scanner

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.5 out of 5)
Loading ... Loading ...
910 views

Here's a demo I wrote some time ago to read a file and output it in the console. I've created 2 versions. One that uses the "Scanner object" and the other the "BufferedReader object" to read the text files from disk.

Both examples of mine work, but the Scanner version is preferred.

BufferedReader version:

JAVA:
  1. public class ReadFile {
  2. public static void main(String[] args) {
  3. ReadFile rf = new ReadFile();
  4. rf.readFile();
  5. }
  6. public void readFile(){
  7. try {
  8. FileReader fr = new FileReader("c:\\input.txt"); // read a file
  9. String tmp;
  10. tmp = br.readLine(); // read first line of file.
  11. while(tmp != null){ // read a line until end of file.
  12. System.out.println("" + tmp);
  13. tmp = br.readLine();
  14. }
  15. br.close();
  16. } catch (FileNotFoundException e) {
  17. e.printStackTrace();
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }


Scanner version:

JAVA:
  1. public class ReadLineDemo {
  2. public static void main(String[] args){
  3. ReadLineDemo EDS = new ReadLineDemo();
  4. EDS.readFile();
  5. }
  6. public void readFile(){
  7. String str;
  8. try {
  9. fr = new FileReader("c:\\input.txt");
  10. System.out.println("begin");
  11. Scanner sc = new Scanner(fr);
  12. while(sc.hasNext()){
  13. str = sc.nextLine();
  14. System.out.println(str);
  15. }
  16. }
  17. e.printStackTrace();
  18. }
  19. }
  20. }


Your Ad Here