Results 1 to 3 of 3
  1. #1
    melody is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Question Looking for Help

    Write a method that accepts a String Containing the name of a text file as a parameter and makes a copy of that file called [filename]-copy. Methods used to read and write a series of String to a text file are provided for you. If an exception is generated while the file is being written, your method output. If an exception is generated while the file is being written your method should print"output error" to standard output.

    public static void main(String[]args){
    copyFile("input-data.txt");
    }
    private static ArrayList<String>readTextFile(String filename)throws IOException{
    BufferedReader buffer = new BufferedReader(new FileReader(filename));
    ArrayList<String>data = new ArrayList<String>();
    String line = "";
    while((line = buffer.readLine()!=null)
    data.add(line);
    return data;
    }

    private static void writeTextFile(String filename, ArrayList<String>) data) throws IOException{
    BufferedWriter buffer = new BufferedWriter(new FileWriter(filename));
    Iterator<String>iter = data.iterator();
    while(iter.hasNext()){
    buffer.write(iter.next());
    buffer.newLine();
    }
    buffer.flush();
    buffer.close();
    }






    My answer is this, is it right?? thank you

    public static void copyFile(String filename)
    {
    try{

    writeTextFile(filename+"-copy",readTextFile(filename));
    }catch(IOException e){
    System.out.println("output error");
    }

    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You could test it and see if it works.

  3. #3
    Lizzip is offline Member
    Join Date
    Oct 2009
    Posts
    12
    Rep Power
    0

Posting Permissions

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