Anton G
Anton G

Reputation: 1

How to read from a text with FileRead?

Im working on a project from two computers, the first one at work and the second one is home. And when I use FileRead I need to change the place that the FileRead needs to read from. For instance at home its like this:

ReadFile rf = new ReadFile("C:\\Users\\Home\\Desktop\\Project\\src\\triangle3d\\TheText.ase");

And when I Email myself this and open it in my workplace I need to change it to this every time:

ReadFile rf = new ReadFile("C:\\Users\\Work\\Desktop\\Project\\src\\triangle3d\\TheText.ase");

Now is there a way that will find TheText.ase without stating its place? will this work? :

ReadFile rf = new ReadFile("TheText.ase");

Upvotes: 0

Views: 72

Answers (4)

SubOptimal
SubOptimal

Reputation: 22993

You can access the file by it's relative pathname in the project tree.

private static void answer() {
    final String relativePath = "src/triangle3d/TheText.ase";
    File ase = new File(relativePath);
    System.out.printf("absolute path: '%s'%n", ase.getAbsolutePath());
    System.out.printf("is '%s' a file: %s%n", relativePath, ase.isFile());
}

Upvotes: 0

Roan
Roan

Reputation: 1206

An possibility would be to find you Desktop folder and from there go to the file as that is the same on both machines.

ReadFile rf=new ReadFile(System.getProperty("user.home") + File.separator + "Desktop" + "\Project\src\triangle3d\TheText.ase");

This might even be better than my other answer as it does not depend on DNS so internet acces is not required.

Hope this helps :)

Upvotes: 1

Roan
Roan

Reputation: 1206

You could make a method that looks on wich computer you are and then loads the file: The only thing you need to do is enter the name of your work computer and the name of your home computer in the is statement below. The name is you network name or the name you gave the computer when you first started it.

    String name = InetAddress.getLocalHost().getHostName();
    if(name.equals("computerNameHome")){//home computer name
        ReadFile rf=new ReadFile("C:\Users\Home\Desktop\Project\src\triangle3d\TheText.ase");   
    }else if(name.equals("computerNameWork")){//work computer name
        ReadFile rf=new ReadFile("C:\Users\Work\Desktop\Project\src\triangle3d\TheText.ase");   
    }

Hope this helps :)

EDIT: this does depend on DNS so you need internet acces to do this.

Upvotes: 0

DreadHeadedDeveloper
DreadHeadedDeveloper

Reputation: 620

public class LocationFileManager
{

   public LocationFileManager(String s)
   {

      ReadFile rf=new ReadFile("C:\Users\\" + s + "\Desktop\Project\src\triangle3d\TheText.ase");

   }

   public static void main(String[] args)
   {

      String s = ""; //<--------- enter here either Home or Work

      LocationFileManager lfm = new LocationFileManager(s);

   }

}

This works by you simply choosing whether it is home or work by shanging the value of s in the main method

EDIT --- Hell, if that's still too much work, then try this....

import javax.swing.JOptionPane;

public class LocationFileManager
{

   public LocationFileManager(String s)
   {

      ReadFile rf=new ReadFile("C:\Users\\" + s + "\Desktop\Project\src\triangle3d\TheText.ase");

   }

   public static void main(String[] args)
   {



      Object[] possibleValues = { "Home", "Work"};
      String s = JOptionPane.showInputDialog(null,
         "Choose one", "Input",
         JOptionPane.INFORMATION_MESSAGE, null,
         possibleValues, possibleValues[0]);




      LocationFileManager lfm = new LocationFileManager(s);

   }

}

Upvotes: 1

Related Questions