Re: How do I make this work?
newbie_at_jav, your mistake is a trivial one and I'm surprised that no
one else caught it: The method as you renamed it isn't a static
method. So, as a result, you need to call "FileReadingMethod" on an
instance of Solution_1 after having made one.
Solution_1 s1 = new Solution_1();
s1.FileReadingMethod("/tmp/test.txt");
As to using Strings instead of Files, Andrew, the decision comes from
the fact that the filename was being read from the args of the
program, and it just wasn't quite worth the time to convert over to a
File object compared to the possibility of just testing if it was
going to work and then letting it break 5 lines into execution if the
filename was typed in wrong.
However, in the future newbie_at_jav, You should definately look into
the File class: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
Perhaps something fun like this:
import java.io.File;
public class RandFile
{
public static void main(String[] args)
{
File di;
if(args.length == 0) di = new File(".");
else di = new File(args[0]);
String[] subitems = di.list();
File targ = new File(subitems[(int)
(Math.random()*subitems.length)]);
while(targ.isDirectory())
{
targ = new File(subitems[(int)(Math.random()*subitems.length)]);
}
System.out.println(targ);
}
}