2013-04-04 69 views
1

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

我已经编写并运行代码来提示使用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"); 

     } 
    } 
} 
+0

要修改通常你想将文件加载到一个变量,修改的东西,然后将其保存。 – 2013-04-04 02:34:44

+0

如果以下答案之一回答您的问题,请点击勾号接受答案。 http://stackoverflow.com/about – Shobit 2013-04-22 03:46:05

回答

0

您可以使用扫描仪类,你用它来读取文件的名称相同的方式。你所要做的就是定义你将如何构建你的文件。例如,用特殊字符(例如:“,”)分隔您的信息,并将其用作标记以标识文件中名称,等级等的不同字段(请参阅PatternScanner API以使用REGEX's)

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

至于写入文本文件,你已经在做它,检查FileWriter看看如何添加一个新的行,这就是它。祝你好运!

+0

感谢您的回应,我只是使用PrintLn语句编辑了我的代码。我如何将这个变成利用外行人条款中的文本文件?我对编码非常陌生,几乎不知道该怎么做。 – user2242998 2013-04-04 03:04:52

0

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

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

然后,可以使用BufferedReader中的readLine()(或任何根据您的使用情况下,其他的方法)的文件中读取行由行。

类似地,还有BufferedWriter和FileWriter和write()写入方法。

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

希望有所帮助。

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(); 
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); 
    } 
    } 
+0

这将适用于所有类型的文件...... – deeban 2014-08-18 12:57:34