VPR-Projekt/src/GeneralMethods.java

54 lines
1.5 KiB
Java
Raw Normal View History

2022-11-24 13:59:26 +01:00
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
/**GeneralMethods is a class for commen public methods.
*
* @auhtor Felix Wöstemeyer
*
* @version 1.0
*/
public class GeneralMethods {
/**The method readData gives back an ArrayList from the data of a given file.
*
* @param pathRead the given filepath to read
* @return returns an ArrayList of Strings with the read data
*/
public static ArrayList<String> readData(String pathRead){
try {
ArrayList<String> data = new ArrayList<String>();
List<String> lines = Files.readAllLines(Paths.get(pathRead));
for(String line : lines){
data.add(line);
}
return data;
} catch (IOException e) {
e.printStackTrace();
return new ArrayList<String>();
}
}
/**The method writeData saves the given data to a certain file.
*
* @param pathWrite the given filepath to write
* @param data the data to be saved
*/
public static void writeData(String pathWrite, ArrayList<String> data){
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(pathWrite));
for (String d : data) {
writer.write(d);
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}