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