2717c942287ff42932159d95c2b0957a9c4dfb67
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / WriteToFile.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.io.*;
13 import java.util.ArrayList;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * Convenience object to write a file
20  *
21  *
22  *
23  */
24 public class WriteToFile {
25     protected static final Logger logger = LoggerFactory
26     .getLogger(WriteToFile.class);
27     private FileWriter fstream;
28     private BufferedWriter bufferOut;
29
30     public WriteToFile(String fileName) throws IOException {
31         fstream = new FileWriter(fileName);
32         bufferOut = new BufferedWriter(fstream);
33     }
34
35     public void save(ArrayList<String> entryList) throws IOException {
36         for (String entry : entryList) {
37             bufferOut.write(entry);
38             bufferOut.append('\n');
39         }
40         try {
41             this.bufferOut.flush();
42         } catch (IOException e) {
43             logger.error("",e);
44         }
45     }
46
47     public boolean close() {
48         try {
49             bufferOut.close();
50             fstream.close();
51         } catch (IOException e) {
52             return false;
53         }
54         return true;
55     }
56 }