所以我有一個客戶端和一個服務器Java程序。客戶端使用Java processbuilder來執行腳本,但我的問題是用戶輸入需要傳遞給bash腳本的信息。因此,本質上,我需要知道如何將三個不同的字符串發送給由bash腳本讀取的三個不同變量。這個腳本正在複製一個文件,所以我寧願不用java創建一個txt文件,並讓腳本讀取文件。我也想要一種能夠在OS X和Windows上運行的方式,因此歡迎進行改進。我目前在Ubuntu上使用Java 7。從Java傳遞字符串到bash腳本
這裏是什麼,我試圖做一個片段: 的.java
Scanner bob = new Scanner(System.in);
String workingDirectory = new String(System.getProperty("user.dir"));
File tempDir = new File(workingDirectory);
String script = new String(workingDirectory + "/copyjava.sh");
System.out.print("Designate the location of the file: ");
String loc = bob.next();
System.out.print("Type the name of the file w/ extension: ");
String name = bob.next();
System.out.print("What is the location of THIS file? "); //I know there is a way to do this automagically but I can't remember how...
String wkspace = bob.next();
ProcessBuilder pb = new ProcessBuilder(script, loc, name, wkspace);
pb.start();
File myFile = new File (name);
腳本:
read loc
read name
read wkspace
cd $LOC
cp $name $wkspace
我剛剛開始編寫腳本,所以這對我來說都是新的。你將如何在Java中實現這個?我找不到在工作目錄以外的目錄中複製文件的任何內容。 – Mrcheese123 2012-07-25 23:51:47
SO搜索顯示此:http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java – jordanm 2012-07-25 23:56:01