【问题标题】:JAVA Trying to use a QueueFileList How?JAVA 想用一个QueueFileList 怎么办?
【发布时间】:2018-10-18 12:42:58
【问题描述】:

您好,我的项目需要一个 FIFO 列表。我猜很简单。如果我上传了几个应该进入列表。如果文件加载成功,它将被删除,下一个可以继续。 之后我需要将它放入 JTable enter image description here

class QueueList {

    public static void main(String args[]) {

        BuildServerServerApplicationTests bq = new BuildServerServerApplicationTests();
        char[] arr = bq.filename;
        Queue<String> fifo = new LinkedList<String>();

        for (int i = 0; i < arr.length; i++)
            fifo.add (String.valueOf(new Integer (arr[i])));

        System.out.print (fifo.remove() + ".");
        while (! fifo.isEmpty())
            System.out.print (fifo.remove());
        System.out.println();
    }
}

这就是我所拥有的,但不知何故我错过了一些希望你能提供帮助的东西 谢谢

【问题讨论】:

    标签: java spring queue client fifo


    【解决方案1】:

    很难理解到底是什么问题,以及你想要做什么,所以我即兴创作了一点,但这应该有助于你的项目取得一些进展。

    在实现如何加载文件时记下我的 cmets。

    “之后我需要把它放到一个 JTable 中” - 我把这个留给你做。我再次假设您会将文本文件内容加载到 JTable 中。因此,您不必像我那样打印内容,而是必须解析 arrayContents 中的每一行文本,然后将其相应地插入到 JTable 对象中。

    祝你好运。 学生23

    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    public class Fifo {
    
    public static void main (String [] args) {
    
        // Your code says fifo.add (String.valueOf(new Integer (arr[i]))); 
        // I Assume arr is a char array of numbers resembling file names
        Character[] arr = {'1', '2', '3', '0'};
    
        System.out.println("Populating FIFO queue...\n");
    
        List<String> fifo = Stream.of(arr)      // create a stream from arr
                .map(String::valueOf)           // map each element to a String
                .collect(Collectors.toList());  // collect results
    
        System.out.println("FIFO queue  > " + fifo.toString());
    
        readAndAddToJTable(fifo);
    
        System.out.println("\nIs fifo empty? FIFO queue > " + fifo.toString());
    
        System.out.println("\nDone.");
    
    }
    
    // Read the file in the FIFO queue and add to JTable
    private static void readAndAddToJTable(List<String> fifoQueue) {
    
        System.out.println("\nProcessing FIFO queue...\n");
    
        while(!fifoQueue.isEmpty()) {
    
            String fileName = fifoQueue.remove(0); // Remove and return first element in fifo queue
    
            /*
             * 1) Load the file and do something with it.
             *      --- Note that you should rather use getResource() or getResourceAsStream()
             *          as its safer, but I am lazy. Using absolute paths can break your program
             *          if the directory structure were to change. You can find these methods
             *          in the Class class.
             * 
             * 2) Read the contents of each file and do something with its contents.
             *      --- I just print its contents out...
             *      --- You want read the contents and add it to your JTable. You will have
             *          to parse the file contents e.g. splitting the strings, etc.
             */
    
            File file = new File("C:\\Users\\Mathew\\StackOverflow\\src\\" + fileName); // dangerous!
    
            ArrayList<String> fileContents = readFileToArrayList(file); // store the text in my tester files
    
            if (fileContents != null) {
    
                // Do something with the contents (I print each line out, you add to JTable)
                for(int i = 0; i < fileContents.size(); i ++) { 
                    System.out.println("Contents of file " + file.getName() + " > " + fileContents.get(i));
                }
    
            } else {
                System.out.println("fileContents array = null");
            }
        }
    }
    
    // Read text file contents into an ArrayList and return the ArrayList.
    // This can probably be done with a Stream() as well, but I cant remember how.
    private static ArrayList<String> readFileToArrayList(File f) {
    
        ArrayList <String> content = new ArrayList <String> ();
        try {
            Scanner scanner = new Scanner(f);
            while(scanner.hasNextLine() == true) {
                content.add(scanner.nextLine()); // Add to end of the list
            }
            scanner.close();
        } catch(Exception e) {
            e.printStackTrace();;
            content = null;
        }
        return content;
    }
    

    }

    【讨论】:

    • Mh 现在的问题应该是,我如何添加而不是数字,文件?例如... character[] = {'1' '2' .. } 我想要 -> [] = { "c\User\temp\File.txt} 类似的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    相关资源
    最近更新 更多