AIM: Write a program in Java to create, write, modify, read operations on a Text file.
FileExample.java
Output
File is Created at specified path so open and check your message
Happy Coding :)
VISIT: https://codesnail.com
FileExample.java
import java.io.*; public class FileExample { public static void main(String[] args) { try { // Creating new Text file File file = new File("C:/JavaProgram/FileExample.txt"); if (!file.exists()) { file.createNewFile(); System.out.println("File created Successfully."); } FileOutputStream fout=new FileOutputStream("C:/JavaProgram/FileExample.txt"); // Writing to new Text file String s="India is great."; byte b[]=s.getBytes(); fout.write(b); fout.close(); System.out.println("Writing Complete!"); FileInputStream fin = new FileInputStream("C:/JavaProgram/FileExample.txt"); //Reading from Text file int i; while((i=fin.read())!=-1) { System.out.print((char)i); } fin.close(); fout=new FileOutputStream("C:/JavaProgram/FileExample.txt"); // Modifying Text file s="I love India."; b=s.getBytes(); fout.write(b); fout.close(); System.out.println("\n Modification Complete!"); fin = new FileInputStream("C:/JavaProgram/FileExample.txt"); //Reading from Text file while((i=fin.read())!=-1) { System.out.print((char)i); } fin.close(); } catch (IOException e) { System.out.println("I/O Exception occurred."); } } }
Output
file example in java by practical server |
File is Created at specified path so open and check your message
|
Happy Coding :)
VISIT: https://codesnail.com
5 Comments
Thank you sir for helping I loved your all practical help code. Once again Thank you
ReplyDeleteWelcome... share with your friends
DeleteThank you sir for helping I loved your all practical help code. Once again Thank you
ReplyDeleteWelcome... share with your friends
DeleteThank you sir for helping I loved your all practical help code.
ReplyDeletePost a Comment