GSLib

Type: Code Library

|

2023


GSLib is a very own Java library. It is made up of different packages that I have written, each filled with different methods I either find myself using often, or wanted to try coding for fun. I wanted my library to include the following categories:

  1. Utilities
    • File Manager
    • Directory Manager
  2. Statistics
  3. Math
  4. Calculus

So far, I have been able to finish and publish the file management package, but I have not yet finished finished the others. I will also be making sure the whole thing will work with the latest version of Java.

Installation

The library hasn’t been uploaded to any online tool for libraries like Maven or Gradle. The only way to use the library right now is to download the GSLib.zip file from the latest release (available here). You can then unzip the compressed file and copy the directory to your projects.

Using the library

The library is not hard to use once you have it imported into your project. All of the methods are static, meaning you just need to import the classes you need to use and call the methods statically. Here is an example with some classes of the utilities package.

The goal in the example, is to create a file, then create a folder copy the file inside the folder and then copy the folder inside another folder we have created.

Code:

import GSLib.Utilities.FileManager;
import GSLib.Utilities.DirectoryManager;

public class Example {
  public static void main(String[] args) {
    // Create file
    FileManager.newFile("newFile.txt");

    // Create first directory
    DirectoryManager.newDirectory("NewDirectory");

    // Copy file into directory
    FileManager.copyFile("newFile.txt", "NewDirectory/copiedFile.txt");

    // Create another directory
    DirectoryManager.newDirectory("AnotherNewDirectory");

    // Copy first directory into new one
    DirectoryManager.copyDirectory("NewDirectory", "AnotherNewDirectory/CopiedDirectory");
  }
}

Output:

File created: newFile.txt
path: /home/sp0k/Documents/Personal/Languages/Java/Libraries/GSLib/newFile.txt
Successfully created NewDirectory!
Path: /home/sp0k/Documents/Personal/Languages/Java/Libraries/GSLib/NewDirectory
Successfully copied file: newFile.txt to NewDirectory/copiedFile.txt!
Successfully created AnotherNewDirectory!
Path: /home/sp0k/Documents/Personal/Languages/Java/Libraries/GSLib/AnotherNewDirectory
Successfully copied the directory from NewDirectory to AnotherNewDirectory/CopiedDirectory

Keep in mind that your output would come in the terminal that you use for these classes. The colors here are just to make it more readable.

Utilities

The Utilities package is a package containing a few classes that I find useful for management inside the code. I find that a Utilities package makes life easier while programming since I no longer need to rewrite a lot of those methods that I often use, especially the file management methods which I use often with school. You can find the package here along with more documentation on it.

File Manager

The File Manager class contains methods I use for managing files in my code. The more projects I do and the bigger they get, the more I find myself rewriting these methods into my code. This class is the reason I started working on this library. My goal in having this class is to replace the need of having multiple File objects in my code to keep tracks of my files. Instead, I get to use most of the File class methods through static method calls, which might potentially make my code more efficient, but definitely makes it easier to read.

This class lets users do the following:

  • Create a file,
  • Delete a file,
  • Read a file,
  • Write to a file,
  • Rename a file,
  • Move a file,
  • Copy a file,
  • Erase the content of a file.
import GSLib.Utilities.FileManager;

public class Example {
  public static void main(String[] args) {
    // Create a file
    FileManager.newFile(filepath);

    // Delete a file
    FileManager.deleteFile(filepath);

    // Read a file (returns a String)
    String file = FileManager.readFile(filepath);

    // Write to a file
    FileManager.writeToFile(filepath, "Hello, World!");

    // Rename a file
    FileManager.renameFile(filepath, newName);

    // Move a file (Doesn't include the name of the file in the destination path)
    FileManager.moveFile(filepath, dest);

    // Copy a file (Includes the name of the file in the destination path)
    FileManager.copyFile(filepath, dest);

    // Erase content of a file
    FileManager.eraseContent(filepath);
  }
}

Directory Manager

The Directory Manager is the second class I decided to write, once I had time to focus on the project again. Having a manager for my files, I thought that on bigger projects I might want to be able to also manage the directories for my data. This lead to this class. I also like this class because there is over 6 months between this one and File Manager, and the difference between the understanding of what I’m doing and the quality of work is huge.

This class lets users do the following:

  • Create directories,
  • Delete directories (and everything inside),
  • Rename a directory,
  • Move a directory,
  • Copy a directory.
import GSLib.Utilities.DirectoryManager;

public class Example {
  public static void main(String[] args) {
    // Create a directory in current one
    DirectoryManager.newDirectory(dirName);

    // Create a directory at a specific location (Doesn't include the name of the file in the path)
    DirectoryManager.newDirectory(dirName, dirPath);

    // Delete a directory
    DirectoryManager.deleteDirectory(dirPath);

    // Rename a directory
    DirectoryManager.renameDirectory(dirPath, newName);


    // Move a directory (Doesn't include the name of the directory in the destination path)
    DirectoryManager.moveDirectory(dirPath, dest);

    // Copy a directory (Includes the name of the directory in the destination path)
    DirectoryManager.copyDirectory(dirPath, dest);
  }
}

Statistics

Coming soon…

Math

Coming soon…

Calculus

Coming soon…

My thoughts on this project

Even though I am still working on this project, I find that I have learned a lot already about programming, the Java language and libraries. I also find that this project was really useful for me as I am saving myself time when I do projects in Java since now I just import my library into my new project and can easily use them. I also got to learn a lot about libraries creation using linux and how to use libraries while coding without IDEs

© Copyright 2024. Made by Gab Savard.