1

Unable to parse the file name. The main reason is new File is expecting pathName but I am passing String. How to convert String to pathName?

        //Creating listIterator to iterate over list
        ListIterator<String> listIterator= listOfFiles.listIterator();
        while(listIterator.hasNext()) {
            fileName=listIterator.next();
            parser.beginParsing(new File(fileName));
        }
        while ((row = parser.parseNext()) != null) {
            System.out.println( Arrays.toString(row));
        }
        parser.stopParsing();
    }
IKo
  • 4,998
  • 8
  • 34
  • 54
Ria Sachdeva
  • 113
  • 6
  • What is the type for the parameter to method `beginParsing()` in the code you posted? Is it [Path](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Path.html)? – Abra Feb 08 '20 at 08:07
  • Please add stack trace. – IKo Feb 08 '20 at 08:28
  • The beginParsing takes pathName. – Ria Sachdeva Feb 08 '20 at 16:14
  • Stack Trace: Exception in thread "main" java.lang.IllegalArgumentException: java.io.FileNotFoundException: fileName (The system cannot find the file specified) at com.univocity.parsers.common.ArgumentUtils.newReader(ArgumentUtils.java:354) at com.univocity.parsers.common.ArgumentUtils.newReader(ArgumentUtils.java:330) at com.univocity.parsers.common.AbstractParser.beginParsing(AbstractParser.java:592) at com.modeling.association.commits.heuristicmodeling.services.strategy.ParsingStrategy.parseData(ParsingStrategy.java:29) at – Ria Sachdeva Feb 08 '20 at 16:27
  • First of all, [edit] your question and add the stack trace. Don't post the stack trace as a comment. Secondly, is class `ParsingStrategy` a class that you wrote? Or is it part of some 3rd party library you are using? – Abra Feb 08 '20 at 19:43

1 Answers1

0

Here are the available constructors for the File class:

File(File parent, String child)
File(String pathname)
File(String parent, String child)
File(URI uri)

I assume that you are using this one:

 File(String pathname)

If so, the type of the parameter is String and pathname is the name of the parameter. So you just need to provide a proper path to your file. No conversion is needed.

IKo
  • 4,998
  • 8
  • 34
  • 54
  • I am getting this exception: Caused by: java.io.FileNotFoundException: "C:\Users\Carnoll\Desktop\IBM Project Details\CSV Files\yetus.csv" (The filename, directory name, or volume label syntax is incorrect) I tried this and it is working: parser.beginParsing(new File("C:\\Users\\Carnoll\\Desktop\\IBM Project Details\\CSV Files\\yetus.csv" )); I am not sure while passing the parameter like below it is throwing file not found exception fileName=listIterator.next(); parser.beginParsing(new File(fileName)); – Ria Sachdeva Feb 08 '20 at 16:05
  • "C:\\Users\\Carnoll\\Desktop\\IBM Project Details\\CSV Files\\yetus.csv" This string is treated as "C:\Users\Carnoll\Desktop\BM Project Details\CSV Files\yetus.csv" And, this is illegal path. May I know how to workaround the escape characters? – Ria Sachdeva Feb 08 '20 at 16:28