0

Here From the start() method i called the loadMap(filename) method with a text file.But i don't know why though the loadMap() is called but the FileReader and BufferedReader doesn't working. and the text commented below this two File reader's Statement System.out.print("INside loadMap()"); doesn't printing in the console and the text File isn't reading. What's the problem here occur actually? Help someone please.

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class DemoClass {
    public static void main(String[] args) {
        start();
    }

    public static void start() {
        try {
            System.out.print("Pobon File Inside");
            loadMap("data\\map1.txt");

        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    private static void loadMap(String filename) throws IOException {
        ArrayList lines = new ArrayList();

        FileReader fReader = new FileReader(filename);
        BufferedReader reader = new BufferedReader(fReader);
        System.out.print("INside loadMap()");

        while (true) {
            String line = reader.readLine();
            if (line == null) {
                reader.close();
                break;
            }
            if (!line.startsWith("!")) {
                lines.add(line);
            }
        }
        System.out.print("INside loadMap()");
    }
}
smk pobon
  • 113
  • 1
  • 12
  • 4
    Well, most likely you get an exception somewhere while trying to read your file(s). But this is exception is swallowed here: `try { ... } catch (Exception e) { // TODO: handle exception }`. Put a `e.printStacktrace()` into the catch block. And then tell us, what exception is written on the console. – Seelenvirtuose Dec 13 '15 at 16:52
  • Most likely it cannot open the file, and throws an exception – Atri Dec 13 '15 at 16:53
  • Did you try to provide absolute path of file? – Steephen Dec 13 '15 at 16:53
  • it is not able to get the filename. can you debug and confirm? – Keshav Dec 13 '15 at 16:54
  • i gave the `e.printStacktrace()` and it shows--> `Insidejava.io.FileNotFoundException: data\map1.txt (The system cannot find the path specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader.(Unknown Source) at DemoClass.loadMap(DemoClass.java:25) at DemoClass.start(DemoClass.java:14) at DemoClass.main(DemoClass.java:8)` this exceptions @Seelenvirtuose @13th Ghost – smk pobon Dec 13 '15 at 17:11
  • 1) Please don't add information as a comment. Go and _edit_ your question accordingly. 2) What do you think the error "FileNotFoundException: data\map1.txt (The system cannot find the path specified)" means? – Seelenvirtuose Dec 13 '15 at 17:14
  • This means system can't find the path specified. This program is taken from an `Applet`. The applet is working well and the File also uploading. I'm trying to convert that applet as an `Application program`. The given program is only a short demonstration of that program. So From the same position and same path the `Applet` working but the `This` program is not. Why? @Seelenvirtuose – smk pobon Dec 13 '15 at 17:24
  • I've also put the text file in the same directory and called the loadMap() method like `loadMap("map1.txt")`. But same exception is showing. Why this happening actually? @Seelenvirtuose – smk pobon Dec 13 '15 at 17:37
  • I already asked you to put question-related information _into the question_ itself by simple _editing_ it. They are not comments! And the file `map1.txt` is not found in the _relative_ directory `data` (as you sepcified `data/map1.txt`) if the directory `data` simply is not inside the _current working directory_ of the program. So, if you really want our help, then please tell us the _absolute_ path of the file and the (absolute) _working directory_ of your program. You can also find something out by doing `System.out.println(new File("data1/map1.txt").getAbsolutePath())` – Seelenvirtuose Dec 13 '15 at 17:45
  • my `DemoClass` is inside default package and `src` folder contains `data` folder and `data` folder contains `map1.txt` file that is `data/map1.txt`. @Seelenvirtuose – smk pobon Dec 13 '15 at 18:17
  • *sigh* And what is the _current working directory_ while running your program? Does it point to the `src` directory inside your project directory? Only then you can retrieve your file with the relative path `data/map1.txt`. Most likely the current working directory _is_ your project directory which does not contain a `data/map1.txt` but a `src/data/map1.txt`! – Seelenvirtuose Dec 13 '15 at 19:14
  • _current working directory_ is `src` and `src` contains `data` folder and `data` contains `map1.txt` file that is `src/data/map1.txt` and `src/DemoClass.java` is _current directory_ – smk pobon Dec 13 '15 at 19:19

1 Answers1

0

If System.out.print("INside loadMap()") is never called, then an IOException must be thrown when creating the FileReader.

In other words, the file you entered as the parameter when calling loadMap() (data\map1.txt) doesn't exist. You should consider retrieving the file in a different manner, such as placing it in a source folder and then calling getClass().getResource()

Community
  • 1
  • 1
Alexander Guyer
  • 2,063
  • 1
  • 14
  • 20
  • But the `FileReader` constructor doesn't support `getClass().getResource()` URL . so how is it possible, can you explain please? @Nerdizzle – smk pobon Dec 13 '15 at 17:17
  • Don't use a `FileReader.` Use a `FileInputStream` as shown in the link provided. – Alexander Guyer Dec 13 '15 at 18:15
  • so how can i read the file as `String` of text using `FileInputStream`? bcz i need to store them in a `ArrayList`. @Nerdizzle – smk pobon Dec 13 '15 at 18:47
  • Create an `InputStreamReader` by passing in the `FileInputStream` to the constructor, and then pass the instantiated `InputStreamReader` into the constructor of a `BufferedReader` like so: `BufferedReader in = new BufferedReader(new InputStreamReader(mFileInputStream));` – Alexander Guyer Dec 13 '15 at 18:57