2013-04-04 78 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