Results 1 to 8 of 8
  1. #1
    Henray is offline Member
    Join Date
    Jul 2013
    Location
    Canada
    Posts
    9
    Rep Power
    0

    Default Question about Reading a File with Scanner and then Writing with BufferedWriter

    Hello guys, I am new to this forum. I sincerely apologize if this post does not belong to this sub-forum, but I thought New To Java seemed fitting.

    I am currently working on an assignment question which asks me to write a method which(takes input String source and String destination) is supposed to read, and copy from the source file and then write it to a destination file. This should happen line by line. It should also return a boolean whether if the copy was successful or not.

    The java file compiles properly, however once I run it (inputting a source.txt file with 5 lines and an empty destination.txt file.), only the very last line of the source file is being copied. If anyone at all could help me it would be kindly appreciated! Thank you for your time.

    I would also appreciate it if anyone could help me improve the code, as in where should I be closing/opening my BufferedWriter to be the most efficient for the computer to process?

    Here is my code below:

    Java Code:
    public class FileUtilities{
      
      public static boolean copyFile(String source, String destination){
        boolean isDone = true;
        File readMe = new File(source);
        
        try{
          Scanner scanner = new Scanner(readMe);
          while(scanner.hasNextLine()) { 
            String lineReadIn = scanner.nextLine();
            FileWriter lineReadOut = new FileWriter(destination);
            BufferedWriter out = new BufferedWriter(lineReadOut);
            out.write(lineReadIn);
            out.newLine();
            out.close();
          }
          scanner.close();
          return isDone;
        }
        catch(IOException e){
          isDone = false;
          return isDone;
        }
      }
      
      public static void main(String[] args){
      System.out.println(copyFile("C:\\Users\\User\\Desktop\\source.txt","C:\\Users\\User\\Desktop\\destination.txt"));
      }
    }
    Last edited by Henray; 07-01-2013 at 06:10 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    Create the FileWriter before you enter the loop. By creating it inside the loop, you're writing each line to a new file with the same name, and only the last one survives.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    Also, do not return from a catch block and do print the stack trace of any caught Exception.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Henray is offline Member
    Join Date
    Jul 2013
    Location
    Canada
    Posts
    9
    Rep Power
    0

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    Quote Originally Posted by DarrylBurke View Post
    Also, do not return from a catch block and do print the stack trace of any caught Exception.

    db
    Thank you kindly for your time. Could you please explain to me why it is wrong to return from a catch block?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,008
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    Quote Originally Posted by Henray View Post
    Thank you kindly for your time. Could you please explain to me why it is wrong to return from a catch block?
    Returning from a catch-block isn't incorrect per se, but you're not handling the exception: i.e. you should close your Scanner and your FileWriter; but you should also do that when no exception was thrown; an excellent opportunity for a finally-block ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Henray is offline Member
    Join Date
    Jul 2013
    Location
    Canada
    Posts
    9
    Rep Power
    0

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    Thank you!

  7. #7
    Henray is offline Member
    Join Date
    Jul 2013
    Location
    Canada
    Posts
    9
    Rep Power
    0

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    Quote Originally Posted by JosAH View Post
    Returning from a catch-block isn't incorrect per se, but you're not handling the exception: i.e. you should close your Scanner and your FileWriter; but you should also do that when no exception was thrown; an excellent opportunity for a finally-block ...

    kind regards,

    Jos
    Thank you for your advice. When I write a finally block and move my .close() methods into this finally block, the compiler tells me "cannot find symbol: variable out".
    I know that it is because the finally block is not in scope and therefore unable to see the BufferedWriter object. (this also occurs when I try to add the .close() methods into the catch block)
    Could you tell me how I could fix this?

    Many thanks,

    Henray

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Question about Reading a File with Scanner and then Writing with BufferedWriter

    The standard idiom is to declare your variables before the start of the try { ... } block and explicitly assign them to null, then close() resources in the finally { ... } block after first testing that they are not null.

    Note that you may need a try { ... } catch (...) { ... } nested inside the finally { ...} block.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. A small question: reading and writing files
    By rakster in forum New To Java
    Replies: 3
    Last Post: 06-01-2013, 12:36 PM
  2. Scanner problem when reading file
    By Gatsu in forum New To Java
    Replies: 8
    Last Post: 12-24-2012, 01:56 PM
  3. Replies: 1
    Last Post: 11-11-2012, 01:55 AM
  4. Reading a file with Scanner
    By Games2Design in forum New To Java
    Replies: 5
    Last Post: 10-04-2012, 10:10 AM
  5. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 09:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •