Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problem with FileWriter

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with FileWriter

    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:

                if(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;
                }
    After the variables have been written, they should be added to a file
     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());
    //        }
    //
     
        }
    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.

    Thank you for your help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with FileWriter

    problem is that the file doesnt wants to be created
    If you are getting an error message, copy the full text of the error message and paste it here.
    Otherwise please explain what "doesn't want to create" means.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with FileWriter

    thats the problem,i don't get any error messages that's why I'm so confused

    --- Update ---

    Well, i found that there is sth wrong with th scanner. I tried to print out the input but it doesnt work

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with FileWriter

    it doesnt work
    Please explain what that means.

    --- Update ---

    Can you post the code that uses FileWriter and that doesn't work? The posted code has commented out the section that uses FileWriter.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with FileWriter

    here is the code with FileWriter

            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];
     
            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());
            }
     
     
        }


    --- Update ---

    When I try to print out the input with System.out.println() it simply doesnt do anything it looks like that the scanner doesnt do anytthing

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with FileWriter

    it simply doesnt do anything
    Which println() are you talking about? What variable has the input that you are trying to print? I don't see anything that you are talking about in post#5.

    Can you copy the full contents of the console from when you execute the program and post it here?

    Can you make a small, complete program that compiles, executes and shows the problem? The small bits of code you have posted don't show enough.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    57
    My Mood
    Angelic
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with FileWriter

     if(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.nextLine();
                    Integer duration = scanner.nextInt();
                    String dur = Integer.toString(duration);
                    String header = scanner.nextLine();
                    String comment = scanner.nextLine();
     
                    System.out.println(date2);
                    System.out.println(time);
                    System.out.println(dur);
                    System.out.println(header);
                    System.out.println(comment);
     
    //                add(new String[]{date2,time,dur,header,comment});
     
                    add(input.substring(4).split("\\s+"));
                    continue;
                }
    I'm talking about these prints. Where I try to print out the input..

    my console shows this when I compile it:

    EventManager started
    If you need help please type help
    >
    add
    In order to create new file: Write the date,time,hour, header and comment
    14.03.2012 14:00 30 header comment
    Date is29.25.2013
    here is th main function which checks if the input is correct and is responsible for creating a File and storing all the input inside the file

     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...
     
            System.out.println("aaaa");
    //        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());
            }
     
     
        }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Problem with FileWriter

    Can you post a SMALL, COMPLETE program that will compile, execute and show the problem?

       System.out.println("date2="+date2); // add a String so you can see it this executed
    If date2 = "" you will only see a blank line. With a String you will know.

    --- Update ---

    When I execute this version of the posted code:
           try{
                File file = new File("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();          // <<< IOException: Stream closed
            }catch(Exception e){
                System.err.println("Error: " +e.getMessage());
                e.printStackTrace();
            }

    I get the noted error and this in the event.txt file:
    date.toString()durationheadercomment
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  2. How to set character encoding of FileWriter
    By gkokaisel in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 8th, 2012, 03:06 PM
  3. [SOLVED] Fliereader/Filewriter (Need Help!)
    By Techsam in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 12th, 2012, 09:22 PM
  4. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  5. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM