打开一定数量的java文件

打开一定数量的java文件,java,Java,当用户提示输入文件时,我想打开X个文件。如果用户决定打开文件1,则仅显示文件1。如果文件3提示,则将显示文件1到文件3等。如何执行此操作 System.out.print("Pick a file to open:"); String promptFile = keyboard.nextLine(); Scanner fileNumber = new Scanner(new File(promptFile )); 您可以将文件名存储在字符串数组中: String [] filenames =

当用户提示输入文件时,我想打开X个文件。如果用户决定打开文件1,则仅显示文件1。如果文件3提示,则将显示文件1到文件3等。如何执行此操作

System.out.print("Pick a file to open:");
String promptFile = keyboard.nextLine();
Scanner fileNumber = new Scanner(new File(promptFile ));

您可以将文件名存储在字符串数组中:

String [] filenames = {"file1", "file2", "file3"} //etc

然后,您只需使用范围从1到
promptFile
的循环变量运行一个循环,然后在循环中,您必须遍历
文件名的每个成员,并将其打开以供显示。

您可以将文件名存储在字符串数组中:

String [] filenames = {"file1", "file2", "file3"} //etc

然后,您只需要运行一个循环,循环变量范围从1到
promptFile
,然后在循环中,您必须遍历
文件名的每个成员,并打开它进行显示。

下面是一个简单的程序来满足您的要求

    String dir = ".";
    File directory = new File(dir);
    Map<Integer, String> map = new HashMap<>();
    int count = 0;
    for (File f : directory.listFiles()) {
        if (f.isFile())
            map.put(count++, f.getName());
    }
    System.out.println("Index -> Files");
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        System.out.println(e.getKey() + " -> " + e.getValue());
    }
    System.out.print("Enter index to open file ");
    Scanner scanner = new Scanner(System.in);
    int fileIndex = scanner.nextInt();
    scanner.close();
    String fileName = map.get(fileIndex);
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String str = null;
    while ((str = br.readLine()) != null) {
        System.out.println(str);
    }
    br.close();
String dir=“.”;
文件目录=新文件(目录);
Map Map=newhashmap();
整数计数=0;
对于(文件f:directory.listFiles()){
if(f.isFile())
map.put(count++,f.getName());
}
System.out.println(“索引->文件”);
对于(Map.Entry e:Map.entrySet()){
System.out.println(e.getKey()+“->”+e.getValue());
}
System.out.print(“输入索引以打开文件”);
扫描仪=新的扫描仪(System.in);
int fileIndex=scanner.nextInt();
scanner.close();
字符串文件名=map.get(fileIndex);
BufferedReader br=新的BufferedReader(新文件读取器(文件名));
字符串str=null;
而((str=br.readLine())!=null){
系统输出打印项次(str);
}
br.close();

您可以添加一个循环来获取用户的输入,并在目录中显示其他一些文件,添加条件以避免出现异常

    String dir = ".";
    File directory = new File(dir);
    Map<Integer, String> map = new HashMap<>();
    int count = 0;
    for (File f : directory.listFiles()) {
        if (f.isFile())
            map.put(count++, f.getName());
    }
    System.out.println("Index -> Files");
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        System.out.println(e.getKey() + " -> " + e.getValue());
    }
    System.out.print("Enter index to open file ");
    Scanner scanner = new Scanner(System.in);
    int fileIndex = scanner.nextInt();
    scanner.close();
    String fileName = map.get(fileIndex);
    BufferedReader br = new BufferedReader(new FileReader(fileName));
    String str = null;
    while ((str = br.readLine()) != null) {
        System.out.println(str);
    }
    br.close();
String dir=“.”;
文件目录=新文件(目录);
Map Map=newhashmap();
整数计数=0;
对于(文件f:directory.listFiles()){
if(f.isFile())
map.put(count++,f.getName());
}
System.out.println(“索引->文件”);
对于(Map.Entry e:Map.entrySet()){
System.out.println(e.getKey()+“->”+e.getValue());
}
System.out.print(“输入索引以打开文件”);
扫描仪=新的扫描仪(System.in);
int fileIndex=scanner.nextInt();
scanner.close();
字符串文件名=map.get(fileIndex);
BufferedReader br=新的BufferedReader(新文件读取器(文件名));
字符串str=null;
而((str=br.readLine())!=null){
系统输出打印项次(str);
}
br.close();

您可以添加一个循环来获取用户的输入,并在目录中显示其他文件,添加条件以避免出现
异常
s

您是否以相同的方式命名这些文件?@zulkarnain是的,只是不同的数字您是否以相同的方式命名这些文件?@zulkarnain是的,只是不同的数字而已