Filehandling.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Filedata 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. String[] stepsplitarray = routebestand.get(3).split(",");
  28. ArrayList<ArrayList<Integer>> corarray = new ArrayList<ArrayList<Integer>>();
  29. ArrayList<Character> steps = new ArrayList<Character>();
  30. Filedata file = new Filedata();
  31. file.setColumns(Integer.parseInt(sizesplitarray[1]));
  32. file.setRows(Integer.parseInt(sizesplitarray[0]));
  33. for(String cor:corsplitarray){
  34. String[] temparray = cor.split(",");
  35. corarray.add(new ArrayList<Integer>());
  36. corarray.get(corarray.size()-1).add(0, Integer.parseInt(temparray[0]));
  37. corarray.get(corarray.size()-1).add(1, Integer.parseInt(temparray[1]));
  38. }
  39. for(String step:stepsplitarray){
  40. steps.add(step.toCharArray()[0]);
  41. }
  42. file.setCordinates(corarray);
  43. file.setSteps(steps);
  44. System.out.println(file.getCordinates());
  45. return file;
  46. }
  47. } catch (IOException e) {
  48. JOptionPane.showMessageDialog(null, "Route niet geldig!", "Alert: " + "Fout", JOptionPane.INFORMATION_MESSAGE);
  49. System.out.println("Route bestand niet geldig!");
  50. }
  51. return null;
  52. }
  53. //route file schrijven
  54. public void writeRouteFile(int maxx, int maxy, ArrayList<ArrayList<Integer>> route, ArrayList<Character> steps, String aFileName){
  55. Path path = Paths.get(aFileName);
  56. List<String> file = new ArrayList<String>();
  57. String routestring = "";
  58. for(ArrayList<Integer> cor:route){
  59. routestring += cor.get(0) + "," + cor.get(1) + ";" ;
  60. }
  61. String stepstring = "";
  62. for(Character c:steps){
  63. stepstring += c +",";
  64. }
  65. file.add("broboticsrouteplanner");
  66. file.add(maxx+","+maxy);
  67. file.add(routestring);
  68. file.add(stepstring);
  69. try {
  70. Files.write(path, file, ENCODING);
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. //nieuwe routefile aanmaken
  76. public void createRouteFile(String filename, List<String> aLines, String aFileName) throws IOException {
  77. File file = new File("example.txt");
  78. Path path = Paths.get(aFileName);
  79. Files.write(path, aLines, ENCODING);
  80. }
  81. }