如何使用控制台从文件夹中选择任何文件?
How do use console to choose any file from a folder?
我需要能够通过在控制台上输入来选择文件夹中的任何文件。
代码
我希望能够通过控制台输入选择文件夹中的任何文件,而不是像 "bridge_rgb.png"
这样的预定义文件。
private void imageName() {
System.out.println("Select your image");
String input = scanner.next();
if (input.equals("bridge_rgb.png")) {
} else if (input.equals("gmit_rgb.png")) {
}
System.out.println("Image Selected");
}
在我的函数中我想做同样的事情,代码如下:
File file = new File("bridge_rgb.png");
BufferedImage source = ImageIO.read(file);
// processing: result gets created
File output = new File("bridge_grey.png");
ImageIO.write(result, "png", output);
我知道 GUI 会使文件选择变得容易,但我必须专门使用控制台。
假设您指定了一个文件夹,或者使用当前工作目录。
至少你需要两种原料:
- 一个函数,列出文件夹中给定的所有 文件 作为可供选择的选项
- 一个用户界面(基于文本或图形),允许显示这些选项并让用户从中进行选择(例如,通过点击下拉列表中的项目
列出当前目录中的文件
搜索 [java] list files in directory
并选择:
File[] files = directory.listFiles();
基于文本UI (TUI) 按数字选择选项
例如来自 How can I read input from the console using the Scanner class in Java?
// output on console
System.out.println("Choose from following files:");
for (int i=0, i < files.length(), i++) {
System.out.println(String.format("[%d] %s", i, file.getName());
}
// reading the number
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
// testing if valid, within options index (0..n)
if (i < 0 || i >= files.length()) {
System.out.println("Invalid input. Please enter a number from options.");
}
// file choosen
System.out.println("Your choice: " + file[i].getName());
// other UI operations, like display file, etc.
// IMPORTANT: always close the stream/scanner when finished
scanner.close();
我需要能够通过在控制台上输入来选择文件夹中的任何文件。
代码
我希望能够通过控制台输入选择文件夹中的任何文件,而不是像 "bridge_rgb.png"
这样的预定义文件。
private void imageName() {
System.out.println("Select your image");
String input = scanner.next();
if (input.equals("bridge_rgb.png")) {
} else if (input.equals("gmit_rgb.png")) {
}
System.out.println("Image Selected");
}
在我的函数中我想做同样的事情,代码如下:
File file = new File("bridge_rgb.png");
BufferedImage source = ImageIO.read(file);
// processing: result gets created
File output = new File("bridge_grey.png");
ImageIO.write(result, "png", output);
我知道 GUI 会使文件选择变得容易,但我必须专门使用控制台。
假设您指定了一个文件夹,或者使用当前工作目录。
至少你需要两种原料:
- 一个函数,列出文件夹中给定的所有 文件 作为可供选择的选项
- 一个用户界面(基于文本或图形),允许显示这些选项并让用户从中进行选择(例如,通过点击下拉列表中的项目
列出当前目录中的文件
搜索 [java] list files in directory
并选择:
File[] files = directory.listFiles();
基于文本UI (TUI) 按数字选择选项
例如来自 How can I read input from the console using the Scanner class in Java?
// output on console
System.out.println("Choose from following files:");
for (int i=0, i < files.length(), i++) {
System.out.println(String.format("[%d] %s", i, file.getName());
}
// reading the number
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
// testing if valid, within options index (0..n)
if (i < 0 || i >= files.length()) {
System.out.println("Invalid input. Please enter a number from options.");
}
// file choosen
System.out.println("Your choice: " + file[i].getName());
// other UI operations, like display file, etc.
// IMPORTANT: always close the stream/scanner when finished
scanner.close();