java - JAVA 想用一个QueueFileList 怎么办?
问题描述
您好,我的项目需要一个 FIFO 列表。我猜很简单。如果我上传了几个应该进入列表。如果文件加载成功,它将删除,下一个可以去。之后我需要把它放到一个 JTable 在这里输入图像描述
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();
}
}
这就是我所拥有的,但不知何故我错过了一些希望你能帮助Thx的东西
解决方案
很难确切地理解问题是什么,以及您要做什么,所以我即兴创作了一点,但这应该有助于您的项目进展。
在实施如何加载文件时,请注意我的评论。
“在那之后我需要把它放到一个 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;
}
}
推荐阅读
- python - Babel 不返回语言?
- python - 如何使用列表(datetime.timedelta)中的循环特定日期并提取到不同的csv文件
- javascript - Visual Studio 2017 突然不会将新的 JavaScript 文件上传到 Azure
- python - Sklearn GridSearchCV 延迟
- c++ - gpa 计算器 LCV 无限循环问题 C++
- javascript - 尝试关闭带有填充表单的窗口时,如何获取“离开站点?您所做的更改可能不会保存”
- jquery - 有人可以解释一下为什么 Spring Mvc 说请求处理失败并抛出空指针异常
- git - mercurial 仅合并来自分支的更改
- html - 插入 Box-Shadow 和变换的问题:Chrome 上的 Scale()
- python-3.x - 从 h2o.grid.grid_search.H2OGridSearch 获取 H2O 神经网络的结果