ファイル
FILE f = new FILE("temp.txt");
FILE f2 =new FILE("/home/steve/testFile.txt");
FILE f2 = new FILE("/home/stgeve/testFile.txt");
if (f2.exists() {
// File exists. Processit...
} else {
// FILE doesn't exist. Create it...
f2.createNewFile();
}
ストリームの使用
文字ストリーム
ファイルからの読み取り
public List<Employee> readFromDisk(String fileName) {
final String METHOD_NAME = "readFromDisk(String fileName);
List<Employee> ret = new ArrayList<>();
File file = new File(fileName);
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file))) {
StringBuffer sb = new StringBuilder();
int numberOfEmployees = 0;
int charecter = reader.read();
while (character = -1) {
sb.append((char)character);
character = reader.read();
}
log info("Read file: \n" + sb.toString());
int index = 0;
while (index < sb.length() -1) {
StringBuilder line = new StringBuilder();
while ((char)sb.charAt(index) != '\n') {
line.append(sb.charAt(index++));
}
StringTokenizer strok = new StringTokenizer(line.toString(), Person.STATE_DELIMITER);
Employee employee = new Employee();
employee.setState(strok);
log.info("Read Employee: " + employee.toString());
ret.add(employee);
numberoFEmployee++;
index++
}
log.info("Read " + numberOfEmployee + "employee from Disk");
} catch (FileNotFoundException e) {
log.logp(level.SEVERE, SOURCE_CLASS, METHOD_NAME, "Cannot file file " + file.getName() + ", message = " + e.getLocalizedMessage() + e);
} catch (IOException e) {
log.logp(level.SEVERE, SOURCE_CLASS, METHOD_NAME, "IOException occurrred message = " + e.getLocalizedMessage() + e);
}
return ret;
ファイルの書き込み
public boolean saveToDisk(
ストリームのバッファリング