2 回答

TA貢獻(xiàn)1833條經(jīng)驗(yàn) 獲得超4個(gè)贊
在你的程序中,你似乎已經(jīng)扔掉了所有東西并希望它能起作用。要找出應(yīng)該使用哪個(gè)類,您應(yīng)該在 Java 版本的 Javadoc 中搜索它。?
打印作家:
將對(duì)象的格式化表示打印到文本輸出流。此類實(shí)現(xiàn)了 PrintStream 中的所有打印方法。它不包含寫入原始字節(jié)的方法,程序應(yīng)使用未編碼的字節(jié)流。
文件編寫器:
使用默認(rèn)緩沖區(qū)大小將文本寫入字符文件。從字符到字節(jié)的編碼使用指定的字符集或平臺(tái)的默認(rèn)字符集。
掃描儀(文件來源):
構(gòu)造一個(gè)新的 Scanner,它生成從指定文件掃描的值。使用底層平臺(tái)的默認(rèn)字符集將文件中的字節(jié)轉(zhuǎn)換為字符。
現(xiàn)在您可以看到每個(gè)課程的用途。PrintWriter 和 FileWriter 都用于寫入文件,但 PrintWriter 提供更多格式選項(xiàng),而 Scanner(文件源)用于讀取文件而不是寫入文件。
因?yàn)?PrintWriter 已經(jīng)有了答案。我正在使用 FileWriter 編寫此內(nèi)容。
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

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超6個(gè)贊
這是您想要實(shí)現(xiàn)的目標(biāo)的一個(gè)工作變體:
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.
}
}
對(duì)上面的幾點(diǎn)評(píng)論:
1)刪除多余的行:
Scanner inputFile = new Scanner(fname); // Create a Scanner object for keyboard input.
FileWriter outFile = new FileWriter ("namef.txt", true);
其實(shí)你根本就沒有使用過它們。
2)在PrintWriter我們傳遞File對(duì)象,而不是String。
3)在for循環(huán)中存在邏輯錯(cuò)誤 - 在每次迭代中,您應(yīng)該編寫N而不是用戶在控制臺(tái)上輸入的實(shí)際數(shù)字。
4)另一個(gè)錯(cuò)誤是在最后一行關(guān)閉了錯(cuò)誤的文件。
編輯:根據(jù)評(píng)論添加。在第 2 點(diǎn))中,還有一種替代方法 - 您可以跳過創(chuàng)建File對(duì)象并String直接將其作為路徑傳遞給甚至不存在的文件PrintWriter,如下所示:
PrintWriter outputfile = new PrintWriter("path/to/your/file/namef.txt");
添加回答
舉報(bào)