Hello i have a question about my code : I'm trying to write a code which would read from command line and then it would create file nd write the input inside:
After the variables have been written, they should be added to a fileif(input.startsWith("add")) { // Call the add method and pass a string array as argument which does not // include "add" and has split the remaining arguments by whitespace. System.out.println("In order to create new file: Write the date,time,hour, header and comment"); Scanner scanner = new Scanner(System.in); Date date = new Date(); String date2 = scanner.nextLine(); SimpleDateFormat formatDate = new SimpleDateFormat("dd.mm.yyyy"); String date_to_string = formatDate.format(date); System.out.println("Date is" + date_to_string); // String date = scanner.toString(); String time = scanner.toString(); Integer duration = scanner.nextInt(); String dur = Integer.toString(duration); String header = scanner.toString(); String comment = scanner.toString(); add(new String[]{date2,time,dur,header,comment}); // add(input.substring(4).split("\\s+")); continue; }
My problem is that the file doesnt wants to be created And I dont know why. I have tried with simple piece of code to create file and with FileWriter since I want to write the variables into it.private void add(String[] tokens) throws IOException { System.out.println("file : "); if(tokens.length < 5) { System.out.println("Illegal arguments"); System.out.println("add [date] [time] [duration] [header] [comment] "+ "--> add a new event with the given arguments; date format: "+ "dd.mm.yyyy; time format: hh:mm; duration in minutes"); System.out.println(); return; } // TODO Check data validity and formating SimpleDateFormat df = new SimpleDateFormat("dd.mm.yyyy HH:mm"); Date date = null; try { date = df.parse(tokens[0] + " " + tokens[1]); } catch (ParseException e){ System.out.println("Error parsing date/time!"); System.out.println("add [date] [time] [duration] [header] [comment] "+ "--> add a new event with the given arguments; date format: "+ "dd.mm.yyyy; time format: hh:mm; duration in minutes"); System.out.println(); return; } int duration = Integer.parseInt(tokens[2]); String header = tokens[3]; String comment = tokens[4]; // ToDo: Write the new date to a file in the directory "events" and send it to the other devices... File eventF = new File("D:\\Uni.lu\\events\\id.event.txt"); try{ if(!eventF.exists()){ eventF.createNewFile(); System.out.println("New file " + eventF.getName() + "was created"); System.out.println("New file " + eventF.getName() + "was created"); } }catch (IOException e){ e.printStackTrace(); } // // try{ // File file = new File("D:\\Uni.lu\\events\\id.event.txt"); // FileWriter fileWriter = new FileWriter(file); // BufferedWriter out = new BufferedWriter(fileWriter); // out.write(date.toString()); // out.write(duration); // out.write(header); // out.write(comment); // out.close(); // out.flush(); // }catch(Exception e){ // System.err.println("Error: " +e.getMessage()); // } // }
Thank you for your help