View Javadoc
1   package io.github.magwas.coder;
2   
3   import java.io.IOException;
4   import java.nio.file.Files;
5   import java.nio.file.Path;
6   import java.nio.file.StandardOpenOption;
7   
8   import org.springframework.stereotype.Service;
9   
10  @Service
11  public class FileWriterService implements ErrorMessages {
12  	public Void apply(String fileName, String content) {
13  		try {
14  			Path path = Path.of(fileName);
15  			Files.createDirectories(path.getParent());
16  			Files.writeString(path, content, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
17  			return null;
18  		} catch (IOException e) {
19  			throw new RuntimeException(ErrorMessages.FILE_ERROR, e);
20  		}
21  	}
22  }