Programming Fundamentals/Files/Java

files.java

edit
// This program creates a file, adds data to the file, displays the file,
// appends more data to the file, displays the file, and then deletes the file.
// It will not run if the file already exists.
//
// References:
//     https://www.mathsisfun.com/temperature-conversion.html
//     https://en.wikibooks.org/wiki/Java_Programming

import java.util.*;

class files 
{
    public static void main(String[] args) 
    {
        String FILENAME = "~file.txt";
    
        if(fileExists(FILENAME))
        {
            System.out.println("File already exists.\n");
        }
        else
        {
            createFile(FILENAME);
            readFile(FILENAME);
            appendFile(FILENAME);
            readFile(FILENAME);
            deleteFile(FILENAME);
        }
    }
    
    private static void createFile(String filename)
    {
        try
        {
            java.io.File file = new java.io.File(filename);
            java.io.BufferedWriter writer = 
                new java.io.BufferedWriter(new java.io.FileWriter(file));
            float c;
            float f;
            
            writer.write("C\tF\n");
            for(c = 0; c <= 50; c++)
            {
                f = c * 9 / 5 + 32;
                writer.write(c + "\t" + f + "\n");
            }
            writer.close();
        }
        catch(java.io.IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private static void readFile(String filename)
    {
        try
        {
            java.io.File file = new java.io.File(filename);
            java.io.BufferedReader reader = 
                new java.io.BufferedReader(new java.io.FileReader(file));
            String line;

            while(true)
            {
                line = reader.readLine();
                if (line == null)
                {
                    break;
                }
                System.out.println(line);
            }
            reader.close();
            System.out.println("");
        }
        catch(java.io.IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private static void appendFile(String filename)
    {
        try
        {
            java.io.File file = new java.io.File(filename);
            java.io.BufferedWriter writer = 
                new java.io.BufferedWriter(new java.io.FileWriter(file, true));
            float c;
            float f;
            
            for(c = 51; c <= 100; c++)
            {
                f = c * 9 / 5 + 32;
                writer.write(c + "\t" + f + "\n");
            }
            writer.close();
        }
        catch(java.io.IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private static void deleteFile(String filename)
    {
        java.io.File file;
        
        file = new java.io.File(filename);
        file.delete();        
    }
    
    private static boolean fileExists(String filename)
    {
        java.io.File file;
        
        file = new java.io.File(filename);
        return file.exists();
    }
}

Try It

edit

Copy and paste the code above into one of the following free online development environments or use your own Java compiler / interpreter / IDE.

See Also

edit