I have some code that opens up a directory, some subdirectories and then creates File objects for each txt file. I then have a Scanner to scan through each file. When I use scan.nextLine() though, it returns the name of the file to me, rather than opening the file and giving me those Strings. Here is the code.
import java.io.*;
import java.util.Scanner;
public class DirTest
{
public void start()
{
String [] fileListing = null;
//directory code from project
File topDir = new File("TopDirectory");
if (topDir.isDirectory());
{
String [] dirList = topDir.list();
for(String name : dirList)
{
File midDir = new File("TopDirectory", name);
if(midDir.isDirectory())
{
fileListing = midDir.list();
for(String name2: fileListing)
{
File files = new File(name, name2);
System.out.println("Directory: " + midDir);
System.out.println("File: " + files);
// Scanner section, scans through fileListing[] files//
for(int i=0; i<3; i++)
{
Scanner scan = new Scanner(fileListing[i]);
String var1 = scan.nextLine();
System.out.println(var1);
}
}
}
}
}
}
}
Should I be opening each file in some way before I scan?
Thanks for any suggestions!
Try this: