티스토리 뷰



파일에서 String읽어오기, String[]으로 읽어오기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class FileStringGetter {
    public String getString(String location) throws IOException {
 
        byte[] data = Files.readAllBytes(Paths.get(location));
        String dataString = new String(data);
        return dataString;
    }
 
    public String[] getLines(String location) throws IOException {
        String str = getString(location);
        String[] lines = str.split(System.getProperty("line.separator"));
        return lines;
    }
}
cs



java8 방식
info_detail.json에서 string 읽어오기
1
2
byte[] jsonData = Files.readAllBytes(Paths.get("./test_data/info_detail.json"));
String jsonString = new String(jsonData);
cs



filename.txt에 한줄씩 추가하면서 쓰기

1
2
3
4
try (PrintStream out = new PrintStream(new FileOutputStream("filename.txt"))) {
       String hello = matchEvent.toString();
       out.append(hello + "\n");
}
cs


파일에서 한줄씩 읽어오기(옛날방식)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.*;
import java.util.ArrayList;
import java.util.List;
 
public class FileListGetter {
    public List<String> getList(String fileLocation, String fileName) {
 
        List<String> list = new ArrayList<>();
 
        File inFile = new File(fileLocation, fileName);
 
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(inFile));
            String line;
            while ((line = br.readLine()) != null) {
                list.add(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(br != nulltry {br.close(); } catch (IOException e) {}
        }
 
        return list;
    }
}
cs



호출하기
1
2
3
FileListGetter fileListGetter = new FileListGetter();
 
List<String> stringList = fileListGetter.getList("./test_data/""bp_list.txt");
cs



@Slf4j
public class FileListGetter {
    public List<String> getList(String fileLocation, String fileName) {
        File inFile = new File(fileLocation, fileName);
        try {
            return Files.readAllLines(Paths.get(inFile.getPath()), Charset.defaultCharset());
        } catch (IOException e) {
            log.error("{}", e);
        }
 
        return null;
    }
}
cs

파일에 데이터가 여러줄 있는 경우 List<String>으로 return하고 싶을때 사용함


end.



1.파일을 열어서

2.한줄을 쓰고

3.파일을 닫는 기능이다.



fileName은 아래와 같이 path + name으로 넘긴다

String target_file_location = "../data_collector/test_data/json_page/"+"espn_gamecast.json";



소스코드는 아래와 같다.

private void appendTextToFile(String fileName, String value){
BufferedWriter out = null;
try
{
FileWriter fstream = new FileWriter(fileName, true); //true tells to append data.
out = new BufferedWriter(fstream);
out.write(value);
}
catch (IOException e)
{
System.err.println("Error: " + e.getMessage());
}
finally
{
if(out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}


end.



end.





공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함