如何从List <file>获取文件路径并将其放入另一个列表

时间:2016-03-24 12:00:32

标签: java list

EDIT 它是我的List listQuestion中的值的列表,现在我需要将pathFile添加为此列表中的额外值。

我的代码看起来像这样

private static List<String> listQuestionE(Scanner sc, List<File> listQuestion){
       //List with value from List<File> listQuestion ( its some value from .log files it doesn't matter
       List<String> question = new ArrayList<String>();
       //I think that I need to do something with File input1 but i try several things and it doesnt work for me.
       for(File input1 : listaQuestion){
           try {
              sc = new Scanner(input1);
           } catch (FileNotFoundException e) {
                e.printStackTrace();
           }

    while(sc.hasNextLine()){
        s = new Scanner(sc.nextLine());
        while(s.hasNext()){

            String words = s.nextLine();
            if(!getTagValues(words).isEmpty() ){
                try {

                    question.add(getTagValues(words).toString());

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
      }
    }
       return question;
   }

我不知道我需要添加代码以获取路径,如果我使用.getAbsolutePath befor我有这样的列表 [来自日志的值],[路径]或最差的[],[路径]因为有时候登录天堂错误所以答案是空的。

1 个答案:

答案 0 :(得分:0)

如果您尝试从File对象确定文件路径,则可以使用Java 7 nio。

    file.getAbsolutePath() // where file is a reference to File object

返回此抽象路径名的绝对路径名字符串。

private static List<String> listQuestionE(Scanner sc, List<File> listQuestion){

       List<String> question = new ArrayList<String>();
       for(File input1 : listQuestion){
           try {
              String fPath = input1.getAbsolutePath(); 
              sc = new Scanner(input1);
              question.add(fPath);
           } catch (FileNotFoundException e) {
                e.printStackTrace();
           }
       }
       return question;
}