简体   繁体   English

使用 JAVA 在命令行中实现

[英]Implement in command line using JAVA

In my project, i need to segregate all.java files in a folder and paste it in a separate folder.在我的项目中,我需要将 all.java 文件隔离在一个文件夹中,并将其粘贴到一个单独的文件夹中。 I figured out that the below mentioned command works for this purpose我发现下面提到的命令适用于此目的

for /f "delims==" %k in ('dir C:\Project\downloads\*.java /s /b') do copy "%k" C:\Project\javaRepo

In the above command, the Source Folder: C:\Project\downloads\在上述命令中,源文件夹:C:\Project\downloads\
Destination folder to copy all.java files: C:\Project\javaRepo复制所有的目标文件夹。java 文件:C:\Project\javaRepo

I tried using the following command in JAVA,我尝试在 JAVA 中使用以下命令,

Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(.....);

But I am not clear how to put the "for" command as an argument for rt.exec.但我不清楚如何将“for”命令作为 rt.exec 的参数。

I tried creating a batch file as follows, but it doesn't work.我尝试如下创建一个批处理文件,但它不起作用。

@echo off
for /f "delims==" %k in ('dir C:\Project\downloads\*.java /s /b') do copy "%k" C:\Project\javaRepo

Can you let me know if my approach is correct, or is there any other better alternative?你能告诉我我的方法是否正确,还是有其他更好的选择? Suggestions / ideas are most welcome.建议/想法是最受欢迎的。

If you are in Java you don't need all this trickery.如果您在 Java 中,则不需要所有这些技巧。 Just use for loop and copy files.只需使用 for 循环并复制文件。 You can call copy via exec for each file or you can copy using pure Java which would be lot better imho.您可以通过 exec 为每个文件调用复制,也可以使用纯 Java 进行复制,恕我直言。

Imagine a Java "clippy" the helpful paperclip:想象一个 Java “clippy” 有用的回形针:

It looks like you're trying to organise a java project!看起来您正在尝试组织 java 项目!

Perhaps you should be using a build tool like ant or gradle .也许您应该使用antgradle 之类的构建工具。 These can do those low level tasks in a very compact and convenient way.这些可以以非常紧凑和方便的方式完成那些低级任务。

If you must do it from Java, you can even use ant, say, as a library which can do these sorts of operations for you.如果您必须从 Java 开始,您甚至可以使用 ant 作为可以为您执行此类操作的库。

Or if you're willing to reorganise your project and follow the conventions maven may also be a more automatic solution.或者,如果您愿意重新组织您的项目并遵循约定maven也可能是一个更自动化的解决方案。

Thank you guys for new suggestions.. I looked into them as well.谢谢你们的新建议。我也调查了他们。 However, I able to implement the same commandline code later on... My code is as follows但是,我稍后可以实现相同的命令行代码......我的代码如下

public class copyJava {
    public static void main(String args[]) throws IOException,
            InterruptedException {
        Process p = null;
        String[] command = {
                "cmd",
                "/c",
                "for /f \"delims==\" %k in ('dir C:\\Project\\workspace\\downloads\\*.java /s /b') do copy \"%k\" C:\\Project\\workspace\\javaRepo" };
        ProcessBuilder copyFiles = new ProcessBuilder(command);
        copyFiles.redirectErrorStream(true);
        p = copyFiles.start();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                p.getInputStream()));
        String line;
        do {
            line = reader.readLine();
            if (line != null) {
                System.out.println(line);
            }
        } while (line != null);
        reader.close();
        p.waitFor();
    }
}

It works good now.. :)现在效果很好.. :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM