简体   繁体   中英

FileInputStream path for exporting to jar

i am trying to export my project to .jar or .exe. This works fine expect for external text files.

Here my code:

    InputStream  is = new FileInputStream("MYPATH/start.txt");
    Reader r = new InputStreamReader(is);

  while ((my_char = r.read()) != -1) {
    text_list.add(String.valueOf((char) my_char));
  } 

How do i have to set "MYPATH" so my jar-File works with text for other PCs?

Perhaps you are looking for

FileReader r = new FileReader(mypath + "/start.txt");
// or
FileReader r = new FileReader(new File(mypath, "start.txt"));

I wouldn't try creating a List of all the bytes as Strings. Often reading a line at a time is more useful.

BufferedReader br = new BufferedReader(new FileReader(new File(mypath, "start.txt")));
for(String line; (line = br.readLine()) != null; ) {
    // process line
}
  URL res = getClass().getClassLoader().getResource("Interface/Text/start.txt");
  InputStream is = res.openStream();
   r = new InputStreamReader(is);             
   while ((my_char = r.read()) != -1) {
        text_list.add(String.valueOf((char) my_char));
    }

Thats my solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM