Вопрос по file, java – Использование аргумента командной строки для передачи файлов в программу

7

Как я могу получить файл в качестве аргумента командной строки?

Ваш Ответ

4   ответа
28

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

import java.io.File;

public class SomeProgram {
    public static void main(String[] args) {
        if(args.length > 0) {
            File file = new File(args[0]);

            // Work with your 'file' object here
        }
    }
}
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Johanna
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededjava.sun.com/docs/books/tutorial/getStarted/application/…Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded Johanna
2

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
0

Error: User Rate Limit ExceededC:\Program Files\file.datError: User Rate Limit Exceededargs[0]Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

import java.io.File;

public class SomeProgram {
    public static void main(String[] args) {
        String current = "";
        File lastFile = null;
        for(String str : args){
            File newFile = new File((current + " " + str).trim());
            if(newFile.exists()){
                lastFile = newFile;
            }
        current += " " + str;
        }
        File yourFile = lastFile;
    }
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
8

Error: User Rate Limit ExceededmainError: User Rate Limit ExceededargsError: User Rate Limit Exceeded

Error: User Rate Limit ExceededargsError: User Rate Limit Exceeded

[[email protected] ~]$ java Foo

Error: User Rate Limit Exceeded

[[email protected] ~]$ java Foo bar baz

Error: User Rate Limit ExceededbarError: User Rate Limit ExceededbazError: User Rate Limit ExceededargsError: User Rate Limit Exceededargs[0]Error: User Rate Limit Exceededargs[1]Error: User Rate Limit ExceededArrayIndexOutOfBoundsExceptionError: User Rate Limit Exceededargs.length.

Error: User Rate Limit ExceededmainError: User Rate Limit ExceededargsError: User Rate Limit Exceededargs[0]Error: User Rate Limit ExceededFileError: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

public class Foo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("no arguments were given.");
        }
        else {
            for (String a : args) {
                System.out.println(a);
            }
        }
    }
}

Error: User Rate Limit Exceeded

[[email protected] ~]$ java Foo
no arguments were given.
[[email protected] ~]$ java Foo bar baz
bar
baz

Похожие вопросы