3

这是一个运行记事本程序并粘贴存储在该程序本身中的特定文本的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();
   }
 }
4

2 回答 2

3

Though this question wasn't primarily about cscript //NoLogo, regardless of its title, it still googles well for that phrase, so let's answer that in too much detail too.

I'm not sure why they call it a "logo", but it's exactly what you might think from the built-in help @MByD displayed. But in the interest of over-completeness...

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>

So if you're piping output and don't want all that Microsoft boilerplate, \\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 has var spam = "spam"; in it.)

And, wow, that is a horribly convoluted way to get text into Notepad. I'm guessing it's more about teaching how to write to a file and exec a command from Java, perhaps?

于 2015-02-17T16:00:37.860 回答
2

你基本上做的是:

  1. 创建一个包含脚本的字符串 ( String vbs = ...)
  2. 将其写入文件(File file = File...to fw.close()
  3. Process p = Runtime.getRuntime().exec(...)通过调用 cscript ( )在单独的进程中执行此脚本

关于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
于 2012-08-06T11:16:43.643 回答