Java Basics 115 – Writing Text File
Continue from previous tutorial.
2-1) Right-click Package name. Select New/Java Class.
2-2) Set Class Name as WriteFile.
Delete all comments and unnecessary codes, leaving only the following codes.
package textfiles; public class WriteFile {
} |
package textfiles; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException; public class WriteFile {
} |
4-1) Add variables and a constructor.
package textfiles; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException; public class WriteFile { private String path; private boolean append_to_file = false; public WriteFile(String file_path) { path = file_path; } } |
4-2) Add a second constructor.
package textfiles; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException; public class WriteFile { private String path; private boolean append_to_file = false; public WriteFile(String file_path) { path = file_path; } public WriteFile(String file_path, boolean append_value) { path = file_path; append_to_file = append_value; } } |
4-3) Add method to write file.
package textfiles; import java.io.FileWriter; import java.io.PrintWriter; import java.io.IOException; public class WriteFile { private String path; private boolean append_to_file = false; public WriteFile(String file_path) { path = file_path; } public WriteFile(String file_path, boolean append_value) { path = file_path; append_to_file = append_value; } public void writeToFile(String textLine) throws IOException { FileWriter write = new FileWriter(path, append_to_file); PrintWriter print_line = new PrintWriter(write); print_line.printf("%s" + "%n", textLine); print_line.close(); } } |
5-1) Append codes to the end of the existing codes.
package textfiles; import java.io.IOException; public class FileData { public static void main(String[] args) throws IOException { String file_name = "C:/test.txt"; try { ReadFile file = new ReadFile(file_name); String[] aryLines = file.OpenFile(); int i; for (i = 0; i < aryLines.length; i++) { System.out.println(aryLines[ i]); } } catch (IOException e) { System.out.println(e.getMessage()); } /* WRITING DATA TO FILE */ WriteFile data = new WriteFile( file_name , true ); data.writeToFile( "This is another line of text" ); System.out.println( "Text File Written To" ); } } |
5-2) Run.
5-3) Run again.
5-3) Modify the codes to add TRY…CATCH Block.
package textfiles; import java.io.IOException; public class FileData { public static void main(String[] args) throws IOException { String file_name = "C:/test.txt"; try { ReadFile file = new ReadFile(file_name); String[] aryLines = file.OpenFile(); int i; for (i = 0; i < aryLines.length; i++) { System.out.println(aryLines[ i]); } } catch (IOException e) { System.out.println(e.getMessage()); } /* WRITING DATA TO FILE */ try { WriteFile data = new WriteFile(file_name, true); data.writeToFile("This is another line of text"); System.out.println("Text File Written To"); } catch (IOException e) { System.out.println(e.getMessage()); } } } |
http://www.homeandlearn.co.uk/java/write_to_textfile.html