2012-03-31 14 views
0

以下のようにして、配列のファイルプログラムを読み書きします。 ArrayListのように次のように見えますか?私は構文に問題があります。私はArrayListがそうであることを知っている:private ArrayList prod list = new ArrayList(); 読み込み/書き込みIO構文はどのようになりますか?ありがとうございました。JavaのArrayListsからファイルを読み書きする方法

static ActionProduct[] prodlist = new ActionProduct[50]; 
    static String filename = System.getProperty("user.dir") + "\\src\\product.txt"; 
    static int pIndex=0; 


public static void readFile() throws IOException { 
     // input file must be supplied in the first argument 
     InputStream istream; 
     if (filename.length() > 0) { 
      File inputFile = new File(filename); 
      istream = new FileInputStream(inputFile); 
     } else { 
      // if no filename, use standard input stream 
      istream = System.in; 
     } 

     // use a buffered reader for line-at-a-time 
     // reading 
     BufferedReader lineReader; 
     lineReader = new BufferedReader(new InputStreamReader(istream)); 

     // read one line at a time 
     String line; 
     while ((line = lineReader.readLine()) != null) { 

      StringTokenizer tokens = new StringTokenizer(line, "\t"); 

      // String tmp = tokens.nextToken(); 
      // System.out.println("token " + tmp); 
      prodlist[pIndex] = new ActionProduct(); 
      String category = prodlist[pIndex].getCategory(); 
      category = tokens.nextToken(); 
      System.out.println("got category " +category); 

      int item = prodlist[pIndex].getItem(); 
      item = Integer.parseInt(tokens.nextToken()); 

      String name = prodlist[pIndex].getName(); 
      System.out.println("got name " +name); 

      double price = prodlist[pIndex].getPrice(); 
      price = Double.parseDouble(tokens.nextToken()); 

      int units = prodlist[pIndex].getUnits(); 
      units = Integer.parseInt(tokens.nextToken()); 
      pIndex++; 
     } 
    } 

答えて

2

この置換:

 prodlist[pIndex] = new ActionProduct(); 

:次いで

static List<ActionProduct> prodlist = new ArrayList<ActionProduct>(); 

これに

static ActionProduct[] prodlist = new ActionProduct[50]; 

最後に、prodlist[pIndex]のすべての用途をpに置き換えて、それ以外の場合はpIndexを取り除きます。

+0

ありがとうKeith – ShaunK

関連する問題