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

这段代码应该从用户那里获取 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


陪伴而非守候
浏览 108回答 2
2回答

潇潇雨雨

在你的程序中,你似乎已经扔掉了所有东西并希望它能起作用。要找出应该使用哪个类,您应该在 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: 2Enter 2 numbers below: Enter the number into the file: 2Enter the number into the file: 1Data entered into the file.文件:21

达令说

这是您想要实现的目标的一个工作变体:import java.io.*; // Needed for File I/O class.import java.util.Scanner; // Needed for Scanner class.public class program01{&nbsp; &nbsp; public static void main (String [] args) throws IOException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int fileName;&nbsp; &nbsp; &nbsp; &nbsp; int num;&nbsp; &nbsp; &nbsp; &nbsp; // Create a Scanner object for keyboard input.&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; File fname = new File ("path/to/your/file/namef.txt");&nbsp; &nbsp; &nbsp; &nbsp; PrintWriter outputfile = new PrintWriter(fname);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the number of data (N) you want to store in the file: ");&nbsp; &nbsp; &nbsp; &nbsp; int N = input.nextInt(); // numbers from the user through keyboard.&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter " + N + " numbers below: ");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= N; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Enter the numbers into the file.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int tmp = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputfile.print(tmp);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Data entered into the file.");&nbsp; &nbsp; &nbsp; &nbsp; outputfile.close(); // Close the file.&nbsp; &nbsp; }}对上面的几点评论:1)删除多余的行:&nbsp;Scanner inputFile = new Scanner(fname);&nbsp; // Create a Scanner object for keyboard input.&nbsp;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");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java