我正在嘗試創建並寫入.txt文件,以便其他程序可以打開並讀取它。問題是輸入的數據沒有被寫入創建的文件。這是一個空白的.txt文檔。輸入的數據未寫入文件
import java.util.Scanner;
import java. io.*; //import class for file input.
public class inventoryStock
{
public static void main(String args[]) throws Exception
{
//Declarations
String[] itemName = new String [10];
double[] itemCost = new double [10];
double[] inStockNumber = new double [10];
int counter = 0;
//End declarations
Scanner input = new Scanner (System.in);
//Open output file.
FileWriter fw = new FileWriter("updatedStock.txt");
PrintWriter pw = new PrintWriter(fw);
do
{
System.out.print("Enter item name");
pw.println();
itemName[counter] = input.next();
System.out.print("Enter item cost");
pw.println();
itemCost[counter] = input.nextDouble();
System.out.print("Enter Number in stock");
pw.println();
inStockNumber[counter] = input.nextDouble();
counter += 1;
}while(counter<10);
pw.flush();
pw.close();
System.exit(0);
} //End of main method
} //End of InventoryStock class.
歡迎來到堆棧溢出。如果您需要幫助解決您的問題,您需要向我們顯示實際的代碼。關於我的頭頂,也許你並沒有將緩衝區清理到磁盤。 –
爲什麼在'pw.close();'之前的'do'和額外的'}'之前有一個額外的'{' – Bill
我是一名初學者,因上次沒有花括號而受到抨擊。另外Imran,我試圖在詳細循環中獲取輸入。在我添加掃描儀項目之前,我剛剛爲「輸入項目名稱」「輸入項目成本」「輸入庫存數量」 –