这是一个java程序代码,它运行记事本程序并粘贴存储在此程序中的特定文本....
我想知道您是否可以向我解释String vbs
值,File file
以及("cscript //NoLogo " + file.getPath())
中的Process p
。
如果你是慷慨的,那么请解释整个代码。
我是Java的初学者,不完全如此,但如果你想从0到10判断我会是1.5 / 10
import java.io.File;
import java.io.FileWriter;
import javax.swing.JTextField;
public class PasteToNotepad {
public static void main(String[] args) throws Exception {
String text = "Some text for testing.";
JTextField textField = new JTextField(text);
textField.setSelectionStart(0);
textField.setSelectionEnd(text.length() - 1);
textField.copy();
String vbs = ""
+ "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
+ "WshShell.Run \"notepad\", 9\n"
+ "WScript.Sleep 500\n"
+ "WshShell.SendKeys \"^V\"";
File file = File.createTempFile("PrintDialog", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
p.waitFor();
}
}
答案 0 :(得分:2)
你基本上做的是:
String vbs = ...
)File file = File...
至fw.close()
)Process p = Runtime.getRuntime().exec(...)
)关于cscript //NoLogo
,这几乎与Java无关,这是一个Windows命令:
C:\Documents and Settings\bsharet>cscript
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.
Usage: CScript scriptname.extension [option...] [arguments...]
Options:
//B Batch mode: Suppresses script errors and prompts from displaying
//D Enable Active Debugging
//E:engine Use engine for executing script
//H:CScript Changes the default script host to CScript.exe
//H:WScript Changes the default script host to WScript.exe (default)
//I Interactive mode (default, opposite of //B)
//Job:xxxx Execute a WSF job
//Logo Display logo (default)
//Nologo Prevent logo display: No banner will be shown at execution time
//S Save current command line options for this user
//T:nn Time out in seconds: Maximum time a script is permitted to run
//X Execute script in debugger
//U Use Unicode for redirected I/O from the console
答案 1 :(得分:2)
虽然这个问题主要不是cscript //NoLogo
,但无论其标题如何,它仍然适用于该短语,所以让我们回答过多细节也是。
我不确定为什么他们称之为"徽标",但它正是您可能从内置帮助@MByD显示的内容。但是为了过度完整......
C:\prompt>cscript spam.js
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\prompt>cscript //NoLogo spam.js
C:\prompt>
因此,如果您需要管道输出并且不想要所有Microsoft样板文件,\\Nologo
- ify it。
C:\prompt>cscript spam.js > out.txt
C:\prompt>more out.txt
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
C:\prompt>cscript spam.js //NoLogo > out.txt
C:\prompt>more out.txt
C:\prompt>
(spam.js
中有var spam = "spam";
。)
而且,哇,这是一种将文本输入记事本的可怕方式。我猜它更多的是教授如何写一个文件和exec
来自Java的命令,或许?