3

Hi i am new to Java working with files. I have got a text file called banking.txt located in C: but when i try put its content into an ArrayList and print it prints out nothing. Is the file path i am using incorrect or is it something different?

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;

public class FileP {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner read = new Scanner(new File("C:\\banking.txt"));
        ArrayList<String> myList = new ArrayList<String>();
        while(read.hasNextLine()) {
            myList.add(read.nextLine());
        }
        System.out.println(myList);
    }
}
Valentin Michalak
  • 1,777
  • 1
  • 12
  • 23
Blue
  • 73
  • 6
  • 1
    "it prints out nothing": this is hard to believe. Which output do you get exactly? – Henry Jan 25 '18 at 10:45
  • It prints out this with no elements inside " { } " – Blue Jan 25 '18 at 10:46
  • There is a small possibility that program is not able to read file in C: directory. Try to run your code with administrative rights. (If using cmd then run cmd as admin or using any IDE then run IDE as admin) try again by changing path from C: drive to D: drive. – Nishant Thapliyal Jan 25 '18 at 10:49
  • 1
    This should be work. may be permission issue. any output? – MadukaJ Jan 25 '18 at 10:50
  • 1
    Related: [Java reading a file into an ArrayList?](https://stackoverflow.com/q/5343689) Although not voting to close as duplicate because your code should be working or throwing an exception (not getting anything in the list should mean the file is empty). – Bernhard Barker Jan 25 '18 at 10:51
  • Just trying to find the run as admin button in intelliJ! haha – Blue Jan 25 '18 at 10:53
  • Are you sure your filepath is correct? – Stephan Hogenboom Jan 25 '18 at 10:54
  • @Blue close IntelliJ and re-run as admin (option By right clicking on IntelliJ app icon) – Nishant Thapliyal Jan 25 '18 at 10:54
  • 3
    You could check whether your file 1) exists and 2) can be read. Check this example : https://ideone.com/7NckQf – Aaron Jan 25 '18 at 10:55
  • Using the code from @MacDaddy answer it works which is odd because ye were saying that my code was correct, so now we know the path is correct. Running as admin now – Blue Jan 25 '18 at 10:59
  • Running as admin gives the same output – Blue Jan 25 '18 at 11:01
  • @DiabloSteve answer solved the problem but thank ye for yer help! – Blue Jan 25 '18 at 11:04
  • 1
    I can reproduce this by trying both `File` and `FileReader` in the Scanner constructor trying to read the same system file (on Win 7) - one prints an empty ArrayList, the other prints the file contents. There might be some permissions differences between the two, but I can't tell you what the exact problem is. – Bernhard Barker Jan 25 '18 at 11:15
  • I can't reproduce the problem. It just works. Always. With both File and FileReader. – DodgyCodeException Jan 25 '18 at 12:23

3 Answers3

2

Try to use InputStream

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.io.InputStream;
    import java.io.InputStreamReader;

    public class FileP {

      public static void main(String[] args) throws IOException {

        ArrayList<String> myList = new ArrayList<String>();
        InputStream is = new FileInputStream("C:\\banking.txt");
        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
        String line = buf.readLine();
        StringBuilder sb = new StringBuilder();
        while (line != null)

        {
          myList.add(line);
          line = buf.readLine();
        }

       System.out.println("myList : " + myList);
      }
    }
MadukaJ
  • 682
  • 6
  • 22
1

I would recommend using the lines method of Java 8:

 Stream<String> content = Files.lines("C:\\banking.txt")
 content.forEach(System.out::println);
Stephan Hogenboom
  • 1,309
  • 2
  • 13
  • 23
  • 3
    Same comment as for CodeMatrix : good suggestion, but no obvious reason why it would fix OP's problem. – Aaron Jan 25 '18 at 10:52
  • Just to be pedantic, the proper way is: `try (Stream content = Files.lines("C:\\banking.txt")) { content.forEach(System.out::println); }`. This ensures the file gets closed after finishing. – DodgyCodeException Jan 26 '18 at 09:55
1

I guess you need to use this code as the Scanner:

Scanner in = new Scanner(new FileReader("C:\\banking.txt"));

This will read the file as a String and you can add it to Array. Check this post for more: Reading a plain text file in Java

Valentin Michalak
  • 1,777
  • 1
  • 12
  • 23
DiabloSteve
  • 421
  • 2
  • 13
  • 3
    Why would this fixed the problem? Scanner has a constructor that takes a File. – Bernhard Barker Jan 25 '18 at 11:04
  • I am unsure but with this modification made to my code it is all printed out. – Blue Jan 25 '18 at 11:05
  • 1
    can you try again with your previous code. because it also have to work. – MadukaJ Jan 25 '18 at 11:08
  • I already tried it, i undid all the changes and { } is printed then i switched back again to this code and it prints it correctly i even switched to netbeans and the very same thing. – Blue Jan 25 '18 at 11:10
  • Dukeling. Check these 2 links to know what is the different between "new File()" and "new FileReader()". https://docs.oracle.com/javase/7/docs/api/java/io/File.html https://docs.oracle.com/javase/8/docs/api/?java/io/FileReader.html – DiabloSteve Jan 25 '18 at 11:57