public class Dir{
static BufferedReader DataInput = new BufferedReader(new InputStreamReader(System.in));
static String Directory ;
public static void main(String[] args) {
System.out.print("Write the directory you want to study: ");
try {
Directory = DataInput.readLine();
File Path = new File(Directory);
if(Path.exists() && Path.isDirectory()) {
File[] List = new File(Directory).listFiles();
int LongList = List.length;
for (int j = 0; j<List.length; j++) {
System.out.println(List[j]);
}
ClassThread[] MyThreads = new ClassThread[List.length];
for(int i = 0; i< LongList; i++) {
File MyFile = List[i];
String file = MyFile.getName();
int Pos = file.indexOf(".");
String Ext = file.substring(Pos + 1); //here we take the extension (all after dot)
MyThreads[i] = new MyThreads(MyFile);
MyThreads[i].start();
System.out.println(MyThreads[i].DevolverInfo()); // I THINK THE PROBLEM STARTS HERE
}
}
else {
System.out.println("This directory does not exits");
}
} catch (IOException e) {
System.out.println("ERROR: "+e);
}
}
}
class ClassThread extends Thread{
private File MyFile ;
private String Content;
ClassThread(File file){
MyFile = file;
System.out.println("Creating... "+ MyFile);
}
public void run() {
DateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Date now = new Date();
String filedate = dateformat.format(now);
long Longfile = MyFile.length();
String Name = MyFile.getName();
System.out.println("Running "+ Name);
if(MyFile.isDirectory()) {
Content = "[DIR] " + Name;
System.out.println(Content); //BY WRITING THIS, IT WORKS
}
else {
Content = "[FILE] " + Name + " " + Longfile + "bytes " + filedate;
System.out.println(Content);
}
}
public String DevolverInfo() {
return Content;
}
}
我正在尝试制作一个程序,以显示使用线程在用户输入路径中的所有文件和文件夹。想法是每个文件使用一个线程,并在控制台中显示它是FILE还是DIRECTORY,其SIZE及其最后修改日期。当我在Thread类的控制台中使用它打印时,它可以工作(您可以在代码中看到它),但是当我使用DevolverInfo()方法返回包含所有信息的字符串Content时,在我的main()中的for循环中,它返回null。
返回null的时刻在:
MisHilos[i].DevolverInfo();
感谢您的帮助!
我看了一眼,使它看起来更普通。您有几个地方可能会导致空指针异常。除此之外,您的概念行之有效……不行,因为Max Vollmer向盲人展示了我的身份……输出不是在等待结果产生。现在可以了。
我建议使用适当的Ide(vs代码,intellij ...),将此类错误指出并进行代码格式化。好吧,一些空指针错误,但不是同步问题。
public class Dir {
static String Directory;
public static void main(String[] args) {
System.out.print("Write the directory you want to study: ");
try {
Directory = "/home/ssmertnig/org/texte";
File Path = new File(Directory);
if (Path.exists() && Path.isDirectory()) {
File[] files = new File(Directory).listFiles();
int listSize = files == null ? 0 : files.length; // listFiles() can return null, then there's a problem
for (int j = 0; j < listSize; j++) {
System.out.println(files[j]);
}
ReadThread[] readThreads = new ReadThread[listSize];
for (int i = 0; i < listSize; i++) {
readThreads[i] = new ReadThread(files[i]);
readThreads[i].start();
}
// also dirt ugly, anyway ...
waitAbit(() -> Arrays.stream(readThreads).allMatch(r -> r.getContent() != null));
for (int i = 0; i < listSize; i++) {
System.out.println(readThreads[i].getContent());
}
} else {
System.out.println("This directory does not exits");
}
} catch (Exception e) {
System.out.println("ERROR: " + e);
}
}
private static void waitAbit(Supplier<Boolean> forWhat) {
while (! forWhat.get()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException();
}
}
}
}
class ReadThread extends Thread {
private File file;
private String content;
private final static DateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
ReadThread(File file) {
this.file = file;
System.out.println("Creating... " + this.file);
}
public void run() {
String filedate = dateformat.format(new Date());
String fileName = file.getName();
System.out.println("Running " + fileName);
if (file.isDirectory()) {
content = "[DIR] " + fileName;
System.out.println(content);
} else {
content = "[FILE] " + fileName + " " + file.length() + "bytes " + filedate;
System.out.println(content);
}
}
public String getContent() {
return content;
}
}