Read File

//FileRead.java
import java.io.*;
class FileRead
{
   public static void main(String args[])
  {
      try{
    // Open the file that is the first
    // command line parameter
    FileInputStream fstream = new FileInputStream("textfile.txt");
    // Get the object of DataInputStream
    DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
    }
    //Close the input stream
    in.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

NB : To read file which placed in the root of class :

new DataInputStream(getClass().getResourceAsStream("textfile.txt"));

Write to File

//FileWrite.java
import java.io.*;
class FileWrite
{
   public static void main(String args[])
  {
      try{
    // Create file
    FileWriter fstream = new FileWriter("out.txt");
        BufferedWriter out = new BufferedWriter(fstream);
    out.write("Hello Java");
    //Close the output stream
    out.close();
    }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

Hope this post help you.😉

For info about my newest post :