在Java中将文件路径作为参数传递 [英] Passing file path as an argument in Java

查看:1562
本文介绍了在Java中将文件路径作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用缓冲本地驱动器上的文件来解析并获取某些数据。出于测试目的,我很容易就能这样做:

I have been working with buffering a file on my local drive to parse and obtain certain data. For test purposes I was easily able to do it this way:

public static void main(String[] args) {

    fileReader fr = new fileReader();
    getList lists = new getList();


    File CP_file = new File("C:/Users/XYZ/workspace/Customer_Product_info.txt");
    int count = fr.fileSizeInLines(CP_file);
    System.out.println("Total number of lines in the file are: "+count);

    List<String> lines = fr.strReader(CP_file);

    ....

}

}

fileReader.java文件具有以下功能:

fileReader.java file has the following function:

public List<String> strReader (File in)
{
    List<String> totLines = new ArrayList<String>();

    try
    {
        BufferedReader br = new BufferedReader(new FileReader(in));
        String line;
        while ((line = br.readLine()) != null)
        {
            totLines.add(line);
        }
        br.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //String result = null;

    return totLines;
}

现在我希望文件路径作为命令行参数传递。我尝试了一些东西,但我对此并不熟悉并且无法使其正常工作。有人可以帮助解释我需要做出的所有更改,以便将更改合并到我的代码中。

Now I want the file path to be passed as a Command line Argument instead. I tried a few things but I am kind of new to this and wasnt able to make it work. Can someone please help and explain what all changes I need to make in order to incorporate that change in my code.

推荐答案

你需要要做到这一点:

public static void main(String[] args) {
    String path = args[0];
    // ... 
    File CP_file = new File(path);
    // ... 
}        

这篇关于在Java中将文件路径作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆