简体   繁体   English

我无法访问存储在文件数组中的一系列文件

[英]I can't access a series of files stored in in a File Array

I want to read all the files from a specific folder and then read them one-by-one and use a greedy algorithm.我想从特定文件夹中读取所有文件,然后一个接一个地读取它们并使用贪心算法。 My code successfully does the first part but I have a problem on the second.我的代码成功完成了第一部分,但第二部分有问题。 My code is as follows:我的代码如下:

import java.io.File;
import java.io.IOException;
public class Comparisons {
    
    public static void main(String[] args) {
        File folder = new File("C:\\Users\\vstou\\OneDrive\\Desktop\\data\\");
        File[] listOfFiles = folder.listFiles();{
            System.out.println(listOfFiles.length);
            for (int i = 0; i < listOfFiles.length; i++) {
                System.out.println(listOfFiles[i]);
            }
            for (int i = 0; i < listOfFiles.length; i++) {
                File file = listOfFiles[i];
                System.out.println(file);
                Greedy.main(new String[] {file.getName()});
                System.out.println("---------------------------------------");
                Sort.main(new String[] {file.getName()});
            }
    }
    }
}

And I get the following error when I run the program.运行程序时出现以下错误。

the error I get我得到的错误

The Greedy Method is as follows (The Sort method is similar) Greedy方法如下(Sort方法类似)

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.IllegalArgumentException;
import java.util.Scanner;
public class Greedy {

public static void main(String[] args) {
    String content1 = "";
    String content2 = "";
    String line;
    int processorsNumber = 0;
    int tasksnumber = 0;
    int value1 = 0;
    int value2 = 0;
    Task task = null;
    Processor temp = null;
    int makespan = 0;
    int processorId = 1;
    
    try {
        int actualTasksNumber = 0;
        File file = new File(args[0]);
        Scanner myReader = new Scanner(file);
        line = myReader.nextLine();
        processorsNumber = Integer.parseInt(line);
        line = myReader.nextLine();
        tasksnumber = Integer.parseInt(line);
        MaxPQ heap = new MaxPQ(processorsNumber);
        for (int i = 0; i < processorsNumber; i++) {
            heap.insert(new Processor(processorId++,new TaskQueue()));
        }
        //heap.output();
        while (myReader.hasNextLine()) {
            line = myReader.nextLine();
            for (int i = 0; i < line.length(); i++) {
                while (line.charAt(i) < '0' || line.charAt(i) > '9') {
                    i++;
                }
                while (line.charAt(i) >= '0' && line.charAt(i) <= '9') {
                    content1 += line.charAt(i);
                    i++;
                }
                while (line.charAt(i) < '0' || line.charAt(i) > '9') {
                    i++;
                }
                while (line.charAt(i) >= '0' && line.charAt(i) <= '9') {
                    content2 += line.charAt(i);
                    if (i < line.length()-1) {
                        i++;
                    } else {
                        break;
                    }
                }
            }
            value1 = Integer.parseInt(content1);
            value2 = Integer.parseInt(content2);
            //System.out.println(value1 + " " + value2);
            content1="";
            content2="";
            task = new Task(value1, value2);
            if (heap.size() > 0) {
                temp = heap.getmax();
                temp.getProcessedTasks().put(task);
                heap.insert(temp);
                actualTasksNumber++;
                value1=0;
                value2=0;
            } else {
                throw new IllegalArgumentException("Invalid number of processors");
            }
        }
        myReader.close();
        if (actualTasksNumber != tasksnumber) {
            throw new IllegalArgumentException("Invalid number of tasks given");
        }
        if (tasksnumber < 50) {
            heap.output();
        }
        temp = heap.getmin();
        makespan = temp.getActiveTime();
        heap.insert(temp);
        System.out.println("Makespan = " + makespan);
    } catch (FileNotFoundException e) {
        System.out.println("The file " + args[0] + " does not exist");
        e.printStackTrace();
    }
}
}```

Change the Parameter in Greedy.main() to a File.将 Greedy.main() 中的参数更改为文件。 (The reason is that you try to use the file.getName() as a parameter to re-instantiate the file. That cant be done) (原因是您尝试使用 file.getName() 作为参数来重新实例化文件。不能这样做)

Here your main Class:这是您的主要 Class:

File folder = new File("C:\\Users\\vstou\\OneDrive\\Desktop\\data\\");
    File[] listOfFiles = folder.listFiles();
    
    System.out.println(listOfFiles.length);
    for (int i = 0; i < listOfFiles.length; i++) {
        System.out.println(listOfFiles[i]);
    }
    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        System.out.println(file);
        Greedy.main(file);
        System.out.println("---------------------------------------");
        //Sort.main(new String[]{file.getName()});
    }

Here your Greedy-Class (needed to upload it cause stackoverflow didnt want to): https://just-paste.it/4h72iX7llV在这里你的贪婪类(需要上传它导致stackoverflow不想): https://just-paste.it/4h72iX7llV

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM