Java Program on How to Write Data in File
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Program 1
package dataflair;
import java.io.*;
public class TestWrite1
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos=null;
byte b[];
try
{
File F=new File("d://filedata");
F.mkdir();
fos=new FileOutputStream("d://filedata/student.txt");
char ch;
String S="Data Flair free course with free certificate ,Indore MP India";
b=S.getBytes();
fos.write(b);
// for(int i=0;i<b.length;i++)
// System.out.print(" "+b[i]);
fos.flush();
// for(int i=0;i<S.length();i++)
// {
// ch=S.charAt(i);
// fos.write(ch);
// }
System.out.println("File Created.......");
}
finally
{
fos.close();
}
}
}Program 2
package dataflair;
import java.io.*;
public class TestFileWrite2
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos=null;
byte b[];
try
{
File F=new File("d://filedata");
F.mkdir();
fos=new FileOutputStream("d://filedata/student.txt");
char ch;
String S="Data Flair Free Course With Free Certificate ,Indore MP India";
for (int i = 0; i < S.length(); i++)
{
ch=S.charAt(i);
if(Character.isUpperCase(ch))
fos.write(ch);
}
System.out.println("File Created.......");
}
finally
{
fos.close();
}
}
}Program 3
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dataflair;
import java.io.*;
/**
*
* @author admin
*/
public class TestFileWrite3
{
public static void main(String[] args) throws IOException
{
FileOutputStream fos=null;
byte b[];
try
{
File F=new File("d://filedata");
F.mkdir();
File F1=new File("d://filedata/student123.txt");
fos=new FileOutputStream(F1);
char ch;
String S="Data Flair Free Course With Free Certificate ,Indore MP India";
for (int i = 0; i < S.length(); i++)
{
ch=S.charAt(i);
if(Character.isUpperCase(ch))
fos.write(ch);
}
System.out.println("File Created.......");
}
finally
{
fos.close();
}
}
} We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google

