cmdline 中的命令行参数与 IDE 中的命令行参数

发布于 2025-01-01 18:28:28 字数 1030 浏览 0 评论 0原文

如果我从实际命令行运行以下代码(即 javac ..., java XXX.java (args[0]) (args[1]),则以下代码的行为符合预期。

但是,如果我尝试通过设置命令行参数eclipse 我收到“输入或输出文件错误”错误,但如果 eclipse 中的 cmd 行参数长度!= 2 我还会收到“必须指定输入文件......”所以我知道它正在分配它们

有谁知道什么处理的是这个吗?

public class main {

    public static Scanner fileScanner(String fName) throws FileNotFoundException {
        return new Scanner(new FileInputStream(fName));
    }

    public static PrintStream printStream(String fName) throws FileNotFoundException {
        return new PrintStream(new FileOutputStream(fName));
    }   

    public static void main(String[] args) {

        Scanner scan=null;
    PrintStream out=null;


    if(args.length != 2) {
        System.out.println("Must specify input file & output file on cmd line");
        System.exit(0);
    }


    try {
        scan = fileScanner(args[0]);
        out = printStream(args[1]);
    } catch(FileNotFoundException e) {
        System.out.println("Error with input or output file");
        System.exit(0);
    }

The following code behaves as expected if I run this from the actual command line (i.e. javac ..., java XXX.java (args[0]) (args[1]).

However if I try to set the command line args through eclipse I get the "Error with input or output file" error, but if the cmd line args in eclipse lenght != 2 I also get the "Must specify input file...." so I know it is assigning them

Does anyone know what the deal is with this?

public class main {

    public static Scanner fileScanner(String fName) throws FileNotFoundException {
        return new Scanner(new FileInputStream(fName));
    }

    public static PrintStream printStream(String fName) throws FileNotFoundException {
        return new PrintStream(new FileOutputStream(fName));
    }   

    public static void main(String[] args) {

        Scanner scan=null;
    PrintStream out=null;


    if(args.length != 2) {
        System.out.println("Must specify input file & output file on cmd line");
        System.exit(0);
    }


    try {
        scan = fileScanner(args[0]);
        out = printStream(args[1]);
    } catch(FileNotFoundException e) {
        System.out.println("Error with input or output file");
        System.exit(0);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云醉月微眠 2025-01-08 18:28:28

我试过你的程序,当我给出带有完整路径的文件名时,它在 Eclipse 中工作正常。

package so.ch1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class main {

    /**
     * @param args
     */


         public static Scanner fileScanner(String fName) throws FileNotFoundException {
                return new Scanner(new FileInputStream(fName));
              }

        public static PrintStream printStream(String fName) throws FileNotFoundException {
                return new PrintStream(new FileOutputStream(fName));
              }


        public static void main(String[] args) {

            Scanner scan=null;
            PrintStream out=null;


            if(args.length != 2) {
                System.out.println("Must specify input file & output file on cmd line");
                System.exit(0);
            }


            try {
                scan = fileScanner(args[0]);
                out = printStream(args[1]);
            } catch(FileNotFoundException e) {
                System.out.println("Error with input or output file");
                System.exit(0);
            }

        }
}

给出的参数: F:/temp/abc.txt F:/temp/xyz.txt

I tried you program, it works fine in eclipse when i give filename with complete path.

package so.ch1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class main {

    /**
     * @param args
     */


         public static Scanner fileScanner(String fName) throws FileNotFoundException {
                return new Scanner(new FileInputStream(fName));
              }

        public static PrintStream printStream(String fName) throws FileNotFoundException {
                return new PrintStream(new FileOutputStream(fName));
              }


        public static void main(String[] args) {

            Scanner scan=null;
            PrintStream out=null;


            if(args.length != 2) {
                System.out.println("Must specify input file & output file on cmd line");
                System.exit(0);
            }


            try {
                scan = fileScanner(args[0]);
                out = printStream(args[1]);
            } catch(FileNotFoundException e) {
                System.out.println("Error with input or output file");
                System.exit(0);
            }

        }
}

Args given: F:/temp/abc.txt F:/temp/xyz.txt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文