이전글
https://greed-yb.tistory.com/255
[JAVA] 파일 생성, 삭제, 쓰기, 이름변경, 이동, 복사, 읽기 하기
파일 생성import java.io.File;public class FileUtil { public static void main(String args[]) throws Exception{ try{ // 만들 파일 이름 및 경로 지정 File file = new File("src/main/resources/config/config.txt"); file.createNewFile(); }catch (Exc
greed-yb.tistory.com
배치로 전달받은 txt, xml 등의 json 데이터가 있는 파일들을 읽고
데이터 parsing 과 파일을 이동시켜야 하는 코드가 필요해서 작업하게 되었다
이전글
https://greed-yb.tistory.com/234
[Java] org.json 사용하기
dependency // gradle // https://mvnrepository.com/artifact/org.json/json implementation group: 'org.json', name: 'json', version: '20230227' // maven org.json json 20180813 JSONObject public class RunTestController { public static void main(String[] args)
greed-yb.tistory.com
https://greed-yb.tistory.com/235
[Java] google json-simple 사용하기
dependency // gradle implementation group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5' // maven com.googlecode.json-simple json-simple 1.1.1 com.google.cod
greed-yb.tistory.com
json parsing 이 필요한 경우 참고하자
Controller
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class FileController {
public static void main(String[] args) throws Exception {
// txt 파일 찾아서 이동시키기
List<String> apiTest = apiTxt();
if (apiTest.size() > 0) {
for (int i = 0; i < apiTest.size(); i++) {
System.out.println("apiTest : " + apiTest.get(i));
}
} else {
System.out.println("apiTest : 읽을 파일이 없습니다");
}
}
static List<String> apiTxt() throws IOException {
String fileDir = "src/main/resources/static/testFile/";
/*파일 경로에 있는 파일 가져오기*/
File dir = new File(fileDir);
String[] fileNames = dir.list();
// 파일 내용을 담을 변수
List<String> result = new ArrayList<>();
if (fileNames.length > 0) {
for (String filename : fileNames) {
// 파일 전체경로
String fileFullPath = fileDir + filename;
// 확장자 확인
int index = filename.lastIndexOf(".");
// txt 인 경우만 예시를 들었는데 본인에 맞게 변경 필요
if (filename.substring(index + 1).equals("txt")) {
BufferedReader br = null;
try {
String str = "";
br = new BufferedReader(new FileReader(fileFullPath));
String resultStr = "";
while ((str = br.readLine()) != null) {
resultStr += str;
}
// 파일 내용
result.add(resultStr);
} catch (IOException e) {
System.out.println("에러");
throw e;
} finally {
br.close();
}
// 파일 이동
File Folder = new File(fileDir + "txtTemp");
if (!Folder.exists()) {
try {
Folder.mkdirs(); // 디렉터리 생성.
} catch (Exception e) {
e.getStackTrace();
}
}
// 현재시간 가져오기
LocalDateTime now = LocalDateTime.now();
String formatter = now.format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
// 파일 경로
Path oriFile = Paths.get(fileFullPath);
// 이동시킬 파일 경로 및 파일 이름 설정
Path newFileName = Paths.get(fileDir + "txtTemp/" + formatter + "_" + filename);
try {
// 파일 이동
Path newFilePath = Files.move(oriFile, newFileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
System.out.println("파일이 존재하지 않습니다.");
}
return result;
}
}
TEST

해당 경로에 test.txt 파일을 준비했다
FileController 를 실행시키면


tesfFile -> txtTemp 폴더로 현재 시간으로 이름을 변경하여 파일을 이동시킨다
한 달 전에 작업해 두고서는 이제야 올린다...
'개발 > Java' 카테고리의 다른 글
[Java] ThreadPool 을 이용한 Method 실행하기 (1) | 2025.04.25 |
---|---|
[Java] ExcelUpload - AJAX 파일 업로드 (0) | 2025.04.08 |
[JAVA] Thumbnails을 이용한 ImageUpload resize 적용하기 (0) | 2024.07.16 |
[JAVA] FileUpload(이미지, 파일, 삭제) 및 Download 구현하기 (0) | 2024.07.15 |
[JAVA] CPU , Memory , Disk 사용량 확인하기 (0) | 2024.07.09 |
댓글