package example8_3_FileOpening; import java.io.FileWriter; import java.io.IOException; public class EditingFiles { public static void main(String[] args) { new EditingFiles().example(); } public void example() { // How we created the files. // for(int i=0; i<10; i++) { // writeFile(i); // } // How we read and write the files. for(int i=0; i<10; i++) { // TODO: Implement editing by reading and writing files. } } /** * Write a file with index fileIndex. * * E.g., write C:\temp\closeMe\file5.txt for * file index 5. * * @param fileIndex the index of the file to be written. */ public void writeFile(int fileIndex) { String filename = "C:\\temp\\closeMe\\file"+fileIndex+".txt"; FileWriter writer; try { writer = new FileWriter(filename); } catch (IOException e) { System.err.println("Could not open file for writing: "+filename); writer = null; } if(null != writer) { System.out.println("Great! We have a file for index "+fileIndex+"."); try { writer.write(""+fileIndex); writer.flush(); } catch (IOException e) { System.err.println("Warning: Could not write bytes to file:"+filename); } } } }