26

I have code that asks the user to input a log file name so it knows what file it is to parse and sort.

To be able to parse correctly, the FileSource.parseXML reads a variable type of File. So, the input String needs to be converted to a File. How would this be done?

import java.io.*;
import java.util.*;

public class Main
{
    public static void main( String[] args )
    {
        FileSource src;
        File fd=null;
        int rc = 0;
        int count = 0;
        Record rec;

      //ask user what file to parse
      Scanner input = new Scanner(System.in);
      System.out.println("Enter file name:");
      String filename = input.nextLine();

      //TODO turn filename into fd


        //parse the records
        src = FileSource.parseXML( fd );


        //print out the number of records parsed
        rc = src.getRecordCount();
        System.out.println(rc);


        //print out all records. 
        for( int i = 0; i < rc; i++)
        {
            rec = src.getRecord( i );
            System.out.println( rec.toString());
        } //end for loop

        return;
    }//end main method  

}
4

3 回答 3

48

File file = new File(userInput);

于 2012-07-20T16:27:18.553 回答
4

看到你有文件名,你应该可以创建一个文件:

File file = new File(filename);

有关文件的更多信息,请参阅此 javadoc

于 2012-07-20T16:27:27.613 回答
4

来自 Oracle 的 javadoc (v7) http://docs.oracle.com/javase/7/docs/api/

  File(String pathname) 
            Creates a new File instance by converting the given pathname string into an abstract pathname
于 2012-07-20T16:28:35.183 回答