PageRenderTime 69ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mpv5/utils/files/FileReaderWriter.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 482 lines | 321 code | 43 blank | 118 comment | 30 complexity | cdc34608ec8d1a07fb68b947044eee6f MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.files;
  18. /**
  19. *
  20. *
  21. */
  22. import java.io.File;
  23. import java.io.BufferedReader;
  24. import java.io.BufferedWriter;
  25. import java.io.FileInputStream;
  26. import java.io.FileOutputStream;
  27. import java.io.FileReader;
  28. import java.io.FileWriter;
  29. import java.io.IOException;
  30. import java.io.InputStreamReader;
  31. import java.io.OutputStreamWriter;
  32. import java.io.Reader;
  33. import java.nio.charset.Charset;
  34. import java.util.ArrayList;
  35. import java.util.logging.Level;
  36. import java.util.logging.Logger;
  37. import mpv5.logging.Log;
  38. import mpv5.utils.arrays.ArrayUtilities;
  39. public class FileReaderWriter {
  40. private BufferedReader datenstrom;
  41. private char[] charArray;
  42. private File filer;
  43. private FileWriter fw;
  44. private BufferedWriter bw;
  45. private String zeilenumbruch = "\r\n";
  46. private Charset charset = Charset.forName("UTF-8");
  47. /**
  48. * A reader/writer helper class for textfiles
  49. * @param file
  50. */
  51. public FileReaderWriter(File file) {
  52. this.filer = file;
  53. }
  54. /**
  55. * A reader/writer helper class for textfiles
  56. * @param file
  57. */
  58. public FileReaderWriter(String file) {
  59. filer = new File(file);
  60. }
  61. public FileReaderWriter(File file, String charset) {
  62. filer = file;
  63. this.charset = Charset.forName(charset);
  64. }
  65. /**
  66. * Reads a specific line number
  67. * @param skipsign If a line starts with this, skip it.
  68. * Skip none if null.
  69. * @return The specified line
  70. */
  71. public String read1Line(String skipsign) {
  72. BufferedReader in = null;
  73. String s = null;
  74. boolean reading = true;
  75. try {
  76. in = new BufferedReader(new InputStreamReader(new FileInputStream(filer)));
  77. if (skipsign != null) {
  78. s = skipsign;
  79. while (reading) {
  80. if (s.startsWith(skipsign)) {
  81. s = in.readLine();
  82. } else {
  83. reading = false;
  84. }
  85. }
  86. } else {
  87. s = in.readLine();
  88. }
  89. } catch (IOException ex) {
  90. mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex);
  91. } finally {
  92. try {
  93. in.close();
  94. } catch (IOException ex) {
  95. mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex);
  96. }
  97. }
  98. return s;
  99. }
  100. /**
  101. * Read a file into an array (line by line)
  102. * @return The array representing the file
  103. */
  104. public String[] readLines() {
  105. BufferedReader in = null;
  106. @SuppressWarnings("unchecked")
  107. ArrayList<String> arr = new ArrayList();
  108. try {
  109. in = new BufferedReader(new InputStreamReader(new FileInputStream(filer)));
  110. String s = in.readLine();
  111. while (s != null) {
  112. arr.add(s);
  113. s = in.readLine();
  114. }
  115. } catch (IOException ex) {
  116. Log.Debug(this, ex.getMessage());
  117. return null;
  118. } finally {
  119. try {
  120. in.close();
  121. } catch (IOException ex) {
  122. mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex);
  123. }
  124. }
  125. return ArrayUtilities.listToStringArray(arr);
  126. }
  127. /**
  128. * Appends the String Array to a file (new line per array field) or creates it if needed
  129. *
  130. * @param text
  131. * @return true is writing successful
  132. */
  133. public boolean write(String[] lines) {
  134. String string = "";
  135. for (int i = 0; i < lines.length; i++) {
  136. string += lines[i] + "\n";
  137. }
  138. if (string.length() > 0) {
  139. if (!write(string)) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. }
  145. /**
  146. * Appends the String to a file (new line) or creates it if needed
  147. *
  148. * @param text
  149. * @return true is writing successful
  150. */
  151. public boolean write(String text) {
  152. FileWriter out;
  153. try {
  154. text = zeilenumbruch + text;
  155. out = new FileWriter(filer, true);
  156. BufferedWriter writer = new BufferedWriter(out);
  157. writer.write(text);
  158. writer.close();
  159. } catch (IOException ex) {
  160. System.out.println(ex.getMessage());
  161. return false;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Appends the String to a file (new line) or creates it if needed
  167. *
  168. * @param text
  169. * @return true is writing successful
  170. */
  171. public boolean writeWCharset(String text) {
  172. try {
  173. text = zeilenumbruch + text;
  174. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filer, true), charset));
  175. writer.write(text);
  176. writer.close();
  177. } catch (IOException ex) {
  178. System.out.println(ex.getMessage());
  179. return false;
  180. }
  181. return true;
  182. }
  183. /**
  184. * Appends the String Array to a file (new line per array field) or creates it if needed.
  185. * <li/>Starts from the beginning of the file.
  186. * <li/>Skips empty lines.
  187. * @param lines
  188. * @return
  189. */
  190. public boolean write0(String[] lines) {
  191. String string = "";
  192. for (int i = 0; i < lines.length; i++) {
  193. string += lines[i] + "\n";
  194. }
  195. if (string.length() > 0) {
  196. if (!write0(string)) {
  197. return false;
  198. }
  199. }
  200. return true;
  201. }
  202. /**
  203. * Appends the String Array to a file (new line per array field) or creates it if needed.
  204. * <li/>Starts from the beginning of the file.
  205. * <li/>Skips empty lines.
  206. * @param lines
  207. * @return
  208. */
  209. public boolean write0WCharset(String[] lines) {
  210. String string = "";
  211. for (int i = 0; i < lines.length; i++) {
  212. string += lines[i] + "\n";
  213. }
  214. if (string.length() > 0) {
  215. if (!write0WCharset(string)) {
  216. return false;
  217. }
  218. }
  219. return true;
  220. }
  221. /**
  222. * Appends the String to a file or creates it if needed. Writes from the beginning of the file.
  223. * @param text
  224. * @return
  225. */
  226. public boolean write0WCharset(String text) {
  227. FileWriter out;
  228. try {
  229. String[] txt = this.readLines();
  230. } catch (Exception e) {
  231. try {
  232. filer.createNewFile();
  233. } catch (IOException ex) {
  234. Log.Debug(e);
  235. }
  236. }
  237. try {
  238. out = new FileWriter(filer, true);
  239. BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filer, true), charset));
  240. writer.write(text);
  241. writer.newLine();
  242. writer.close();
  243. } catch (IOException ex) {
  244. System.out.println(ex.getMessage());
  245. return false;
  246. }
  247. return true;
  248. }
  249. /**
  250. * Appends the String to a file or creates it if needed. Writes from the beginning of the file.
  251. * @param text
  252. * @return
  253. */
  254. public boolean write0(String text) {
  255. FileWriter out;
  256. try {
  257. String[] txt = this.readLines();
  258. } catch (Exception e) {
  259. try {
  260. filer.createNewFile();
  261. } catch (IOException ex) {
  262. Log.Debug(e);
  263. }
  264. }
  265. try {
  266. out = new FileWriter(filer, true);
  267. BufferedWriter writer = new BufferedWriter(out);
  268. writer.write(text);
  269. writer.newLine();
  270. writer.close();
  271. } catch (IOException ex) {
  272. System.out.println(ex.getMessage());
  273. return false;
  274. }
  275. return true;
  276. }
  277. /**
  278. * Creates a file and writes the String into it - \
  279. * deletes existing files!
  280. *
  281. * @param text
  282. * @return true is writing successful
  283. */
  284. public boolean writeOnce(String text) {
  285. try {
  286. if (filer.exists()) {
  287. filer.delete();
  288. }
  289. fw = new FileWriter(filer);
  290. bw = new BufferedWriter(fw);
  291. bw.write(text);
  292. bw.close();
  293. } catch (IOException ex) {
  294. System.out.println(ex.getMessage());
  295. return false;
  296. }
  297. return true;
  298. }
  299. /**
  300. * Reads a whole text file into a String
  301. * @return a string with text of file
  302. */
  303. public String read() {
  304. try {
  305. int charArrayLaenge;
  306. //Wie heist die Datei?
  307. File dateiHandle = filer;
  308. //Wie lange ist diese Datei?
  309. long datenStromLaenge = dateiHandle.length() + 1;
  310. charArrayLaenge = (int) datenStromLaenge - 1;
  311. charArray = new char[charArrayLaenge];
  312. try {
  313. FileReader dateiInhalt = new FileReader(dateiHandle);
  314. datenstrom = new BufferedReader(dateiInhalt);
  315. datenstrom.read(charArray, 0, charArrayLaenge);
  316. } catch (IOException e) {
  317. Log.Debug(e);
  318. }
  319. datenstrom.close();
  320. } catch (Exception ex) {
  321. Log.Debug(ex);
  322. return "\n" + filer.getPath() + " not found :-( \n";
  323. }
  324. return new String(charArray);
  325. }
  326. /**
  327. * Reads a whole text file into a String
  328. * @return a string with text of file
  329. */
  330. public String readWCharset() {
  331. try {
  332. int charArrayLaenge;
  333. //Wie heist die Datei?
  334. File dateiHandle = filer;
  335. //Wie lange ist diese Datei?
  336. long datenStromLaenge = dateiHandle.length() + 1;
  337. charArrayLaenge = (int) datenStromLaenge - 1;
  338. charArray = new char[charArrayLaenge];
  339. try {
  340. Reader dateiInhalt = new InputStreamReader(new FileInputStream(dateiHandle), charset);
  341. datenstrom = new BufferedReader(dateiInhalt);
  342. datenstrom.read(charArray, 0, charArrayLaenge);
  343. } catch (IOException e) {
  344. Log.Debug(e);
  345. }
  346. datenstrom.close();
  347. } catch (Exception ex) {
  348. Log.Debug(ex);
  349. return "\n" + filer.getPath() + " not found :-( \n";
  350. }
  351. return new String(charArray);
  352. }
  353. /**
  354. * Try to delete the file (may fail, though)
  355. */
  356. public void deleteFile() {
  357. filer.delete();
  358. }
  359. /**
  360. * Write one line representing the given array
  361. * @param text
  362. * @param sep The separator between the array items
  363. * @return
  364. */
  365. public boolean writeLine(String[] text, String sep) {
  366. String string = "";
  367. for (int i = 0; i < text.length; i++) {
  368. string += text[i] + sep;
  369. }
  370. if (!writeOnce(string.substring(0, string.length() - sep.length()))) {
  371. return false;
  372. }
  373. return true;
  374. }
  375. /**
  376. * Write a file representing the given array
  377. * @param text
  378. * @param sep The separator between the array columns
  379. * @param quoted "?
  380. * @return
  381. */
  382. public boolean write(Object[][] text, String sep, boolean quoted) {
  383. boolean result = true;
  384. for (int i = 0; i < text.length; i++) {
  385. String string = "";
  386. for (int j = 0; j < text[i].length; j++) {
  387. Object[] t = text[i];
  388. if (quoted) {
  389. string += "\"" + String.valueOf(t[j]).replaceAll("\\<.*?\\>", "") + "\"" + sep;
  390. } else {
  391. string += String.valueOf(t[j]).replaceAll("\\<.*?\\>", "") + sep;
  392. }
  393. }
  394. result = write(string);
  395. string = null;
  396. }
  397. return result;
  398. }
  399. /**
  400. * Flush the file (make it an empty file)
  401. */
  402. public void flush() {
  403. writeOnce("");
  404. }
  405. public boolean writeWCharset(String[] text) {
  406. for (int i = 0; i < text.length; i++) {
  407. String string = text[i];
  408. if (string != null) {
  409. if (!writeWCharset(string)) {
  410. return false;
  411. }
  412. }
  413. }
  414. return true;
  415. }
  416. public String[] readLinesWCharset() {
  417. BufferedReader in = null;
  418. @SuppressWarnings("unchecked")
  419. ArrayList<String> arr = new ArrayList();
  420. try {
  421. in = new BufferedReader(new InputStreamReader(new FileInputStream(filer), charset));
  422. String s = in.readLine();
  423. while (s != null) {
  424. arr.add(s);
  425. s = in.readLine();
  426. }
  427. } catch (IOException ex) {
  428. Log.Debug(this, ex.getMessage());
  429. return null;
  430. } finally {
  431. try {
  432. in.close();
  433. } catch (IOException ex) {
  434. mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex);
  435. }
  436. }
  437. return ArrayUtilities.listToStringArray(arr);
  438. }
  439. }