在Java中读取和写入.txt文件

时间:2013-04-04 02:28:11

标签: java file text

这是我的作业提示: 您的程序需要从文本文件中读取信息,而不是使用扫描程序从命令行读取。 您的程序还需要将消息写入文本文件,而不是在屏幕上显示消息。

我已编写并运行代码以提示使用CLI。但是,我完全不知道如何使用文本文件更改代码。有人可以向我解释一下吗?

到目前为止

代码:

import java.util.*;

public class parallelArrays {

    public static void main(String[] args) {
       String[] names;
       int[] exam1Grades;
       int[] exam2Grades;
       int n, sum1, sum2;
       double avg1,avg2;

       Scanner kb = new Scanner(System.in);

       // Get the number of students from the user
       System.out.print("Enter # of students:");
       n = kb.nextInt();

       // Allocate the arrays
       names = new String[n];
       exam1Grades = new int[n];
       exam2Grades = new int[n];

       // Input the names and grades
       for (int i=0; i<=names.length-1; i++) {
          System.out.print("Enter name for student #" + (i+1) + ":");
          names[i] = kb.next();
          System.out.print("Enter exam #1 grade:");
          exam1Grades[i] = kb.nextInt();
          System.out.print("Enter exam #2 grade:");
          exam2Grades[i] = kb.nextInt();
       }

       // Add up all the grades (could have been done in the above loop)
       sum1 = 0;
       sum2 = 0;
       for (int i=0; i<=names.length-1; i++) {
          sum1 = sum1 + exam1Grades[i];
          sum2 = sum2 + exam2Grades[i];
       }

       // Calculate and output the averages
       avg1 = sum1/n;
       System.out.println();
       System.out.println("Exam #1 average: " + avg1);

       avg2 = sum2/n;
       System.out.println("Exam #2 average: " + avg2);
       System.out.println();

       // Compare each grade to the average
       for (int i=0; i<=names.length-1; i++) {
       if (exam1Grades[i] > avg1)
           System.out.println("Student " + names[i] + " is above average on exam 1");
           else if (exam1Grades[i] < avg1)
           System.out.println("Student " + names[i] + " is below average on exam 1");
           else
           System.out.println("Student " + names[i] + " is average on exam 1");

       if (exam2Grades[i] > avg2)
           System.out.println("Student " + names[i] + " is above average on exam 2");
           else if (exam2Grades[i] < avg2)
           System.out.println("Student " + names[i] + " is below average on exam 2");
           else
           System.out.println("Student " + names[i] + " is average on exam 2");

       }
    }
}

4 个答案:

答案 0 :(得分:1)

像@Shobit先前所说的那样,将BufferedWriter与FileWriter一起使用,并使用write()和newLine()方法,您可以将所需的行插入文件而不是使用Println。

BufferedWriter writer = new BufferedWriter(new FileWriter("path-of-file")); //you don't need to create a File object, FileWriter takes a string for the filepath as well
writer.write("Student number..."); 
writer.writeLine(); //for a new line in the file

当你写完文件时,

writer.close();

答案 1 :(得分:0)

您可以像使用Scanner类一样使用它来读取文件的名称。您所要做的就是定义如何构建文件。例如,将您的信息与特殊字符分开,即:“,”并将其用作标记,以标识文件中名称,成绩等的不同字段(请参阅Pattern和{{3} }使用REGEX的API)

您可能想要创建一个Student类并将所需的字段映射到该类,将它们添加到List并更容易迭代。

至于写入文本文件,你已经在做了,请检查FileWriter以了解如何添加新行,这将是它。祝你好运!

答案 2 :(得分:0)

您可以使用BufferedReader和BufferedWriter来读取/写入Java中的文件。 BufferedReader构造函数接受FileReader对象,其构造函数接受File对象。它看起来像这样:

BufferedReader br = new BufferedReader(new FileReader(new File("path-of-file")));

然后,您可以使用BufferedReader的readLine()(或任何其他方法,具体取决于您的使用案例)从文件中逐行读取。

类似地,还有BufferedWriter和FileWriter以及用于编写的write()方法。

阅读/写作后关闭读者和作者流是一个好习惯。您可以使用流上的close方法执行相同的操作。

希望有所帮助。

答案 3 :(得分:0)

    import java.nio.file.path;
    import java.nio.file.paths;
    class FileCopy {
    public static void main(String args[])
    {
    Path sour =Paths.get("Source path");
    Path dest =Paths.get("destination path"); // The new file name should be given  

    or else FileAlreadyExistsException occurs.                                            

    Files.copy(sour, dest);
    }
    }