为了账号安全,请及时绑定邮箱和手机立即绑定

这段代码应该从用户那里获取 N 个值。然后输入到.txt文件中

这段代码应该从用户那里获取 N 个值。然后输入到.txt文件中

陪伴而非守候 2023-07-28 10:07:00
这段代码应该从用户那里获取 N 个值。然后将值输入到 .txt 文件中。我无法在 .txt 文件中显示值。不知道为什么。// This program writes data into a file.import java.io.*; // Needed for File I/O class.import java.util.Scanner; // Needed for Scanner class.public class program01{    public static void main (String [] args) throws IOException    {        int fileName;        int num;        // Create a Scanner object for keyboard input.        Scanner input = new Scanner(System.in);        File fname = new File ("namef.txt");        Scanner inputFile = new Scanner(fname);  // Create a Scanner object for keyboard input.        FileWriter outFile = new FileWriter ("namef.txt", true);        PrintWriter outputfile = new PrintWriter("/Users/******/Desktop/namef.txt");        System.out.println("Enter the number of data (N) you want to store in the file: ");        int N = input.nextInt(); // numbers from the user through keyboard.        System.out.println("Enter " + N + " numbers below: ");        for ( int i = 1; i <= N; i++)        {            // Enter the numbers into the file.            input.nextInt();            outputfile.print(N);        }        System.out.println("Data entered into the file.");        inputFile.close(); // Close the file.    }} // End of class
查看完整描述

2 回答

?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

在你的程序中,你似乎已经扔掉了所有东西并希望它能起作用。要找出应该使用哪个类,您应该在 Java 版本的 Javadoc 中搜索它。 

打印作家:

将对象的格式化表示打印到文本输出流。此类实现了 PrintStream 中的所有打印方法。它不包含写入原始字节的方法,程序应使用未编码的字节流。

文件编写器:

使用默认缓冲区大小将文本写入字符文件。从字符到字节的编码使用指定的字符集或平台的默认字符集。

扫描仪(文件来源):

构造一个新的 Scanner,它生成从指定文件扫描的值。使用底层平台的默认字符集将文件中的字节转换为字符。

现在您可以看到每个课程的用途。PrintWriter 和 FileWriter 都用于写入文件,但 PrintWriter 提供更多格式选项,而 Scanner(文件源)用于读取文件而不是写入文件。


因为 PrintWriter 已经有了答案。我正在使用 FileWriter 编写此内容。

public static void main(String[] args) throws Exception {

    // Create a Scanner object for keyboard input.

    Scanner input = new Scanner(System.in);


    // You can provide file object or just file name either would work.

    // If you are going for file name there is no need to create file object

    FileWriter outputfile = new FileWriter("namef.txt");

    System.out.print("Enter the number of data (N) you want to store in the file: ");


    int N = input.nextInt(); // numbers from the user through keyboard.

    System.out.println("Enter " + N + " numbers below: ");

    for (int i = 1; i <= N; i++) {

        System.out.print("Enter the number into the file: ");

        // Writing the value that nextInt() returned.

        // Doc: Scans the next token of the input as an int.

        outputfile.write(Integer.toString(input.nextInt()) + "\n");

    }

    System.out.println("Data entered into the file.");

    input.close();

    outputfile.close(); // Close the file.

}

输出:


Enter the number of data (N) you want to store in the file: 2

Enter 2 numbers below: 

Enter the number into the file: 2

Enter the number into the file: 1

Data entered into the file.

文件:


2

1


查看完整回答
反对 回复 2023-07-28
?
达令说

TA贡献1821条经验 获得超6个赞

这是您想要实现的目标的一个工作变体:


import java.io.*; // Needed for File I/O class.

import java.util.Scanner; // Needed for Scanner class.


public class program01

{

    public static void main (String [] args) throws IOException

    {

        int fileName;

        int num;


        // Create a Scanner object for keyboard input.

        Scanner input = new Scanner(System.in);


        File fname = new File ("path/to/your/file/namef.txt");

        PrintWriter outputfile = new PrintWriter(fname);


        System.out.println("Enter the number of data (N) you want to store in the file: ");


        int N = input.nextInt(); // numbers from the user through keyboard.

        System.out.println("Enter " + N + " numbers below: ");

        for (int i = 1; i <= N; i++)

        {

            // Enter the numbers into the file.

            int tmp = input.nextInt();


            outputfile.print(tmp);

        }

        System.out.println("Data entered into the file.");

        outputfile.close(); // Close the file.

    }

}

对上面的几点评论:


1)删除多余的行:


 Scanner inputFile = new Scanner(fname);  // Create a Scanner object for keyboard input.

 FileWriter outFile = new FileWriter ("namef.txt", true);

其实你根本就没有使用过它们。


2)在PrintWriter我们传递File对象,而不是String。


3)在for循环中存在逻辑错误 - 在每次迭代中,您应该编写N而不是用户在控制台上输入的实际数字。


4)另一个错误是在最后一行关闭了错误的文件。


编辑:根据评论添加。在第 2 点)中,还有一种替代方法 - 您可以跳过创建File对象并String直接将其作为路径传递给甚至不存在的文件PrintWriter,如下所示:


PrintWriter outputfile = new PrintWriter("path/to/your/file/namef.txt");


查看完整回答
反对 回复 2023-07-28
  • 2 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信