ファイルの中身をStringで読み出すメソッド作成

This commit is contained in:
r-ca 2024-01-08 11:48:17 +09:00
parent 52657fdbbd
commit 2ba243ea4a
No known key found for this signature in database
GPG Key ID: 6A72911AC73464A9
2 changed files with 28 additions and 0 deletions

View File

@ -26,6 +26,9 @@ public interface FileManager {
File getFile(String fileName);
File getFile(Path path);
String loadText(String fileName);
String loadText(Path path);
boolean isExist(Path path);
boolean isExist(String fileName);

View File

@ -3,6 +3,7 @@ package one.nem.lacerta.source.file.impl;
import android.graphics.Bitmap;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
@ -149,6 +150,30 @@ public class FileManagerImpl implements FileManager {
return path.toFile();
}
@Override
public String loadText(String fileName) {
try(FileInputStream fileInputStream = new FileInputStream(currentDir.resolve(fileName).toFile())) {
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes); // TODO-rca: エラーハンドリング
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public String loadText(Path path) {
try(FileInputStream fileInputStream = new FileInputStream(path.toFile())) {
byte[] bytes = new byte[fileInputStream.available()];
fileInputStream.read(bytes); // TODO-rca: エラーハンドリング
return new String(bytes);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public boolean isExist(Path path) {
logger.debug("isExist", "called");