Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. package testPackage;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Scanner;
  9.  
  10. public class test {
  11.  
  12. public static List<String> readFile(String filename) throws IOException {
  13.  
  14. Scanner sc = new Scanner(new File(filename + ".txt"));
  15. List<String> lines = new ArrayList<String>();
  16. while (sc.hasNextLine()) {
  17. lines.add(sc.nextLine());
  18. }
  19. return lines;
  20. }
  21. }
  22.  
  23. ------------------------------------second class---------------------------------------------------
  24. package testPackage;
  25.  
  26. import java.io.IOException;
  27. import java.util.List;
  28.  
  29. public class TestResult {
  30.  
  31. public static void main(String[] args) throws IOException {
  32.  
  33. List<String> arr = test.readFile("test");
  34.  
  35. System.out.println(arr.get(0)); //returns string1
  36. System.out.println(arr.get(0)); // returns a blank?
  37.  
  38. for (String el : arr) {
  39. System.out.println(el); // returns String1 ->nextline>blank>nextline>String2>nextline>blank>nextline>String3>nextline>blank
  40. }
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement