-1

I am having some very wierd issues while attempting to read a file.

Its only a few lines of simple code, but for some reason its thinking that my file has 8 lines of wierd rumbo jumbo text, while it has 2 lines and 4 letters in each line.

Code (Executed once, it's reading the correct file)

Scanner scanner = null;

    ArrayList<String> lines = new ArrayList<String>();

    try {
        scanner = new Scanner(getClass().getResourceAsStream("/level.txt"));
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    while (scanner.hasNext()) {
        lines.add(scanner.nextLine());
    }

    Main.main.log(lines.size() + " size");

File (level.txt, with no spaces)

sssas
sssas

Output:

8 Size

Its super weird since it's only a few lines and a simple file.

Any help, suggestions or error's made? There are no stacktraces!

Thanks,

Jake

SirTrashyton
  • 173
  • 2
  • 3
  • 12

3 Answers3

1

The first issue to consider is as @Sotirios Delimanolis says, you may read from a wrong txt file.

The second issue is that if you are perfectly sure about reading from the correct .txt file, the solution is to read with reading scanner.hasNextLine() while appending to the "lines" variable.

I think the problem occurs when you read with "hasNext()" which reads token by token, and go into next step with "scanner.nextLine()" which goes to the next line.

For example you may use the following;

Scanner scanner = null;

ArrayList<String> lines = new ArrayList<String>();

try {
    scanner = new Scanner(getClass().getResourceAsStream("/level.txt"));
} catch (Exception ex) {
    ex.printStackTrace();
}

while (scanner.hasNextLine()) { /* difference is here */
    lines.add(scanner.nextLine());
}

Main.main.log(lines.size() + " size");

EDIT:

You can use the following code and modify it however you want. I think the problem is also occurs when you are reading the File. To read the file you can use new File() constructor instead of your choice. See below:

Scanner scanner = null;
ArrayList<String> lines = new ArrayList<String>();

try {
    scanner = new Scanner(new File("level.txt")); /* difference is here */
} catch (Exception ex) {
    ex.printStackTrace();
}

while (scanner.hasNextLine()) { /* difference is here */
    lines.add(scanner.nextLine());
}

System.out.println(lines.size()); // gives output 2.
Onur Şahindur
  • 492
  • 8
  • 18
1

Java 7 one-liner to read a file to a list:

List<String> lines = Files.readAllLines(
    Paths.get(getClass().getResource("/level.txt").toURI()),
    StandardCharsets.UTF_8
);
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
user432
  • 3,506
  • 17
  • 24
0

I would suggest to go on different kind of method which is more correct to do..

public static void main(String[] args) {

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

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\testing.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            lines.add(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}

This will do the trick perfectly.. hope that helps

EDIT:

If you would like to read file from classpath of the project you can use the following:

 InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt"); 
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); 

Somethink like that will be fine.. I am not saying you cannot do it with scanner.. IMHO I think this is better.. But it is a matter of choice and not big architecture problem.. Consideration is yours :)

Aviad
  • 1,539
  • 1
  • 9
  • 24
  • Basically Scanner is more appropriate to use with input from user... and its api is not the best.. for reading files it is more appropriate to use BufferReader.. Look at this link for example http://stackoverflow.com/questions/20838244/difference-between-buffered-reader-and-file-reader-and-scanner-class – Aviad May 25 '14 at 21:24
  • Easier to use doesn't mean the other solution is wrong. Also, you're reading from the file system, while OP is reading from the classpath. Fix those two things and you should be fine. – Sotirios Delimanolis May 25 '14 at 21:25
  • Scanner is used if you want to parse the input, or getting input from the cmd.. BufferReader is just reading plain text which this is the situation here..Don't you agree? – Aviad May 25 '14 at 21:29
  • How can I read from where the file is located in my src, and not from anywhere on my computer? – SirTrashyton May 25 '14 at 21:32
  • Yeah, how do I read from the class path with this example above? – SirTrashyton May 25 '14 at 21:32
  • You can use a `Scanner` to read from any `InputStream` just fine. – Sotirios Delimanolis May 25 '14 at 21:33
  • InputStream in = this.getClass().getClassLoader() .getResourceAsStream("SomeTextFile.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); Somethink like that will be fine.. I am not saying you cannot do it with scanner.. IMHO I think this is better.. But it is a matter of choice and not big architecture problem.. Consideration is yours :) – Aviad May 25 '14 at 21:39
  • I want you to edit your answer. Rephrase it and add the details in your previous comment. – Sotirios Delimanolis May 25 '14 at 21:53
  • Added it to my answer – Aviad May 25 '14 at 22:24