admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package tai_ofa; |
| 6 | |
| 7 | import java.io.BufferedReader; |
| 8 | import java.io.BufferedWriter; |
| 9 | import java.io.File; |
| 10 | import java.io.FileNotFoundException; |
| 11 | import java.io.FileReader; |
| 12 | import java.io.FileWriter; |
| 13 | import java.io.IOException; |
| 14 | import java.io.Writer; |
| 15 | import java.util.ArrayList; |
| 16 | |
| 17 | /** |
| 18 | * |
| 19 | * @author Raghav Kashyap (raghavkashyap@paxterrasolutions.com) |
| 20 | |
| 21 | * TestON is free software: you can redistribute it and/or modify |
| 22 | * it under the terms of the GNU General Public License as published by |
| 23 | * the Free Software Foundation, either version 2 of the License, or |
| 24 | * (at your option) any later version. |
| 25 | |
| 26 | * TestON is distributed in the hope that it will be useful, |
| 27 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 | * GNU General Public License for more details. |
| 30 | |
| 31 | * You should have received a copy of the GNU General Public License |
| 32 | * along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 33 | |
| 34 | */ |
| 35 | public class OFAFileOperations { |
| 36 | |
| 37 | BufferedReader input; |
| 38 | ArrayList list = new ArrayList(); |
| 39 | ArrayList<String> filePath = new ArrayList<String>(); |
| 40 | |
| 41 | public OFAFileOperations() { |
| 42 | } |
| 43 | |
| 44 | public void writeInFile(String path, String demoFile) { |
| 45 | try { |
| 46 | FileWriter fstream = new FileWriter(path); |
| 47 | BufferedWriter out = new BufferedWriter(fstream); |
| 48 | out.write(demoFile); |
| 49 | out.close(); |
| 50 | } catch (Exception e) { |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public String getContents(File aFile) { |
| 55 | StringBuilder contents = new StringBuilder(); |
| 56 | |
| 57 | try { |
| 58 | //use buffering, reading one line at a time |
| 59 | //FileReader always assumes default encoding is OK! |
| 60 | try { |
| 61 | input = new BufferedReader(new FileReader(aFile)); |
| 62 | } catch (Exception e) { |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | String line = null; //not declared within while loop |
| 67 | |
| 68 | while ((line = input.readLine()) != null) { |
| 69 | contents.append(line); |
| 70 | contents.append(System.getProperty("line.separator")); |
| 71 | } |
| 72 | } finally { |
| 73 | try { |
| 74 | input.close(); |
| 75 | } catch (Exception e) { |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | } catch (IOException ex) { |
| 80 | ex.printStackTrace(); |
| 81 | } |
| 82 | |
| 83 | return contents.toString(); |
| 84 | } |
| 85 | |
| 86 | public String getExtension(String name) { |
| 87 | String extension = null; |
| 88 | try { |
| 89 | |
| 90 | if (name.contains(".")) { |
| 91 | int dotPos = name.lastIndexOf("."); |
| 92 | extension = name.substring(dotPos); |
| 93 | } |
| 94 | } catch (Exception e) { |
| 95 | } |
| 96 | |
| 97 | return extension; |
| 98 | } |
| 99 | |
| 100 | public String getFileName(String name) { |
| 101 | String fileName = null; |
| 102 | try { |
| 103 | |
| 104 | if (name.contains(".")) { |
| 105 | int dotPos = name.lastIndexOf("."); |
| 106 | fileName = name.substring(0, dotPos); |
| 107 | } |
| 108 | } catch (Exception e) { |
| 109 | } |
| 110 | |
| 111 | return fileName; |
| 112 | } |
| 113 | |
| 114 | public void setContents(File aFile, String aContents) throws FileNotFoundException, IOException { |
| 115 | if (aFile == null) { |
| 116 | throw new IllegalArgumentException("File should not be null."); |
| 117 | } |
| 118 | if (!aFile.exists()) { |
| 119 | throw new FileNotFoundException("File does not exist: " + aFile); |
| 120 | } |
| 121 | if (!aFile.isFile()) { |
| 122 | throw new IllegalArgumentException("Should not be a directory: " + aFile); |
| 123 | } |
| 124 | if (!aFile.canWrite()) { |
| 125 | throw new IllegalArgumentException("File cannot be written: " + aFile); |
| 126 | } |
| 127 | |
| 128 | //use buffering |
| 129 | Writer output = new BufferedWriter(new FileWriter(aFile)); |
| 130 | try { |
| 131 | //FileWriter always assumes default encoding is OK! |
| 132 | output.write(aContents); |
| 133 | } finally { |
| 134 | output.close(); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | public void saveFile(File saveToDisk, String content) throws FileNotFoundException, IOException { |
| 139 | setContents(saveToDisk, content); |
| 140 | } |
| 141 | static int spc_count = -1; |
| 142 | |
| 143 | void Process(File aFile) { |
| 144 | |
| 145 | spc_count++; |
| 146 | String spcs = ""; |
| 147 | for (int i = 0; i < spc_count; i++) { |
| 148 | spcs += " "; |
| 149 | } |
| 150 | if (aFile.isFile()) { |
| 151 | list.add(aFile.getName()); |
| 152 | filePath.add(aFile.getPath()); |
| 153 | |
| 154 | } else if (aFile.isDirectory()) { |
| 155 | File[] listOfFiles = aFile.listFiles(); |
| 156 | |
| 157 | |
| 158 | if (listOfFiles != null) { |
| 159 | for (int i = 0; i < listOfFiles.length; i++) { |
| 160 | Process(listOfFiles[i]); |
| 161 | } |
| 162 | } else { |
| 163 | } |
| 164 | } |
| 165 | spc_count--; |
| 166 | |
| 167 | } |
| 168 | } |