2012-04-02 197 views
1

好吧,所以我有這兩個代碼都工作,但我不能爲我的生活弄清楚如何讓他們兩個一起工作。我希望能夠從第一段代碼中獲得最終結果,它告訴我用戶的絕對桌面路徑(即時通訊也在討論也來自域的用戶,因爲程序在沒有第一個代碼的情況下工作就可以找到如果用戶是基於用戶的桌面下車即到實際機器本身上的帳戶,但如果你登錄上一域的帳戶這就是它不工作。這裏是我的代碼,我把複製文件獲取絕對桌面路徑

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(); 
} 
    } 
} 

再次澄清我的問題。我有這兩段代碼。我的原始程序只有在用戶帳戶位於實際的計算機硬盤驅動器上時才起作用,但是,即使用戶位於域組中,帳戶信息是第二段代碼也應該找到用戶桌面的目錄從遠程位置的服務器檢索並且計算機登錄到該服務器,然後將信息保存回遠程服務器)。我想將這兩者結合起來,以便它可以在本地磁盤上的用戶以及來自域組的用戶上運行。與我一起工作的計算機只是窗口。

+0

根本沒有幫助...... – 2012-04-02 18:24:02

+0

這只是告訴你,你應該縮短你的問題,只保留與你的問題相關的代碼。 – talnicolas 2012-04-02 18:32:46

+0

你仍然明白我需要什麼,但我知道它不是最好的方式來提問,但是我在尋找答案,而不是問我問題的方法,我知道我沒有以最好的形式提問,但我是匆忙。發表評論會將其從特色問題中刪除,在這些問題中,實際上能夠幫助我的人可以看到,但現在已從其中移除,並且僅限於看到它的人,並且他們可能會選擇不回答。所以,如果你要發表評論,至少應該包括一些可以幫助我解決問題的代碼。謝謝。 – 2012-04-02 18:36:19

回答

1

如果你想獲得方法getCurrentUserDesktopPath()的結果在你的移動器類,你只需要把這一行你的主要方法:

String desktopPath = WindowsUtils.getCurrentUserDesktopPath(); 

由於這種方法是由靜態你不需要申報WindowsUtils對象。