获取绝对桌面路径

时间:2012-04-02 18:16:50

标签: java file

好的,所以我有这两段代码都可以工作,但是我不知道如何让我们两个人一起工作。我希望能够从第一段代码中获得最终结果,它告诉我用户的绝对桌面路径(我正在谈论也来自域的用户,因为程序没有第一个代码就可以找到用户桌面,如果用户基于实际计算机本身的帐户,但如果您登录域上的帐户则不起作用。这是我用于复制文件的代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

 public class Mover
     public static void main(String[] args) throws IOException, InterruptedException    {   

        String currentdir = new File(".").getAbsolutePath();
        File TS3S = new File(currentdir + "/Teamspeak 3");
        File TS3D = new File(currentdir + "/TS3");
        File MinecraftLauncherS = new File(currentdir + "/Minecraft");
        File MinecraftLauncherD = new File(currentdir + "/Desktop");
        File ShortcutS = new File(currentdir + "/Shortcuts");
        File ShortcutD = new File(currentdir + "/Desktop");
        File MinecraftFilesS = new File(currentdir + "/minecraft files");
        File MinecraftFilesD = new File(currentdir + "/Application Data");
        File FrapsS = new File(currentdir + "/Fraps");
        File FrapsD = new File(currentdir + "/Fraps");

        //make sure source exists
        if(!TS3S.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(TS3S,TS3D);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!MinecraftLauncherS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(MinecraftLauncherS,MinecraftLauncherD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }


        //make sure source exists
        if(!MinecraftFilesS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(MinecraftFilesS,MinecraftFilesD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!ShortcutS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(ShortcutS,ShortcutD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        //make sure source exists
        if(!MinecraftLauncherS.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(FrapsS,FrapsD);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        System.out.println("Done");
        Runtime.getRuntime ().exec (currentdir + "/Desktop/TS3/ts3client_win32.exe");
        Runtime.getRuntime ().exec (currentdir + "/Desktop/Minecraft.exe");
        System.exit(0);
        }

    public static void copyFolder(File src, File dest)
        throws IOException{

        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
               //construct the src and dest file structure
               File srcFile = new File(src, file);
               File destFile = new File(dest, file);
               //recursive copy
               copyFolder(srcFile,destFile);
            }

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest); 

                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = in.read(buffer)) > 0){
                   out.write(buffer, 0, length);
                }

                in.close();
                out.close();
                System.out.println("File copied from " + src + " to " + dest);
        }
    }
}

以下是获取桌面绝对路径的代码(我认为如果用户从域网络登录,也可以使用。)

import java.io.*;

public class WindowsUtils {
  private static final String REGQUERY_UTIL = "reg query ";
  private static final String REGSTR_TOKEN = "REG_SZ";
  private static final String DESKTOP_FOLDER_CMD = REGQUERY_UTIL 
 + "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\" 
 + "Explorer\\Shell Folders\" /v DESKTOP";

  private WindowsUtils() {}

  public static String getCurrentUserDesktopPath() {
try {
  Process process = Runtime.getRuntime().exec(DESKTOP_FOLDER_CMD);
  StreamReader reader = new StreamReader(process.getInputStream());

  reader.start();
  process.waitFor();
  reader.join();
  String result = reader.getResult();
  int p = result.indexOf(REGSTR_TOKEN);

  if (p == -1) return null;
  return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (Exception e) {
  return null;
}
  }

  /**
   * TEST
   */
  public static void main(String[] args) {
    System.out.println("Desktop directory : " 
       + getCurrentUserDesktopPath());
  }


  static class StreamReader extends Thread {
private InputStream is;
private StringWriter sw;

StreamReader(InputStream is) {
  this.is = is;
  sw = new StringWriter();
}

public void run() {
  try {
    int c;
    while ((c = is.read()) != -1)
      sw.write(c);
    }
    catch (IOException e) { ; }
  }

String getResult() {
  return sw.toString();
}
  }
}

再次澄清我的问题。我有这两段代码。我的原始程序只有在用户帐户位于实际计算机硬盘驱动器上时才有效,但是,即使用户位于域组内,第二段代码也应该找到用户桌面的目录(帐户信息是从远程位置的服务器检索并且计算机登录到该服务器,然后将信息保存回远程服务器上。我想将这两者结合起来,以便它适用于本地磁盘内的用户和来自域组的用户。我正在使用的计算机只是窗口。

1 个答案:

答案 0 :(得分:1)

如果您想在Mover类中获取方法getCurrentUserDesktopPath()的结果,只需将此行放在main方法中:

String desktopPath = WindowsUtils.getCurrentUserDesktopPath();

由于此方法是静态的,因此您无需声明WindowsUtils对象。