2012-02-02 4 views
1

実際のコマンドライン(javac ...、java XXX.java(args [0])(args [1])からこのコードを実行すると、次のコードが正常に動作します。cmdlineからのコマンドラインの引数対IDEの場合

eclipse経由でコマンドライン引数を設定しようとすると、 "入力または出力ファイルでエラーが発生する"というエラーが表示されますが、eclipse lenght!= 2でcmd行argを実行すると、 ....」私はそれが彼らに

誰でも契約はこれであるものを知っていますか?

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); 
    } 
+4

/temp/xyz.txtあなたがファイル名に渡しているので、あなたは絶対パスを渡していますか?私はあなたが相対パスを渡している場合は、ファイルを見つけることができないようにコマンドラインから実行するときとは別の作業ディレクトリを使用していると思われる。 – Chris

答えて

0

Iを割り当てている知っています私は完全なパスでファイル名を与えるとき、あなたはプログラムを試して、それは日食でうまく動作します。与えられた

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:F:/temp/abc.txt F:

関連する問題