加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 将文件作为命令行参数传递并读取其行

发布时间:2020-12-15 05:17:02 所属栏目:Java 来源:网络整理
导读:这是我在互联网上找到的用于读取文件行的??代码,我也使用 eclipse,并在其参数字段中将文件名称作为SanShin.txt传递.但它会打印: Error: textfile.txt (The system cannot find the file specified) 码: public class Zip { public static void main(String
这是我在互联网上找到的用于读取文件行的??代码,我也使用 eclipse,并在其参数字段中将文件名称作为SanShin.txt传递.但它会打印:

Error: textfile.txt (The system cannot find the file specified)

码:

public class Zip {
    public static void main(String[] args){
        try{
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream("textfile.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
              // Print the content on the console
              System.out.println (strLine);
            }
            //Close the input stream
            in.close();
            }catch (Exception e){//Catch exception if any
              System.err.println("Error: " + e.getMessage());
            }


    }
}

请帮我解释为什么会出现这个错误.
谢谢

解决方法

...
// command line parameter
if(argv.length != 1) {
  System.err.println("Invalid command line,exactly one argument required");
  System.exit(1);
}

try {
  FileInputStream fstream = new FileInputStream(argv[0]);
} catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}

// Get the object of DataInputStream
...

> java -cp ... Zip pathtotest.file

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读