.csv 파일에 8 개의 정수를 저장하고 싶습니다. (파일 이름은 EditText에서 입력으로 받아 들여집니다.) 원하는대로 검색 할 수 있습니다.안드로이드에서 csv 파일을 읽고 쓰는 방법?
2
A
답변
1
당신이 사용할 수있는 파일 이름으로 활용하려면 다음
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(fileName+".csv"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
:
try {
String content = "Separe here integers by semi-colon";
File file = new File(fileName +".csv");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
파일을 읽으려면 :
EditText fileNameEdit= (EditText) getActivity().findViewById(R.id.fileName);
String fileName = fileNameEdit.getText().toString();
그런 다음 디스크에있는 파일을 쓰기
그럼 당신은 분할 기능을 사용할 수있는 정수를합니다 :
String[] intArray = sCurrentLine.split(";");
+0
전체 파일 이름을 어떻게 언급합니까? 나는 EditText에서 "/ storage/sdcard0/test"를 언급하려했지만 항상 catch 블록으로 간다. AndroidManifest.xml에 WRITE_EXTERNAL_STORAGE 권한을 추가해야합니까? 도와주세요. 감사. –
+0
편집 : 완료! AndroidManifest.xml 파일에 WRITE_EXTERNAL_STORAGE 권한을 추가해야했습니다. –
0
CSV는 일반 텍스트 파일입니다. 여기서 값은 문자 ";"로 나뉩니다. 그래서, 예를 들어 BufferedWriter를 써서 작성할 수 있습니다.
자습서 : 문제는 어디 http://www.codeproject.com/Questions/491823/Read-fWriteplusCSVplusinplusplusAndroid –
입니까? – PKlumpp