Java

Примеры, конструкции Java:

Чтение и запись файла:

package test.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class Application {

public static void main(String[] args) throws IOException {
File file = new File(«/home/antoxa/Downloads/test.txt»);
if (!file.exists()) {
createFile(file);
}
List<String> msgToFile = new ArrayList<>();
msgToFile.add(«First line!»);
msgToFile.add(«Second line»);
addToFile(file, msgToFile);
printFile(file);
}

private static void createFile(File file) {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println(«Ошибка создания файла!»);
}
}

private static void printFile(File file) throws IOException {
try (Reader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);) {
StringBuilder builder = new StringBuilder();
String line, separator = System.lineSeparator();
while ((line = reader.readLine()) != null) {
if (builder.length() > 0) {
builder.append(separator);
}
builder.append(line);
}
System.out.println(builder.toString());
}
}

private static void addToFile(File file, List<String> lines) throws FileNotFoundException {
try (PrintWriter writer = new PrintWriter(file)) {
for (String line : lines) {
writer.println(line);
}
}
}

}

Чтение файла свойств: