Simple Program to print text to a file
//print text to a file
import java.io.*;
import static java.lang.System.out;
class file1
{
public static void main(String args[])
{
FileOutputStream o;
PrintStream p;
try
{
o=new FileOutputStream("file1.txt");
p=new PrintStream(o);
p.println("hello world..!");
p.println("From Niraj Chauhan");
p.close();
}
catch(Exception e)
{
out.print("error Writing a file to Hdisk");
}
}
}
Print Data variables to a file
//print data to a file
import java.io.*;
import static java.lang.System.out;
class file2
{
public static void main(String args[])
{
FileOutputStream o;
PrintStream p;
try
{
o=new FileOutputStream("file2.txt");
p=new PrintStream(o);
int a=10;
float b=123.23F;
p.println("a="+a+"\nb="+b);
p.close();
}
catch(Exception e)
{
out.print("error Writing a file to Hdisk");
}
}
}
/copy one file to another file ( PLEASE PASS FILE NAME AS ARGUMENTS)
//copy one file to another file
import java.io.*;
class file4
{
public static void main(String args[])throws Exception
{
int n=args.length;
if(n==0)
System.out.print("PLEASE ADD CMD LINE ARGUMENT AS FILE NAME");
else
{
FileOutputStream out=new FileOutputStream("filecopy.txt");
for(int i=0;i<n;i++)
{
try
{
InputStream in = new FileInputStream(args[i]);
byte buf[]=new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
}
catch(Exception e)
{
}
}
}
}
}
Create a Directory from core java program
//create a directory
import java.io.*;
class file6
{
public static void main(String args[])throws Exception
{
int n=args.length;
if(n==0)
System.out.print("PLEASE ADD CMD LINE ARGUMENT AS DIR NAME");
else
{
for(int i=0;i<n;i++)
{
try
{
boolean success = (new File(args[i])).mkdir();
if(success)
System.out.println("Dir "+args[i]+" Successfully Created..!");
else
System.out.println("Dir "+args[i]+" NOT Successfully Created..!");
}
catch(Exception e)
{
}
}
}
}
}
Make a file name data.txt and save following data
0001Niraj Chauhan 010020030
0002Pratik G patel 010020030
0003Ankur Shah 010020030
0004Amit Parekh 010020030
0005Chetan Chawda 010020030
//Read file & print after Calculations
import java.io.*;
import static java.lang.System.out;
class student
{
int rollno,sub1,sub2,sub3,total;
String name;
float per;
student()
{
System.out.print("ih");
}
student(int rollno,String name,int sub1,int sub2,int sub3)
{
this.rollno=rollno;
this.sub1=sub1;
this.name=name;
this.sub2=sub2;
this.sub3=sub3;
}
public void showdata()
{
out.println("\n\n==================================");
System.out.println("No:"+rollno+"\tname:"+name);
out.println("==================================");
System.out.println("Sub1:"+sub1);
System.out.println("Sub1:"+sub2);
System.out.println("Sub1:"+sub3);
out.println("==================================");
out.println("Total:"+(sub1+sub2+sub3));
out.println("per:"+((sub1+sub2+sub3)/3)+"%");
}
}
class file8
{
public static void main(String args[])throws Exception
{
int count=0;
try
{
FileReader fr=new FileReader("data.txt");
BufferedReader br=new BufferedReader(fr);
String n1;
count=0;int i=0;
count=line("data.txt");
student s[]=new student[count];
while((n1=br.readLine())!=null)
{
int no=Integer.parseInt(n1.substring(0,4));
String name=n1.substring(4,26);
int no1=Integer.parseInt(n1.substring(26,29));
int no2=Integer.parseInt(n1.substring(29,32));
int no3=Integer.parseInt(n1.substring(32,35));
s[i]=new student(no,name,no1,no2,no3);
s[i].showdata();
i++;
}
System.out.println("\nTotal No Of Records Inserted:"+count);
}
catch(Exception e)
{
out.println("\nInput File Damaged..of total line "+count);
}
}
static int line(String fn)throws Exception
{
FileReader fr=new FileReader("data.txt");
BufferedReader br=new BufferedReader(fr);
String n1;int x=0;
while((n1=br.readLine())!=null)
{
x++;
}
return x;
}
}
Read a file Line By line
//Read file line by line
import java.io.*;
class file5
{
public static void main(String args[])throws Exception
{
int n=args.length;
if(n==0)
System.out.print("PLEASE ADD CMD LINE ARGUMENT AS FILE NAME");
else
{
for(int i=0;i<n;i++)
{
try
{
boolean success = (new File(args[i])).mkdir();
FileReader fr=new FileReader(args[i]);
BufferedReader br=new BufferedReader(fr);
String n1;
while((n1=br.readLine())!=null)
{
System.out.println(n1);
}
}
catch(Exception e)
{
}
}
}
}
}
print content of a file by passing file name arguments
//print content of a file by passing file name arguments
import java.io.*;
import static java.lang.System.out;
class file3
{
public static void main(String args[])throws Exception
{
int n=args.length;
if(n==0)
out.print("PLEASE ADD CMD LINE ARGUMENT AS FILE NAME");
else
{
for(int i=0;i<n;i++)
{
try
{
out.println("\n\nthe contents of "+args[i]+" is :\n\n");
InputStream in = new FileInputStream(args[i]);
byte buf[]=new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
}
catch(Exception e)
{
}
}
}
}
}