Java File Reading with Buffering


package filereadingwithbuffering;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

/**
 *
 * @author tunatore
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        File f = new File(“C:\\newtextfile.txt”); //location of the file 

        try {
            FileWriter writer = new FileWriter(f);
            writer.write(“testing write op.”);
            writer.flush();
            writer.close();

            FileReader reader = new FileReader(f);
            BufferedReader buffReader = new BufferedReader(reader);

            String read = “”;
            while ((read = buffReader.readLine()) != null) {
                System.out.println(read);
            }

            buffReader.close();
            reader.close();

            //make directory with the file class

            File directory = new File(“C:\\testdirectory”);
            directory.mkdir();

            File fileInaDirectory = new File(directory, “newText.txt”);
            fileInaDirectory.createNewFile();

            // you cannot delete a directory if it is not empty

            for (String fileName : directory.list()) {
                System.out.println(fileName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

Advertisements