1

I have two classes Main and Person

Main.java:

public class Main {
public static void main(String[] args) {
    ArrayList<Person> al=new ArrayList<>();
    try(Scanner sc = new Scanner(new File("C:/info.txt"))){
    while (sc.hasNextLine()) {
        Scanner s2 = new Scanner(sc.nextLine());
        while (s2.hasNext()) {
            String firstname = s2.next();
            String lastname = s2.next();
            String country = s2.next();
            Person p= new Person(firstname,lastname,country);
                al.add(p);
            }
        }
        sc.close(); 
    }
    catch(IOException ex){
       ex.getStackTrace();
    }
}}

Person.java:

public class Person {
String name;
String surname;
String country;
Person(String n,String s,String c){
    name=n;
    surname=s;
    country=c;
}}

As you see I use "info.txt" (to create Person objects and add them in ArrayList) which has his own address in my comp.So,I want to run this program in different computer,but because of "info.txt" has different address in different computer,I can't write code for all computers,so I wrote something like this:

    String classPath = System.getProperty("java.class.path");
    Path mypath = Paths.get(classPath,"project_name","info.txt");
    String finalpath = mypath.toString();


In my opinion,this code is not correct,and if it is true,I want the correct code :) thanks

fafasfa
  • 11
  • 1
  • "Correct" is relative to your requirements. I'd argue that your Main class is not correct, because you have too much happening in the main method. It'd be more useful as a PersonFactory class that could return a List of Person instances. I don't see why this would end up on another computer, unless you're trying to simulate a remote database. You can write a web service that will return serialized Person instances, but I'll bet that's too advanced right now. – duffymo Dec 22 '14 at 21:18

2 Answers2

2

MAVENize your project and then store it under Resources if you use eclipse have a look Convert Existing Eclipse Project to Maven Project

Community
  • 1
  • 1
dtrihinas
  • 446
  • 1
  • 4
  • 11
  • 1
    And once you store it under `resources`, you can access it with `getClass().getResourceAsStream(name)`. Name in this case is the file name. For this kind of access the file needs to be in the folders equivalent to the package of your class. –  Dec 22 '14 at 21:22
  • I dont use netbeans but this seems to do the job in regards to maven https://platform.netbeans.org/tutorials/nbm-maven-quickstart.html – dtrihinas Dec 22 '14 at 21:25
2

You can use the args parameter as William suggested. But if the file will always be inside to the application itself, then you should use resource loading (see link).

http://docs.oracle.com/javase/tutorial/deployment/webstart/retrievingResources.html

Here is a good tutorial: http://www.mkyong.com/java/java-read-a-file-from-resources-folder/

Simple resource loading code:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());

To help figure out where the resource is being loaded from in your situation, add this line:

System.out.println(file.getAbsolutePath());
Eric S.
  • 256
  • 2
  • 5
  • 2
    Consider adding key code from your links to your answer, and putting the links at the bottom of your answer. This will ensure the information is valid even if the link dies. – MeetTitan Dec 22 '14 at 21:29
  • It is **not correct** to convert a URL to a file using URL.getFile(). URL.getFile() returns the path portion of a URL, which is *not* a valid filename. For instance, if the filename contains a space, URL.getFile() will return a string containing "%20". Furthermore, if the resource URL points to an entry inside a .jar, it can't be converted to a file by any means. – VGR Dec 22 '14 at 23:17