2016-03-02 118 views
0

首次執行程序時,打開外部文件,從中讀取數據並將其放入數組中,然後關閉文件。用程序提交外部文件。 要第一次填充外部文件,將代碼硬編碼到您的程序中,將數據發送到外部文件。你只需要做一次 - 就在你創建第一個外部文件的時候。 當從驅動程序的菜單中選擇退出選項時,打開備份外部文件,將陣列保存到文件,然後關閉文件。
一個警告 - 不要將用於打印的數據格式化爲屏幕格式,以及如何格式化陣列中的數據。在處理從外部文件中提取數據時,您必須將數據保存到文件中,正如您從文件中讀取數據一樣。將數據發送到外部文件

***我的數據沒有保存到文件中。有人能指點我嗎?我需要將數據發送到外部文件,以及如何不將數據發送到那裏。程序運行但不會將數據發送到文件。

import java.io.*; 
    import java.util.Scanner; 
    public class Driver 
    { 
     public static void main(String[] args) 
     { 
      //local constants 
     //local variables 
     String fileName = "items.txt"; 
     Scanner scanner = null; 
     ItemsList itemsList = new ItemsList(5); 
     int i = 0; 
     int choice = 0; 
     String itemName; 
     String name; 
     double price = 0; 
     int qty; 
     String cho = "y"; 

    Scanner scan = new Scanner(System.in);  

    do 
    { 
     //Ask the user the following 
     System.out.println("Menu"); 
     System.out.println("1. Add item"); 
     System.out.println("2. Display items"); 
     System.out.println("3. Exit"); 
     System.out.println("Enter your choice"); 
     choice = scan.nextInt(); 

     switch(choice) 
     { 
      //Add an item to the itemsList 
      case 1: 
       //Ask user for to enter information 
       System.out.println("\tEnter item name : "); 
       name=scan.next(); 

       System.out.println("\tEnter price : "); 
       price = scan.nextDouble(); 

       System.out.println("\tEnter quantity : "); 
       qty= scan.nextInt(); 

       //Add the OneItem to the itemsList 
       itemsList.addItem(new OneItem(name, price, qty)); 
       break; 

      case 2: 
       //print the list 
       //print heading with specific formatter 
       System.out.printf("%-10s%-10s%-10s\n\n", "Item","Price","Quantity"); 
       System.out.println(itemsList.toString()); 
       break; 

      case 3: 
       //Terminate the program 
       System.out.println("Terminate the program."); 
       break; 

      default: 
       System.out.println("Incorrect option is selected."); 
       break; 
     } 
     //Ask user if they would like to continue 
     System.out.print("\t\tWould to like to continue <(Y or N)> : "); 
     cho = scan.next(); 
     System.out.println(); 

    } 
    while(cho.equalsIgnoreCase("Y")); 


    //open the file and catch the exception if file not found 
    try 
    {   
     //create an instance of scanner 
     scan = new Scanner(new File(fileName)); 

     //read file items until end of file 
     while(scan.hasNext()) 
     { 
      itemName = scan.next(); 
      price = scan.nextDouble(); 
      qty=scan.nextInt(); 

      //Add the OneItem object to the itemList 
      itemsList.addItem(new OneItem(itemName, price, qty)); 
      i++; 
     } 

     //close the file object 
     scan.close();   
    } 
    catch (FileNotFoundException e) 
    { 
     System.out.println(e.getMessage()); 
    } 

    writeToFile(itemsList); 
} 

    private static void writeToFile(ItemsList itemsList) 
    { 

    //Create a file name called items.txt 
    String filename="items.txt"; 
    //Create a variable of Class PrintWriter 
    PrintWriter filewriter=null; 

    try 
    { 
     //create an instance of PrintWriter 
     filewriter=new PrintWriter(new File(filename)); 

     //close the file writer 
     filewriter.close(); 
    } 
    catch (IOException e) 
    { 
     System.out.println(e.getMessage()); 
    } 
} 


}//end Driver class 

=========================

public class ItemsList 
{ 
    //declare variables 
    private OneItem items[]; 
    private int size; 
    private int count; 

    //constructor to set items, size and count to zero 
    public ItemsList() 
    { 
     items = null; 
     size = 0; 
     count = 0; 
    } 
    //Parameter constructor 
    public ItemsList(int size) 
    { 
     items = new OneItem[size]; 

     for (int i = 0; i < items.length; i++) 
     { 
      items[i] = new OneItem(); 
     } 

     this.size = size; 
     count = 0; 
    } 
    //Add OneItem to the itemlist 
    public void addItem(OneItem item) 
    { 
     if(items.length == count) 
     { 
      resize(); 
      items[count] = item; 
      count++; 
     } 
     else 
     { 
      items[count] = item; 
      count++; 
     }  
    } 

    //Resize 
    private void resize() 
    { 
     int oldsize = size; 
     count = oldsize; 
     int newsize = 2 * this.size; 
     size = newsize; 

     OneItem[] tempList = new OneItem[size]; 

     for (int i = 0; i < oldsize; i++)  
      tempList[i] = items[i]; 


     items = new OneItem[size]; 
     items = tempList;  
    } 

    //getSize 
    public int getSize() 
    { 
     return count; 
    } 

    //toString 
    public String toString() 
    { 
     String description = ""; 
     for (int i = 0; i <count; i++) 
     { 
      description += items[i].toString(); 
     } 

     return description; 
    } 
} 

=========== =========

public class OneItem 
{ 
    //declare a variables 
    private String name; 
    private double price; 
    private int quantity; 

    //default constructor 
    public OneItem() 
    { 
     name = ""; 
     price = 0; 
     quantity = 0; 
    } 
    //parameter constructor 
    public OneItem(String name, double price, int quantity) 
    { 
     this.name = name; 
     this.price = price; 
     this.quantity = quantity; 
    } 

    //toString 
    public String toString() 
    {  
     return String.format("%-10s%-10.2f%-10d\n", name,price,quantity); 
    } 
}//end of the OneItem class 

回答

0
  • 將writeToFile()不寫任何東西到文件。
  • 它只是創建一個實例並關閉。
  • 您可以迭代itemsList並使用println(OneItemObject)將每個OneItem打印到文件中 - toString()應負責給出需要寫入文件的內容。 (幾乎僞下面:-)

    PrintWriter filewriter=null; 
    
    try 
    { 
    // create a new writer 
    filewriter = new PrintWriter(new File(filename)); 
    
    //Iterate over items and print each item 
        for(Item item: itemsList) 
        { 
        // print object 
        filewriter.println(item); 
        } 
    } 
    catch(Exception e) 
    { 
        e.printStackTrace(); 
    } 
    finally 
    { 
    // close writer 
    if(filewriter!=null) filewriter.close(); 
    } 
    
+0

那沒有不爲我工作 –