KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > FileToDataLoader


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.utils;
13
14 import org.openbravo.data.*;
15 import org.openbravo.database.StandAloneConnection;
16 import java.io.*;
17 import org.apache.log4j.Logger;
18 import java.util.Vector JavaDoc;
19
20 public class FileToDataLoader extends StandAloneConnection{
21
22   static Logger log4j = Logger.getLogger(FileToDataLoader.class);
23
24   public static FieldProvider[] getFileIntoData(File path, String JavaDoc FileName, SetFieldProvider data) {
25     if (log4j.isDebugEnabled()) log4j.debug("processing replace file: " + FileName);
26     Vector JavaDoc<FieldProvider> vector = new Vector JavaDoc<FieldProvider>();
27     FieldProvider[] newData = null;
28     try {
29       File file = new File(path, FileName);
30       if (!file.exists()) {
31         log4j.error("Unknown file: " + path + "\\" + FileName);
32         return null;
33       }
34       BufferedReader fileBuffer = new BufferedReader(new FileReader(file));
35
36       String JavaDoc nextLine = fileBuffer.readLine();
37       while (nextLine != null) {
38         FieldProvider fieldProvider = data.setFieldProvider(nextLine);
39         if (fieldProvider != null) vector.addElement(fieldProvider);
40         nextLine = fileBuffer.readLine();
41       }
42       fileBuffer.close();
43       newData = new FieldProvider[vector.size()];
44       vector.copyInto(newData);
45     } catch (Exception JavaDoc e) {
46       e.printStackTrace();
47       return null;
48     }
49     return newData;
50   }
51 }
52
Popular Tags