如何使用Perl编写Java代码以将PWD目录保存到新文件?

时间:2022-04-04 22:09:24

How to write Java code to save pwd (present working directory) to new file.

如何编写Java代码以将pwd(当前工作目录)保存到新文件中。

In java , Use Perl script inside java code itself.

在java中,在Java代码本身内部使用Perl脚本。

2 个解决方案

#1


1  

You can do it using Java only. For example Paths class.

您只能使用Java来完成。例如Paths类。

Path pwd = Paths.get("");
System.out.println(pwd.toAbsolutePath());

#2


0  

You can use Runtime.getRuntime().exec() or the Process API. You can then just call process.getInputStream() to get InputStream.

您可以使用Runtime.getRuntime()。exec()或Process API。然后,您可以调用process.getInputStream()来获取InputStream。

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Run {
    public static void main(String[] args) throws IOException {
        Process process = Runtime.getRuntime().exec("perl script.pl");
        InputStream inputStream = process.getInputStream();
        Scanner s = new Scanner(inputStream).useDelimiter("\\.");
        String result = s.hasNext() ? s.next() : "";
        System.out.println(result);

        //Creating a text file to write the output of the executed command
        //note that this will overwrite the file if it already exists
        PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
        writer.write(result);
        writer.close();

        // EDIT: 
        // writing commands to file name abc.txt
        List<String> cmdList = new ArrayList<String>();
        cmdList.add("echo '#!/bin/sh' >> ");
        cmdList.add("echo 'pwd' >> ");
        cmdList.add("echo 'output=$?' >>");
        cmdList.add("echo 'exit $output' >>");

        PrintWriter w = new PrintWriter("abc.txt");
        for (String cmd : cmdList) {
            w.println(cmd);
        }
        w.close();
    }
}

#1


1  

You can do it using Java only. For example Paths class.

您只能使用Java来完成。例如Paths类。

Path pwd = Paths.get("");
System.out.println(pwd.toAbsolutePath());

#2


0  

You can use Runtime.getRuntime().exec() or the Process API. You can then just call process.getInputStream() to get InputStream.

您可以使用Runtime.getRuntime()。exec()或Process API。然后,您可以调用process.getInputStream()来获取InputStream。

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Run {
    public static void main(String[] args) throws IOException {
        Process process = Runtime.getRuntime().exec("perl script.pl");
        InputStream inputStream = process.getInputStream();
        Scanner s = new Scanner(inputStream).useDelimiter("\\.");
        String result = s.hasNext() ? s.next() : "";
        System.out.println(result);

        //Creating a text file to write the output of the executed command
        //note that this will overwrite the file if it already exists
        PrintWriter writer = new PrintWriter("output.txt", "UTF-8");
        writer.write(result);
        writer.close();

        // EDIT: 
        // writing commands to file name abc.txt
        List<String> cmdList = new ArrayList<String>();
        cmdList.add("echo '#!/bin/sh' >> ");
        cmdList.add("echo 'pwd' >> ");
        cmdList.add("echo 'output=$?' >>");
        cmdList.add("echo 'exit $output' >>");

        PrintWriter w = new PrintWriter("abc.txt");
        for (String cmd : cmdList) {
            w.println(cmd);
        }
        w.close();
    }
}