ceri Westcott
ceri Westcott

Reputation: 45

User enters what the file name is they want to read in?

For the purpose of this class task, we have been asked to make a program that uses the File Class(I know input stream is much better) but yeah, we have to ask the user to input the name of the .txt file.

public class input {

public static void main(String[] args) throws FileNotFoundException {
    Scanner s = new Scanner(System.in);
    String name;
    int lineCount = 0;
    int wordCount = 0;


    System.out.println("Please type the file you want to read in: ");
    name = s.next();

    File input = new File("C:\\Users\\Ceri\\workspace1\\inputoutput\\src\\inputoutput\\lab1task3.txt");
    Scanner in = new Scanner(input);

How would I get

 File input = new File(...); 

to search for the file as just typing 'lab1task3' doesn't work.

edit: error -

 Exception in thread "main" java.io.FileNotFoundException: \lab1task3.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at inputoutput.input.main(input.java:19)

Upvotes: 0

Views: 104

Answers (2)

Evan Bechtol
Evan Bechtol

Reputation: 2865

Scanner can't read in files that way, you need to store it as a file first! If you put this inside of a try-catch block, you can ensure that the program won't break if a file isn't found. I would suggest wrapping it in a do-while/while loop (depending on structure), with the end condition being that the file is found.

I changed your main method to this and it compiles correctly:

public static void main(String[] args) throws FileNotFoundException {
    Scanner sc = new Scanner (System.in);

    System.out.println("Please type the file you want to read in: ");
    String fname = sc.nextLine();

    File file = new File (fname);
    sc.close();
}

Upvotes: 1

Cyphrags
Cyphrags

Reputation: 528

To search for a file inside a specific folder, you could just iterate over the files inside the given folder via:

File givenFolder = new File(...);
String fileName = (...);
File toSearch = findFile(givenFolder, fileName);

Where the function findFile(File folder, String fileName) would iterate over the files in the givenFolder and try to find the file. It could look like this:

public File findFile(File givenFolder, String fileName)
{
   List<File> files = getFiles();
   for(File f : files)
   {
     if(f.getName().equals(fileName))
     {
       return f;
     }
   }
   return null;
}

The function getFiles is just iterating over all files in the given folder and calls it self when finding a folder:

public List<File> getFiles(File givenFolder)
{
  List<File> files = new ArrayList<File>();
  for(File f : givenFolder.listFiles())
  {
    if(f.isDirectory())
    {
       files.addAll(getFiles(f));
    }
    else
    {
       files.add(f);
    }
  }
}

I hope this helps you :) If you want to know more about what happens here exactly feel free to ask :)

Upvotes: 0

Related Questions