2016-07-21 100 views
2

我正在嘗試創建並寫入.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. 
+0

歡迎來到堆棧溢出。如果您需要幫助解決您的問題,您需要向我們顯示實際的代碼。關於我的頭頂,也許你並沒有將緩衝區清理到磁盤。 –

+0

爲什麼在'pw.close();'之前的'do'和額外的'}'之前有一個額外的'{' – Bill

+0

我是一名初學者,因上次沒有花括號而受到抨擊。另外Imran,我試圖在詳細循環中獲取輸入。在我添加掃描儀項目之前,我剛剛爲「輸入項目名稱」「輸入項目成本」「輸入庫存數量」 –

回答

1

看來你並沒有真正寫出你想要的文件。你可以嘗試下面的代碼。

pw.println(itemName[counter] + ", " + itemCost[counter] + ", " + inStockNumber[counter]); 

兩個建議給你。

  1. 由於10代碼在您的代碼中無處不在。您最好將其提取到單個變量以實現更好的可維護性。
  2. 請按照java的naming convention。對於你的情況,類名的第一個字母應該大寫。使用InventoryStock代替庫存。

整個代碼如下所示,希望能對您有所幫助。謝謝。

import java.util.Scanner; 
import java.io.*; //import class for file input. 

public class InventoryStock { 
    public static void main(String args[]) throws Exception { 

     int size = 10; 
     // Declarations 
     String[] itemName = new String[size]; 
     double[] itemCost = new double[size]; 
     double[] inStockNumber = new double[size]; 
     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"); 
       itemName[counter] = input.next(); 
       System.out.print("Enter item cost"); 
       itemCost[counter] = input.nextDouble(); 
       System.out.print("Enter Number in stock"); 
       inStockNumber[counter] = input.nextDouble(); 

       pw.println(itemName[counter] + ", " + itemCost[counter] + ", " + inStockNumber[counter]); 

       counter += 1; 
      } while (counter < size); 
      fw.flush(); 
     } 
     fw.close(); 
     System.exit(0); 
    } // End of main method 
} // End of InventoryStock class. 
+0

這也是一種很好的方式來獲得我正在尋找的東西。所以我所犯的錯誤,但並沒有我原先想象的那麼糟糕。你們非常感謝你的幫助Gearon @Gearon –

+0

不客氣。 – Gearon

0

你還是得告訴PrintWriter寫入文件,否則它不會做任何事情,即使你抓住了用戶的輸入與input.next()。試試這樣的:

Scanner input = new Scanner (System.in); 
//Open output file. 
FileWriter fw = new FileWriter("updatedStock.txt"); 
PrintWriter pw = new PrintWriter(fw, true); 

do 
{ 
    System.out.print("Enter item name");  
    itemName[counter] = input.next(); 
    pw.write(itemName[counter]); 
    pw.println(); 

    System.out.print("Enter item cost"); 
    itemCost[counter] = input.nextDouble(); 
    pw.write(String.valueOf(itemCost[counter])); 
    pw.println(); 

    System.out.print("Enter Number in stock"); 
    inStockNumber[counter] = input.nextDouble(); 
    pw.write(String.valueOf(inStockNumber[counter])); 
    pw.println(); 

    counter += 1; 
}while(counter<10); 

pw.flush(); 
pw.close(); 
System.exit(0); 
+0

這樣做非常感謝您的幫助。我仍然在努力學習所有這些技巧和用來獲得我想要的效果的語言。我很感激。@ quidproquo –