Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Tutorial 40 Creating and Writing Text Files
- //code for reading a file and writing a file is almost identical
- //but instead of using a BufferedReader, you use a BufferedWriter and instead of a FileReader you use a FileWriter
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class App {
- public static void main(String[] args) {
- File file = new File("test.txt");
- try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
- br.write("This is line one");
- br.newLine();
- br.write("This is line two");
- br.newLine();
- br.write("Last line.");
- } catch (IOException e) {
- System.out.println("Unable to read file " + file.toString());
- }
- }
- }
- //---------------------------------------------------------------------------------------------------------------------------------------
- /* Run Results:
- blank
- */
- // a new "test.txt" file should be created with the words:
- /*
- This is line one
- This is line two
- Last line.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement