【问题标题】:Opening a file and storing a message. Reading the message into a string variable to print. Java/Netbeans打开文件并存储消息。将消息读入字符串变量以进行打印。 Java/Netbeans
【发布时间】:2020-05-08 06:48:09
【问题描述】:

嘿,我正在做的这个练习需要一些帮助。我必须打开一个名为 hello.txt 的文件,然后存储消息“Hello World!”在文件中。然后我必须关闭文件并打开它,并将消息读入字符串变量以打印它。到目前为止,我在下面有这段代码。您对我如何成功编译此代码有什么建议吗?

package ProgrammingExercise11;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class Input {

    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner in = new Scanner(System.in);
        File inputFile = new File(hello.txt);
        Scanner in = new Scanner(inputFile);
        PrintWriter out = new PrintWriter(hello.txt);
        out.println("Hello, World!");
        String line = in.nextLine();
        in.close();
        out.close();
    }

}

【问题讨论】:

  • 如果您的代码无法编译,请告诉我们您的编译器给您的错误信息。

标签: java input netbeans output


【解决方案1】:

好的,我们来回答你的问题。

Scanner in = new Scanner(System.in);
        File inputFile = new File(hello.txt);
        Scanner in = new Scanner(inputFile);
        PrintWriter out = new PrintWriter(hello.txt);
        out.println("Hello, World!");
        String line = in.nextLine();
        in.close();
        out.close();

您的代码无法编译,因为您引入了两个具有相同名称的变量in,并且您没有声明hello.txt。 按照你的想法来解决它。

 public static void main(String[] args) throws Exception {
            String filePath = "hello.txt";
            File inputFile = new File(filePath);

            PrintWriter printWriter = new PrintWriter(inputFile);
            printWriter.write("Hello World!");
            printWriter.close();

            InputStream is = new FileInputStream(inputFile.getPath());
            BufferedReader buf = new BufferedReader(new InputStreamReader(is));
            String line = buf.readLine();
            System.out.println(line);

            is.close();
            buf.close();

    }

欢迎来到 java 世界!

【讨论】:

    【解决方案2】:

    从 Java 7 开始的 Files 类提供了对文件、目录或其他类型文件进行操作的静态方法,更加简单灵活。

    String file = "hello.txt";
    Path filePath = Paths.get(file);
    if (Files.isRegularFile(filePath)) {
        try(OutputStream fos = new FileOutputStream(file);
            InputStream fis = new FileInputStream(file)){
            // write message to file.
            String message = "Hello World!";
            byte[] messageBytes = message.getBytes();
            fos.write(messageBytes);
    
            // read message from file.
            byte[] receivedBytes = new byte[fis.available()];
            fis.read(receivedBytes);
            System.out.println(new String(receivedBytes));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 2018-09-01
      • 2013-10-18
      • 2014-10-15
      • 2018-12-28
      相关资源
      最近更新 更多