How to Read and Write Data from File in Java

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

New! Keyboard shortcuts … Drive keyboard shortcuts have been updated to give you first-letters navigation
import java.io.*;
class TestWrite
{
    public static void main(String args[]) throws Exception
    {
        //FileOutputStream fos=null;
           FileInputStream fis=null;
        try
        {
                 fis=new FileInputStream("c://dataflairjava/student.txt");
                 char ch;
                 int n;
                 while((n=fis.read())!=-1)
                 {
                    ch=(char)n;
                    System.out.print(ch);
                 }  
            //  fos=new FileOutputStream("c://dataflairjava/student.txt",true);
            //  System.out.println("File created...");
            //      char ch='t';
            //      fos.flush();
            //      fos.write(ch);
        }
        
        finally
        {
            //fos.close();
             fis.close();
        }
    }
}

 

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *