Results 1 to 3 of 3
Thread: Looking for Help
- 10-28-2009, 11:00 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
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");
}
}
- 10-29-2009, 07:14 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You could test it and see if it works.
- 10-29-2009, 07:22 AM #3
Member
- Join Date
- Oct 2009
- Posts
- 12
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks