सामग्री पर जाएँ

कंप्यूटर प्रोग्रामिंग/फ़ाइलें/जावा

विकिविश्वविद्यालय से
// 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.

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();
    }
}

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।