Java File Repository is a lightweight Java library inspired by JpaRepository that allows you to store and manage data in text files. This library provides convenient methods for accessing data such as findById, findAll, save, update, and deleteById. It is developed using Java 17.
- Java 17 or higher
- Any Java development environment (e.g., Eclipse, IntelliJ, or VS Code)
- Maven
-
Clone the Java File Repository repository:
git https://github.com/BrodyGaudel/JavaTextRepository.git
-
Build and install the library using Maven:
cd JavaTextRepository mvn clean install -
Add the following dependency to your project's
pom.xmlfile:<dependency> <groupId>org.mounangabouka.brodygaudel</groupId> <artifactId>javaTextRepository</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
-
Refresh your project dependencies, and you're ready to use Java File Repository in your project.
-
Import the necessary classes into your Java or Kotlin file:
import org.mounangabouka.brodygaudel.repository.Repository; import com.example.model.YourModel;
-
Create an instance of the
Repositoryclass, specifying the file path where the data will be stored:Repository<YourModel> repository = new TxtRepository<>("data.txt", YourModel.class);
-
Use the provided methods to interact with the data:
-
Find an object by its ID:
YourModel model = repository.findById(id);
-
Find all objects:
List<YourModel> models = repository.findAll();
-
Save a new object:
YourModel model = new YourModel(); // Set the properties of the model repository.save(model);
-
Update an existing object:
YourModel model = repository.findById(id); // Modify the properties of the model repository.update(model);
-
Delete an object by its ID:
repository.deleteById(id);
-
Here's a simple example of using Java File Repository:
import org.mounangabouka.brodygaudel.repository.Repository;
import com.example.model.User;
public class Main {
public static void main(String[] args) {
Repository<User> userRepository = new TxtRepository<>("users.txt", User.class);
User user1 = new User("john.doe@example.com", "John Doe");
userRepository.save(user1);
User user2 = new User("jane.smith@example.com", "Jane Smith");
userRepository.save(user2);
User foundUser = userRepository.findById(1);
System.out.println(foundUser);
List<User> allUsers = userRepository.findAll();
System.out.println(allUsers);
user2.setName("Jane Johnson");
userRepository.update(user2);
userRepository.deleteById(1);
}
}Contributions to Java File Repository are welcome! If you find any issues or have suggestions for improvements, please feel free to create a pull request or submit an issue in the GitHub repository.
Java File Repository is open-source software licensed under the MIT License. See the LICENSE file for more information.