OpenKeyWord  Build_ID: 457, Datum: 01.02.2020 07:45:48
Dont repeat yourself. - Do it once and only once!
OKW_FileHelper.java
1 /*
2  ==============================================================================
3  Author: Zoltán Hrabovszki <zh@openkeyword.de>
4 
5  Copyright © 2012 - 2019 IT-Beratung Hrabovszki
6  www.OpenKeyWord.de
7  ==============================================================================
8 
9  This file is part of OpenKeyWord.
10 
11  OpenKeyWord is free software: you can redistribute it and/or modify
12  it under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenKeyWord is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  GNU General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenKeyWord. If not, see <http://www.gnu.org/licenses/>.
23 
24  Diese Datei ist Teil von OpenKeyWord.
25 
26  OpenKeyWord ist Freie Software: Sie können es unter den Bedingungen
27  der GNU General Public License, wie von der Free Software Foundation,
28  Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
29  veröffentlichten Version, weiterverbreiten und/oder modifizieren.
30 
31  OpenKeyWord wird in der Hoffnung, dass es nützlich sein wird, aber
32  OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
33  Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
34  Siehe die GNU General Public License für weitere Details.
35 
36  Sie sollten eine Kopie der GNU General Public License zusammen mit
37  OpenKeyWord erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
38 */
39 package okw;
40 
41 import java.io.File;
42 import java.io.FileNotFoundException;
43 import java.io.IOException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import java.nio.file.StandardCopyOption;
48 import java.util.ArrayList;
49 
50 import okw.exceptions.OKWDirectoryDoesNotExistsException;
51 import okw.exceptions.OKWDirectorySeperatorNotImplementedException;
52 import okw.exceptions.OKWFileDoesNotExistsException;
53 import okw.log.Logger_Sngltn;
54 
63 public class OKW_FileHelper
64 {
75 
79  // private static LogMessenger LM = new LogMessenger("OKW_FileHelper");
80 
110  public static Boolean createEmptyDirectory( String fpsPath )
111  {
112 
113  String lvsPath = "";
114  Boolean lvbReturn = false;
115 
116  try
117  {
118  // lvsPath = MyParser.ParseMe(fpsPath);
119  Log.LogFunctionStart( "DirectoryCreateEmpty", "String", fpsPath );
120 
121  File myDirectory = new File( fpsPath );
122 
123  // Determine whether the directory exists.
124  if ( myDirectory.exists() & myDirectory.isDirectory() )
125  {
126  deleteDirectory( lvsPath );
127  }
128  else if ( myDirectory.exists() & myDirectory.isFile() )
129  {
130  deleteFile( lvsPath );
131  }
132 
133  // Try to create the directory.
134  myDirectory.mkdirs();
135 
136  lvbReturn = true;
137 
138  }
139  catch (Exception e)
140  {
141  Log.LogPrint( "The process failed:" );
142  }
143  finally
144  {
145  Log.LogFunctionEnd( lvbReturn );
146  }
147  return lvbReturn;
148  }
149 
168  public static void deleteDirectory( String Path )
169  {
170  String lvsPath = Path;
171  Boolean lvbReturn = false;
172 
173  Log.LogFunctionStartDebug( "OKW_Helper.DirectoryDelete", "Path", Path );
174 
175  try
176  {
177 
178  // \todo TODO: ANTLR-Paser einschalten
179  // lvsPaFiNa = MyParser.ParseMe(lvsPaFiNa);
180 
181  File myPath = new File( lvsPath );
182 
183  if ( myPath.exists() )
184  {
185  for ( File f : myPath.listFiles() )
186  {
187  if ( f.isDirectory() )
188  {
189  deleteDirectory( f.getPath() );
190  f.delete();
191  }
192  else
193  {
194  f.delete();
195  }
196  }
197  myPath.delete();
198  }
199  }
200  finally
201  {
202  Log.LogFunctionEndDebug( lvbReturn );
203  }
204  }
205 
222  public static Boolean directoryExists( String fpsPaFiNa )
223  {
224  Boolean lvbReturn = false;
225 
226  Log.LogFunctionStartDebug( "OKW_FileHelper.DirectoryExists", "fpsPaFiNa", fpsPaFiNa );
227 
228  try
229  {
230  // \todo TODO: ANTLR-Paser einschalten
231  // lvsPaFiNa = MyParser.ParseMe(lvsPaFiNa);
232 
233  File myFile = new File( fpsPaFiNa );
234 
235  // Existiert "fpsPaFiNa"?
236  if ( myFile.exists() )
237  {
238  // Ist ein Verzeichniss?
239  if ( myFile.isDirectory() )
240  {
241  lvbReturn = true;
242  }
243  // Ist es eine Datei?
244  else if ( myFile.isFile() )
245  {
246  throw ( new OKWFileDoesNotExistsException( "This is not a directory! Given path is a directory!" ) );
247  }
248  // Dann Existiert "fpsPaFiNa" nicht!
249  else
250  {
251  lvbReturn = false;
252  }
253  }
254  }
255  finally
256  {
257  Log.LogFunctionEndDebug( lvbReturn );
258  }
259 
260  return lvbReturn;
261  }
262 
263  public static void copyDirectory( String fpsSourceFolder, String fpsDestinationFolder )
264  {
265  // Source directory which you want to copy to new location
266  File sourceFolder = new File( fpsSourceFolder );
267 
268  // Target directory where files should be copied
269  File destinationFolder = new File( fpsDestinationFolder );
270 
271  copyDirectory( sourceFolder, destinationFolder );
272 
273  }
274 
275  private static boolean copyDirectory( File fpSourceFolder, File fpDestinationFolder )
276  {
277 
278  Boolean lvbReturn = false;
279 
280  try
281  {
282  // \todo TODO: ANTLR-Paser einschalten
283  // lvsPaNaSource = MyParser.ParseMe(fpsPaNaSource);
284  // lvsPaNaDestination = MyParser.ParseMe(fpsPaNaDestination);
285 
286  Log.LogFunctionStartDebug( "OKW_FileHelper.DirectoryMove", "fpSourceFolder", fpSourceFolder.getPath(), "fpDestinationFolder",
287  fpDestinationFolder.getPath() );
288 
289  // Check if sourceFolder is a directory or file
290  // If sourceFolder is file; then copy the file directly to new
291  // location
292  if ( fpSourceFolder.isDirectory() )
293  {
294  // Verify if destinationFolder is already present; If not then
295  // create it
296  if ( !fpDestinationFolder.exists() )
297  {
298  fpDestinationFolder.mkdir();
299  System.out.println( "Directory created :: " + fpDestinationFolder );
300  }
301 
302  // Get all files from source directory
303  String files[] = fpSourceFolder.list();
304 
305  // Iterate over all files and copy them to destinationFolder one
306  // by one
307  for ( String file : files )
308  {
309  File srcFile = new File( fpSourceFolder, file );
310  File destFile = new File( fpDestinationFolder, file );
311 
312  // Recursive function call
313  copyDirectory( srcFile, destFile );
314  }
315  }
316  else
317  {
318  // Copy the file content from one place to another
319  Files.copy( fpSourceFolder.toPath(), fpDestinationFolder.toPath(), StandardCopyOption.REPLACE_EXISTING );
320  System.out.println( "File copied :: " + fpDestinationFolder );
321  }
322  }
323  catch (IOException e)
324  {
325  Log.LogPrintDebug( e.getMessage() );
326  }
327  finally
328  {
329  Log.LogFunctionEndDebug( lvbReturn );
330  }
331 
332  return lvbReturn;
333  }
334 
347  public static boolean moveDirectory( String fpsPaNaSource, String fpsPaNaDestination ) throws IOException, FileNotFoundException
348  {
349  Boolean lvbReturn = false;
350 
351  String lvsPaNaSource = fpsPaNaSource;
352  String lvsPaNaDestination = fpsPaNaDestination;
353 
354  try
355  {
356  // \todo TODO: ANTLR-Paser einschalten
357  // lvsPaNaSource = MyParser.ParseMe(fpsPaNaSource);
358  // lvsPaNaDestination = MyParser.ParseMe(fpsPaNaDestination);
359 
360  Log.LogFunctionStartDebug( "OKW_FileHelper.DirectoryMove", "fpsPaNaSource", fpsPaNaSource, "fpsPaNaDestination", fpsPaNaDestination );
361 
362  if ( directoryExists( lvsPaNaSource ) )
363  {
364  // Löschen des ZIEL-verzeichnissen wenn vorhanden
365  deleteDirectory( lvsPaNaDestination );
366 
367  // Copy with subfolders
368  copy( lvsPaNaSource, lvsPaNaDestination, true );
369 
370  // Delete Source
371  deleteDirectory( lvsPaNaSource );
372 
373  lvbReturn = true;
374  }
375  }
376  finally
377  {
378  Log.LogFunctionEndDebug( lvbReturn );
379  }
380 
381  return lvbReturn;
382  }
383 
384  public static void copy( String fpsSource, String fpsDestination, Boolean copySubDirs ) throws IOException, FileNotFoundException
385  {
386  Path lvSourcePath = Paths.get( fpsSource );
387  Path lvDestinationPath = Paths.get( fpsDestination );
388 
389  if ( Files.notExists( lvSourcePath ) )
390  {
391  throw new FileNotFoundException( "Source directory does not exist or could not be found: " + fpsSource );
392  }
393 
394  else
395  {
396 
397  Files.copy( lvSourcePath, lvDestinationPath );
398  }
399  }
400 
401 
406  public static ArrayList<File> ListFiles( String fpsPath )
407  {
408  ArrayList<File> lvReturn = new ArrayList<File>();
409 
410  File root = new File( fpsPath );
411  File[] list = root.listFiles();
412 
413  if ( list != null )
414  {
415  ;
416 
417  for ( File f : list )
418  {
419  if ( f.isDirectory() )
420  {
421  System.out.println( " Dir:" + f.getAbsolutePath() );
422  ListFiles( f.getAbsolutePath() );
423  }
424  else
425  {
426  String myPathy = f.getAbsolutePath();
427  Log.LogPrint( "File:" + myPathy );
428  lvReturn.add( f );
429  }
430  }
431  }
432 
433  return lvReturn;
434  }
435 
436 
461  public static void deleteFiles( String fpsPaFiNa )
462  {
463 
464  if ( directoryExists( fpsPaFiNa ) )
465  {
466  File myDir = new File( fpsPaFiNa );
467  File[] listOfFiles = myDir.listFiles();
468 
469  for ( File myFileToDelete : listOfFiles )
470  {
471  if ( !myFileToDelete.isDirectory() )
472  {
473  myFileToDelete.delete();
474  }
475  }
476  }
477  }
478 
496  public static Boolean deleteFile( String fpsPaFiNa )
497  {
498  Boolean lvbReturn = false;
499 
500  Log.LogFunctionStartDebug( "OKW_FileHelper.FileDelete", "fpsPaFiNa", fpsPaFiNa );
501 
502  if ( fileExists( fpsPaFiNa ) )
503  {
504  try
505  {
506  File myFile = new File( fpsPaFiNa );
507 
508  lvbReturn = myFile.delete();
509  }
510  finally
511  {
513  }
514  }
515 
516  return lvbReturn;
517  }
518 
532  public static Boolean fileExists( String fpsPaFiNa )
533  {
534 
535  Boolean lvbReturn = false;
536 
537  Log.LogFunctionStartDebug( "OKW_FileHelper.FileExist", "fpsPaFiNa", fpsPaFiNa );
538 
539  try
540  {
541  File myFile = new File( fpsPaFiNa );
542 
543  // Existiert "fpsPaFiNa"?
544  if ( myFile.exists() )
545  {
546  // Ist es eine Datei?
547  if ( myFile.isFile() )
548  {
549  lvbReturn = true;
550  }
551  // Ist ein Verzeichniss?
552  else if ( myFile.isDirectory() )
553  {
554  throw ( new OKWFileDoesNotExistsException( "This is not a file! Given path is a directory!" ) );
555  }
556  // Dann Existiert "fpsPaFiNa" nicht!
557  else
558  {
559  lvbReturn = false;
560  }
561  }
562  }
563  finally
564  {
565  Log.LogFunctionEndDebug( lvbReturn.toString() );
566  }
567 
568  return lvbReturn;
569  }
570 
589  public static Boolean isFile( String PATH )
590  {
591 
592  Boolean lvbReturn = false;
593 
594  Log.LogFunctionStartDebug( "OKW_FileHelper.isFile", "PATH", PATH );
595 
596  try
597  {
598  File myFile = new File( PATH );
599 
600  // Existiert "fpsPaFiNa"?
601  if ( myFile.exists() )
602  {
603  // Ist es eine Datei?
604  if ( myFile.isFile() )
605  {
606  lvbReturn = true;
607  }
608  else
609  {
610  lvbReturn = false;
611  }
612  }
613  else
614  throw ( new OKWFileDoesNotExistsException( "This is not a file! Given path is a directory!" ) );
615  }
616  finally
617  {
618  Log.LogFunctionEndDebug( lvbReturn.toString() );
619  }
620 
621  return lvbReturn;
622  }
623 
637  public static void move( String fpsPaFiNaSource, String fpsPaFiNaDestination ) throws IOException, FileNotFoundException
638  {
639 
640  Log.LogFunctionStartDebug( "OKW_FileHelper.Move", "fpsPaFiNaSource", fpsPaFiNaSource, "fpsPaFiNaDestination", fpsPaFiNaDestination );
641  try
642  {
643  if ( fileExists( fpsPaFiNaSource ) )
644  {
645 
646  deleteFile( fpsPaFiNaDestination );
647  copy( fpsPaFiNaSource, fpsPaFiNaDestination, true );
648  deleteFile( fpsPaFiNaSource );
649 
650  }
651  else
652  {
653  // \todo TODO: Log in xml-datei auslagern
655  throw new OKWFileDoesNotExistsException( "file doesnot exists..." );
656  }
657  }
658  finally
659  {
661  }
662  }
663 
676  public static boolean createFile( String fpsPaFiNa ) throws IOException
677  {
678 
679  Boolean lvbReturn = false;
680 
681  Path newFilePath = Paths.get( fpsPaFiNa );
682 
683  try
684  {
685 
686  if ( !Files.exists( newFilePath ) )
687  {
688  Files.createFile( newFilePath );
689  }
690  }
691  finally
692  {
693  Log.LogFunctionEnd( lvbReturn );
694  }
695 
696  return lvbReturn;
697 
698  }
699 
727  public static Boolean isDirectoryEmpty( String PATH )
728  {
729 
730  Boolean lvbReturn = false;
731  Log.LogFunctionStart( "OKW_FileHelper.IsDirectoryEmpty", "PATH", PATH );
732  try
733  {
734  if ( directoryExists( PATH ) )
735  {
736  lvbReturn = Paths.get( PATH ).toFile().listFiles().length == 0;
737  }
738  else
739  {
740  // \todo TODO: Meldung in LM_OKW_FileHelper.xml auslagern...
741  throw new OKWDirectoryDoesNotExistsException( "Directory Does not Exists..." );
742  }
743  }
744  finally
745  {
746  Log.LogFunctionEnd( lvbReturn );
747  }
748 
749  return false;
750  }
751 
778  public static String convertDirectorySeperator( String fpsPath )
779  {
780  String lvsReturn = fpsPath;
781 
782  Log.LogFunctionStartDebug( "OKW_FileHelper.ConvertDirectorySeperator", "fpsPath", fpsPath );
783 
784  try
785  {
786  String myFileSeparator = System.getProperty( "file.separator" );
787 
788  if ( "/".equals( myFileSeparator ) )
789  {
790  lvsReturn = fpsPath.replace( "\\", "/" );
791  }
792  else if ( "\\".equals( myFileSeparator ) )
793  {
794  lvsReturn = fpsPath.replace( "/", "\\" );
795  }
796  else
797  throw ( new OKWDirectorySeperatorNotImplementedException( "Error: Unknown file.separator: -" + myFileSeparator + "-" ) );
798 
799  lvsReturn = lvsReturn.replace( "\\C:", "C:" );
800  }
801  finally
802  {
803  Log.LogFunctionEndDebug( lvsReturn );
804  }
805 
806  return lvsReturn;
807  }
808 
829  public static void DirectoryCreate( String PATH ) throws IOException
830  {
831  File myFile = new File( PATH );
832 
833  Log.LogFunctionStartDebug( "OKW_FileHelper.CreateDirectory", "PATH", PATH );
834 
835  try
836  {
837  org.apache.commons.io.FileUtils.forceMkdir( myFile );
838  }
839  finally
840  {
842  }
843  }
844 }
okw.OKW_FileHelper
OKW_FileHelper enthält FilfsMethoden für die Handhabung von Dateien und Verzechnissen.
Definition: OKW_FileHelper.java:63
okw.OKW_FileHelper.isFile
static Boolean isFile(String PATH)
Prüft, ob der gegebene PATH eine Datei ist.
Definition: OKW_FileHelper.java:589
okw.log.Logger_Sngltn.LogFunctionEnd
void LogFunctionEnd(String fps_Return)
LogFunctionEnd(String):
Definition: Logger_Sngltn.java:196
okw.OKW_FileHelper.fileExists
static Boolean fileExists(String fpsPaFiNa)
Prüft, ob die gegebene fpsPaFiNa Datei existiert.
Definition: OKW_FileHelper.java:532
okw.log.Logger_Sngltn.LogFunctionEndDebug
void LogFunctionEndDebug()
LogFunctionEndDebug:
Definition: Logger_Sngltn.java:249
okw.log.Logger_Sngltn.getInstance
static Logger_Sngltn getInstance()
Zentrale Logger-Klasse stellt Logger-Methoden innerhalb von OKW zur Verfügung.
Definition: Logger_Sngltn.java:88
okw.OKW_FileHelper.isDirectoryEmpty
static Boolean isDirectoryEmpty(String PATH)
Diese Methode prüft, ob das gegebene Verzeichniss existiert und leer ist.
Definition: OKW_FileHelper.java:727
okw.OKW_FileHelper.DirectoryCreate
static void DirectoryCreate(String PATH)
Erstellt ein Verzeichnis, einschließlich aller notwendigen, aber nicht vorhandenen übergeordneten Ver...
Definition: OKW_FileHelper.java:829
okw.exceptions.OKWDirectorySeperatorNotImplementedException
OKWDirectorySeperatorNotImplementedException-Ausnahme wird ausgelöst, wenn das Betriebssystem einen u...
Definition: OKWDirectorySeperatorNotImplementedException.java:54
okw.log.Logger_Sngltn.LogFunctionStart
void LogFunctionStart(String fps_FunctionName, String... fpsParameter)
LogFunctionStart:
Definition: Logger_Sngltn.java:303
okw.OKW_FileHelper.createEmptyDirectory
static Boolean createEmptyDirectory(String fpsPath)
Definition: OKW_FileHelper.java:110
okw.log.Logger_Sngltn.LogFunctionStartDebug
void LogFunctionStartDebug(String fps_FunctionName, String... fpsParameter)
LogFunctionStartDebug:
Definition: Logger_Sngltn.java:311
okw.OKW_FileHelper.convertDirectorySeperator
static String convertDirectorySeperator(String fpsPath)
Konvertiert für das Host-Betriebsystem den Path Separator.
Definition: OKW_FileHelper.java:778
okw.OKW_FileHelper.deleteFile
static Boolean deleteFile(String fpsPaFiNa)
Löscht die gegebene Datei fpsPaFiNa.
Definition: OKW_FileHelper.java:496
okw.OKW_FileHelper.deleteDirectory
static void deleteDirectory(String Path)
Löschent rekursiv alle Dateien und Unterverzeichnisse und das gegebenen Verzeichniss selbst.
Definition: OKW_FileHelper.java:168
okw.OKW_FileHelper.createFile
static boolean createFile(String fpsPaFiNa)
Legt eine Leere Datei an.
Definition: OKW_FileHelper.java:676
okw.OKW_FileHelper.Log
static Logger_Sngltn Log
Instanz der Zentralen Loggerklasse.
Definition: OKW_FileHelper.java:74
okw.log.Logger_Sngltn.LogPrint
void LogPrint(String fps_Message)
LogPrint Function: Prints the values of expressions to the results file.
Definition: Logger_Sngltn.java:487
okw.exceptions.OKWFileDoesNotExistsException
OKWFileDoesNotExistsException-Ausnahme wird ausgelöst, wenn eine Datei nicht gefunden wird.
Definition: OKWFileDoesNotExistsException.java:55
okw.OKW_FileHelper.ListFiles
static ArrayList< File > ListFiles(String fpsPath)
Definition: OKW_FileHelper.java:406
okw.log.Logger_Sngltn.LogPrintDebug
void LogPrintDebug(String fpsMessage)
Loggt eine Nachricht.
Definition: Logger_Sngltn.java:520
okw.OKW_FileHelper.move
static void move(String fpsPaFiNaSource, String fpsPaFiNaDestination)
Verschiebt die gegeben Quell-Datei zu einer neuen Ziel-Datei.
Definition: OKW_FileHelper.java:637
okw.OKW_FileHelper.directoryExists
static Boolean directoryExists(String fpsPaFiNa)
Prüft, ob die gegebene fpsPaFiNa Datei existiert.
Definition: OKW_FileHelper.java:222
okw.OKW_FileHelper.deleteFiles
static void deleteFiles(String fpsPaFiNa)
Löscht alle Dateien des gegebenen Musters im gegebenen Verzeichniss Rekursive.
Definition: OKW_FileHelper.java:461
okw.OKW_FileHelper.moveDirectory
static boolean moveDirectory(String fpsPaNaSource, String fpsPaNaDestination)
Verzeichniss verschieben löschen des gegebenen Verzeichnisses.
Definition: OKW_FileHelper.java:347
okw.log.Logger_Sngltn
Definition: Logger_Sngltn.java:54
okw.exceptions.OKWDirectoryDoesNotExistsException
OKWDirectoryDoesNotExistsException-Ausnahme wird ausgelöst, wenn ein Verzeichniss nicht gefunden word...
Definition: OKWDirectoryDoesNotExistsException.java:56