27

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  

}
5
  • 3
    You mean something like File fd = new File(filename) ? Commented Jul 20, 2012 at 16:27
  • 1
    is this homework? because its kindof strange you got all the rest of that figure out, but couldn't find the constructor for File? Commented Jul 20, 2012 at 16:29
  • The answers already posted have addressed the simpler interpretation of the question. But are you asking about some complexity? Are you dealing with current directory problems, or something like that? In all cases, the java.io.File Javadoc provides a wealth of information. Commented Jul 20, 2012 at 16:30
  • 1
    I'm new to Java. I've been doing a lot of reading but it doesn't make sense to me until I see examples relating to projects I'm working on. These posts and feedbacks help a ton. Commented Jul 20, 2012 at 18:11
  • @user1541269, please stop trying to make pointless edits on other user's posts. You have tried a few times to add your own code and most ridiculously, to change the names of variables in the C# XNA Draggable UI accepted answer's code. That is inappropriate and has been rejected each time by multiple site moderators. Commented Jan 14, 2014 at 23:19

3 Answers 3

49

File file = new File(userInput);

Sign up to request clarification or add additional context in comments.

1 Comment

its sad that this question could be answered by something that wasn't even long enough to be a valid answer. fd = new File(fileName);
5

Seeing that you have the file name, you should just be able to make a File:

File file = new File(filename);

See this javadoc for more information about Files.

1 Comment

String str = Base64.encodeToString(byteArray, Base64.NO_WRAP); File file = new File(str); How to add my Base64 encoded string str in this File Format?
4

From Oracle's 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

2 Comments

You should use Java 6 or later, even when posting a link for documentation.
you're right, i wasn't paying attention :) it still works but I'll definitely be more careful in the future (as well as update this answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.