NEWS: Total Picture Views: 147066 | Total Article Views: 155674 | Top 5 Most Popular Article: 1. Am I Stuck Algorithm with Becker Robot | 2. How to read a JSON and work with it | 3. PickUpThings with Becker Robot | 4. Encrypted Data GUI (Download .Zip) | 5. Create Wall and Things with Becker

File I/O Example Java

Description:

In this example I will be showing how to read,write, show the length of a file and more.

Code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package runtime;

import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jean-Mathieu
 */
public class Launch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        writeToFile();
        readFromFile();
        printFileSize();
        printNumFilesInDir("C:\\temp");
        createFile("C:\\temp\\newSample.txt");
        createFile("C:\\temp\\sampleFile.txt");
    }

    private static void writeToFile() {
        File myFile = new File("C:\\temp\\sampleFile.txt");

        PrintWriter output = null;
        try {
            output = new PrintWriter(myFile);
            output.println("Here's a sample line of code in text");
            output.print("Here's another line of code\n");
            output.printf("The cost of this class is: $%.2f", 19.95);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Launch.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            output.close();
        }
    }

    private static void readFromFile() {
        File myFile = new File("C:\\temp\\sampleFile.txt");
        Scanner input = null;
        try {
            input = new Scanner(myFile);
            while (input.hasNextLine()) {
                System.out.println(input.nextLine());
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Launch.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            input.close();
        }
    }

    private static void printFileSize() {
        File myFile = new File("C:\\temp\\sampleFile.txt");
        if (myFile.exists() && myFile.isFile()) {
            System.out.println("File Size: " + myFile.length() + " bytes");
        }
    }

    private static void printNumFilesInDir(String aDirectory) {
        File myDirectory = new File(aDirectory);
        System.out.println("Num files in " + myDirectory.getName() + ": " + myDirectory.listFiles().length);
    }

    private static void createFile(String aFileName) {
        File aNewFile = new File(aFileName);
        try {
            if (!aNewFile.exists()) {
                aNewFile.createNewFile();
                System.out.println("File " + aNewFile.getName() + " created");
            } else {
                System.out.println("Sorry > " + aNewFile.getName() + " already exists in " + aNewFile.getAbsolutePath());
            }
        } catch (IOException ex) {
            Logger.getLogger(Launch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
Attachments: None

Tags: File Write Read Size

Total Views: 856

My name is Jean-Mathieu

I created this website so other people could enjoy finding useful stuff easier. If you have any question do not hesitate to contact me.

jean8mathieuCreated on 03/25/15


affiliate_link

Disclosure: We are a website that needs compensation to operate like any other website on the internet.
We may receive consideration for our reviews but we are totally unbiased and do not accept paid reviews or fake reviews claiming to be something they are not.