Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / ReadFromFile.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.utils;
11
12 import java.util.ArrayList;
13 import java.io.*;
14
15 /**
16  * Convenience object for reading from file
17  *
18  *
19  *
20  */
21 public class ReadFromFile {
22     private FileInputStream fileStream;
23     private DataInputStream dataInput;
24     private BufferedReader bufferedReader;
25     private String fileName;
26     private File filePointer;
27
28     public ReadFromFile(String name) throws FileNotFoundException {
29         fileName = name;
30         fileStream = new FileInputStream(this.fileName);
31         filePointer = new File(fileName); //needed to allow file deletion
32     }
33
34     public ArrayList<String> readFile() throws IOException {
35         dataInput = new DataInputStream(this.fileStream);
36         bufferedReader = new BufferedReader(new InputStreamReader(dataInput));
37
38         ArrayList<String> lineList = new ArrayList<String>();
39         String line;
40         while ((line = bufferedReader.readLine()) != null) {
41             lineList.add(line);
42         }
43         bufferedReader.close();
44         dataInput.close();
45         fileStream.close();
46         return lineList;
47     }
48
49     public boolean delete() {
50         if (filePointer == null) {
51             return true;
52         }
53         return filePointer.delete();
54     }
55 }