Open Kilda Java Documentation
FileUtil.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.wfm.topology.utils;
17 
18 import com.google.common.base.Charsets;
19 import com.google.common.io.Files;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.Serializable;
24 
28 public class FileUtil implements Serializable {
29 
30  public File dir = Files.createTempDir();
31  public String fileName = "temp.txt";
32  private File file;
33 
34  public FileUtil withDir(File dir) {
35  this.dir = dir;
36  return this;
37  }
38 
39  public FileUtil withFileName(String fileName) {
40  this.fileName = fileName;
41  return this;
42  }
43 
44  public File getFile() {
45  if (file == null) {
46  dir.mkdirs();
47  file = new File(dir.getAbsolutePath(), fileName);
48  }
49  return file;
50  }
51 
57  public boolean append(String text) {
58  boolean success = true;
59 
60  try {
61  Files.append(text, getFile(), Charsets.UTF_8);
62  } catch (IOException e) {
63  success = false;
64  e.printStackTrace();
65  }
66 
67  return success;
68  }
69 
73  public int numLines() {
74  int result = -1;
75 
76  try {
77  result = Files.readLines(getFile(), Charsets.UTF_8).size();
78  } catch (IOException e) {
79  e.printStackTrace();
80  }
81 
82  return result;
83  }
84 }
FileUtil withFileName(String fileName)
Definition: FileUtil.java:39
list result
Definition: plan-d.py:72