0

I'm fairly new to java. I'm making a text-based game right now. I have a few conversations in Korean saved in text files. and I'm loading it in a method with BufferReader with a following code:

Path read = Paths.get("conversation.txt");       
try {
        BufferedReader reader = Files.newBufferedReader(read, Charset.forName("euc-kr"));

        String lineS = reader.readLine();

        while (lineS != null) {

                System.out.println(lineS);

            }
            lineS = reader.readLine();
            sc.nextLine();
        }

    } catch (IOException e) {
        System.out.println("Cannot find the file");
        e.printStackTrace();
    }

This doesn't have any problem if I run it in Eclipse. But if I run it in CMD, it makes error.enter image description here

I almost finished making this game, and I'm going to send it to my friends. But it is making errors here. How could I resolve this problem?

James Kang
  • 61
  • 5
  • is this the code you have in Main.intro1() method (based on your stack trace)? – Gimhani Sep 13 '18 at 05:23
  • So `conversation.txt` is inside `jar` file when run from command line? Then it makes sense your file cannot be found. Also this has nothing to do with encoding of file – Piro Sep 13 '18 at 05:43
  • Piro, what does it mean when you said conversation.txt is inside jar file? what is jar file? conversation.txt file is in the regular java folder with bin folder and src folder – James Kang Sep 13 '18 at 05:46
  • So how are you running your program? Jar file is like zip file with java classes, see [jar tag info](https://stackoverflow.com/tags/jar/info). I expected you created jar file (There is export option in eclipse for that) to distribute your game. – Piro Sep 13 '18 at 05:57
  • Okay. So I just created Jar file. and it works. but it still doesn't read the file written in Korean inside... it shows an error – James Kang Sep 13 '18 at 06:17
  • at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) – James Kang Sep 13 '18 at 06:18
  • Possible duplicate of [How to read a file from jar in Java?](https://stackoverflow.com/questions/3369794/how-to-read-a-file-from-jar-in-java) – Piro Sep 13 '18 at 06:42

2 Answers2

-1

check the file path and also check the permission of file folder and file.

-1

Try to print the absolute path the programme point to. And make a zip file include this programme and the text file to avoid the NoSuchFileException. Or make a jar file including the text file with eclipse. How about export jar file like this?

萝莉w
  • 177
  • 11
  • I literally have no idea what you mean....:// if you could give me an example with visual, that would be a lot easier to understand.. – James Kang Sep 13 '18 at 05:51