我昨天发布了这个,但我的代码很乱。我要做的是计算两个单独文件的行数,并在每个文件的单独线程中打印行号。这就是我所拥有的:
import java.io.File;
import java.util.Scanner;
public class fileReader implements Runnable
{
static int count = 0;
static int count1 = 0;
public void run()
{
try
{
Scanner file1 = new Scanner(new File("filetest1.txt"));
Scanner file2 = new Scanner(new File("filetest2.txt"));
while (file1.hasNextLine())
{
count++;
file1.nextLine();
}
while (file2.hasNextLine())
{
count1++;
file2.nextLine();
}
}
catch(Exception e)
{
count = -1;
count1 = -1;
}
}
public static void main(String[] args)
{
(new Thread(new fileReader())).start();
System.out.println("File one has " + count + " lines");
System.out.println("File two has " + count1 + " lines");
}
}
问题是它不起作用。有人可以指出我正确的方向吗?谢谢。