Filehandling.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package customComponents;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.charset.Charset;
  5. import java.nio.charset.StandardCharsets;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import javax.swing.JOptionPane;
  12. public class Filehandling {
  13. final static Charset ENCODING = StandardCharsets.UTF_8;
  14. public Filehandling(){
  15. }
  16. //route uitlezen uit een file en de file controleren
  17. public ArrayList<ArrayList<Integer>> readRouteFile(String aFileName){
  18. Path path = Paths.get(aFileName);
  19. try {
  20. List<String> routebestand = Files.readAllLines(path, ENCODING);
  21. if(routebestand.size() == 0 || !routebestand.get(0).contains("broboticsrouteplanner")){
  22. JOptionPane.showConfirmDialog(null, "Route niet geldig!", "Alert: " + "Fout", JOptionPane.INFORMATION_MESSAGE);
  23. System.out.println("Route bestand niet geldig!");
  24. }else{
  25. String[] sizesplitarray = routebestand.get(1).split(",");
  26. String[] corsplitarray = routebestand.get(2).split(";");
  27. ArrayList<ArrayList<Integer>> corarray = new ArrayList<ArrayList<Integer>>();
  28. corarray.add(new ArrayList<Integer>());
  29. corarray.get(0).add(Integer.parseInt(sizesplitarray[0]));
  30. corarray.get(0).add(Integer.parseInt(sizesplitarray[1]));
  31. int teller = 1;
  32. for(String cor:corsplitarray){
  33. String[] temparray = cor.split(",");
  34. corarray.add(teller, new ArrayList<Integer>());
  35. corarray.get(teller).add(0, Integer.parseInt(temparray[0]));
  36. corarray.get(teller).add(1, Integer.parseInt(temparray[1]));
  37. teller++;
  38. }
  39. return corarray;
  40. }
  41. } catch (IOException e) {
  42. JOptionPane.showMessageDialog(null, "Route niet geldig!", "Alert: " + "Fout", JOptionPane.INFORMATION_MESSAGE);
  43. System.out.println("Route bestand niet geldig!");
  44. }
  45. return null;
  46. }
  47. //route file schrijven
  48. public void writeRouteFile(int maxx, int maxy, ArrayList<ArrayList<Integer>> route, String aFileName){
  49. Path path = Paths.get(aFileName);
  50. List<String> file = new ArrayList<String>();
  51. String routestring = "";
  52. for(ArrayList<Integer> cor:route){
  53. routestring += cor.get(0) + "," + cor.get(1) + ";" ;
  54. }
  55. file.add("broboticsrouteplanner");
  56. file.add(maxx+","+maxy);
  57. file.add(routestring + ";");
  58. try {
  59. Files.write(path, file, ENCODING);
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. //nieuwe routefile aanmaken
  65. public void createRouteFile(String filename, List<String> aLines, String aFileName) throws IOException {
  66. File file = new File("example.txt");
  67. Path path = Paths.get(aFileName);
  68. Files.write(path, aLines, ENCODING);
  69. }
  70. }