打印指定文件练习 Java

huangapple go评论64阅读模式
英文:

Printing a Specified File Exercise Java

问题

以下是翻译好的内容:

import java.nio.file.Paths;
import java.util.Scanner;

public class PrintingASpecifiedFile {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("应该打印哪个文件的内容?");

        String fileName = scanner.nextLine();

        try (Scanner fileScanner = new Scanner(Paths.get(fileName))) {

            while (fileScanner.hasNextLine()) {
                String output = fileScanner.nextLine();
                System.out.println(output);
            }

        } catch (Exception e) {
            System.out.println("错误:" + e.getMessage());
        }
    }
}
英文:

Taking a beginners course in Java and I am stuck on one of the exercises. We're meant to print the text within a specific file, which we can find via the file's name which was inputted by a user. In our previous exercise, we learned that

    try(Scanner scanner = new Scanner(Paths.get("data.txt")))

would find the text within the file "data.txt", but I am unsure about how to convert this into finding any file name inputted by the user.

More details below.

Exercise: Write a program that asks the user for a string, and then prints the contents of a file with a name matching the string provided. You may assume that the user provides a file name that the program can find.

The exercise template contains the files "data.txt" and "song.txt", which you may use when testing the functionality of your program. The output of the program can be seen below for when a user has entered the string "song.txt". The content that is printed comes from the file "song.txt". Naturally, the program should also work with other filenames, assuming the file can be found.

My code so far is:

    import java.nio.file.Paths;
    import java.util.Scanner;

    public class PrintingASpecifiedFile {

        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("Which file should have its contents printed?");
    
            String fileName = scanner.nextLine();
    
          //try(Scanner scanner = new Scanner(Paths.get(fileName))) {
            try(scanner = Paths.get(fileName)) {    // this part of the code is underlined red
        
                while (scanner.hasNextLine()){
                    String output = scanner.nextLine();
                    System.out.println(output);
                }
        
            } 
            catch (Exception e){
                  System.out.println("Error: " + e.getMessage());
            }

          }
       }

I have tried searching how to add a new scanner as that was a suggestion but every time I've tried it gets an error. Also the "try" section is underlined red and cannot seem to figure out why. The underlined red part says variables in try-with-resources are not supported in -source 8

If anyone has tips, I would really appreciate it! Thank you!

答案1

得分: 0

你在控制台中使用了一个 Scanner 来读取文件路径。

你需要另一个 Scanner(或者其他读取文件的替代方法)来读取文件。

try (Scanner fileScanner = new Scanner(fileName)) {
try (Scanner fileScanner = new Scanner(Paths.get(fileName))) {
英文:

You used one Scanner for reading the file path from the console.

You need an other Scanner (or reader alternative) for reading the file.

try (Scanner fileScanner = new Scanner(fileName)) {
try (Scanner fileScanner = new Scanner(Paths.get(fileName))) {

huangapple
  • 本文由 发表于 2020年9月29日 22:24:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64121633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定