Skip to content

Class Jutilas

hxs edited this page Nov 29, 2019 · 15 revisions

Jutilas

Package jutilas.utils
File Jutilas.java

Jutilas is the class that contains the generic functions to develop an application.
This class implements the singleton pattern, so to get the Jutilas instance you need to use the getInstance() method.

Example: Jutilas.getInstance().

Methods


Varius

void sortIgnoreCase(List<String> list)

This method order a String list while ignoring the difference between upper and lower case.

Example:

List<String> list = new List<String>();
// insert elements on list
Jutilas.getInstance().sortIgnoreCase(list);

void setConf(String filePath, String param, String value, String head)

throws FileException

This method sets a configuration, based on key and value, and saves it to the file.
Use the class java.util.Properties.

Parameters:

  • filePath, path to the file where you want to save the configurations
  • param, key of configuration to be save
  • value, value of configuration
  • head, header to be inserted in the file

Exceptions:

  • FileException, exception thrown if an I/O error is returned while working on the file

Example:

Jutilas.getInstance().setConf("app.conf", "key", "val", "Configuration file");

String getConf(String filePath, String param)

throws FileException

This method get a configuration saved in it to the file.
Use the class java.util.Properties.

Return: Return the value of configuration required.

Parameters:

  • filePath, path to the file where have save the configurations
  • param, key of configuration to be get

Exceptions:

  • FileException, exception thrown if an I/O error is returned while working on the file

Example:

String conf = Jutilas.getInstance().getConf("app.conf", "key");

String getStringPath(String... path)

This method create a path formatted for OS.

Return:
Return the string of the path formatted for OS.

Parameters:

  • path, list of directory

Example:

String path  = Jutilas.getInstance().getStringPath("path", "to", "file", "file.txt");

void restartApp()

throws IOException

This method restart the java application.
Use the class java.util.Properties.

Exceptions:

  • IOException, exception thrown if an I/O error is returned while restart the app

Example:

Jutilas.getInstance().restartApp();

System

boolean openBrowser(String URL)

throws IOException, URISyntaxException

This method opens the default browser of the operating system, which will load the URL passed as an argument.

Return:
Return true if success, false if java.awt.Desktop is not supported.

Parameters:

  • URL, url to be open in browser

Exceptions:

  • URISyntaxException, exception thrown if past url syntax is not correct
  • IOException, exception thrown if the call to the operating system returns an input/output error

Example:

Jutilas.getInstance().openBrowser("https://github.com/d3v4s");

boolean openFileFromOS(String... path)

throws IOException

This method opens the default application of the operating system, for open the file passed.

Return:
Return true if success, false if java.awt.Desktop is not supported.

Parameters:

  • path, path to file to be opened

Exceptions:

  • IOException, exception thrown if the call to the operating system returns an input/output error

Example:

Jutilas.getInstance().openFileFromOS("my", "dir", "file.txt"); // open the file editor

File

void copyFile(String filePath, String toPath, CopyOption... copyOptions)

throws FileException

This method copy a file.

Parameters:

  • filePath, path to file to be copied
  • toPath, path where to copy the file
  • copyOptions*, copy options to pass to the function Files.copy()

Exceptions:

  • FileException, exception thrown if the copy of the file returns a I/O errors

Example:

Jutilas.getInstance().copyFile("path/from/file.txt", "path/to/file.txt");

void renameFile(String filePath, String newName)

throws FileException

This method rename a file.

Parameters:

  • filePath, path to file to be renamed
  • newName, new file name

Exceptions:

  • FileException, exception thrown if the raname of the file returns a I/O errors

Example:

Jutilas.getInstance().renameFile("my/dir/file.txt", "new_name.txt");

String readFile(String filePath)

throws FileException

This method read a file.

Return:
Return a String containing the read text.

Parameters:

  • filePath, path to file to be read

Exceptions:

  • FileException, exception thrown if reading the file returns a I/O errors

Example:

String txt = Jutilas.getInstance().readFile("my/dir/file.txt");

String readFile(File file)

throws FileException

This method read a file.

Return:
Return a String containing the read text.

Parameters:

  • file, file to be read

Exceptions:

  • FileException, exception thrown if reading the file returns a I/O errors

Example:

File file = new File("path/to/file.txt");
String txt = Jutilas.getInstance().readFile(file);

String readRandomAccessFile(File file)

throws FileException

This method read a file using the java.io.RandomAccessFile class.

Return:
Return a String containing the read text.

Parameters:

  • file, file to be read

Exceptions:

  • FileException, exception thrown if reading the file returns a I/O errors

Example:

File file = new File("path/to/file.txt");
String txt = Jutilas.getInstance().readRandomAccessFile(file);

String getLastRowsFile(String filePath, int numRows)

throws FileException

This method read the last row of file, using the java.io.RandomAccessFile class.

Return:
Return a String containing the last row of file.

Parameters:

  • filePath, path to file to be read
  • numRows, number of last rows to be read

Exceptions:

  • FileException, exception thrown if reading the file returns a I/O errors

Example:

String txt = Jutilas.getInstance().getLastRowsFile("path/to/file.txt", 50);

void recursiveDelete(String filePath)

This method delete files, instead if a directory recursively deletes all the files and directories inside it.

Return:
Return a String containing the last row of file.

Parameters:

  • filePath, path to file to be deleted

Example:

Jutilas.getInstance().recursiveDelete("path/to/dir");

void emptyDirectory(String... path)

throws FileException

This method delete all files and directories,inside the directory path passed.

Parameters:

  • path, path to directory to be empty

Example:

Jutilas.getInstance().emptyDirectory("path/to/dir");

boolean isEmptyDirectory(File file)

This method check if the directory is empty.

Return:
Return true if the directory is empty, otherwise false.

Parameters:

  • file, file to be checked

Example:

Jutilas.getInstance().isEmptyDirectory("path/to/dir");