Skip to main content

Sample: File Operation In JAVA

1. Transfer File Content One to Another


Code>>


import java.io.*;
class jFile
{
    public static void main(String args[])
    {
        File inFile=new File("Input.txt");//Existing File
        File outFile=new File("Output.txt");
        FileReader ins=null;
        FileWriter outs=null;
        try
        {
            ins=new FileReader(inFile);
            outs=new FileWriter(outFile);
            int ch;
            while((ch=ins.read())!=-1)
            {
                outs.write(ch);
            }            
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }finally
        {
            try{
                ins.close();
                outs.close();
            }catch(Exception ex)
            {}
        }
    }
}


2. Writing Bytes to FILE


Code>>
import java.io.*;
class jBytes
{
    public static void main(String args[])
    {
        try{ 
       byte cites[]={'D','E','L','H','I','\n','K','O','L','K','A','T','A'};
       FileOutputStream outfile=null;
       outfile=new FileOutputStream("City.txt");
       outfile.write(cites);
       outfile.close();
        }catch(Exception e)
        {}

     }
}


3. Reading Bytes from FILE
Code>>
import java.io.*;
public class jBytesCopy 
{
    public static void main(String args[])
    {
        try
        {
            FileInputStream inFile=null;
            int b;
            inFile=new FileInputStream("city.txt");
            while((b=inFile.read())!=-1)
            {
                System.out.println((char)b);
            }
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

Comments

Popular posts from this blog

Java Sample: While loop

public class whilechk1 {     public static void main(String args[])     {         int row,column,y;         System.out.print("\nMultiplication Table\n");         row=1;         do         {             column=1;             do             {                 y=row*column;                 System.out.print("\t"+y);                 column++;             }while(column<=10);             System.out.println("\n");             row++;         }while(row<=10);     } }

A Hit Count Using Java Servlet's Session Tracking

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionTracker extends HttpServlet {         public void doGet(HttpServletRequest req,HttpServletResponse res)         throws ServletException,IOException         {                res.setContentType("text/html");                PrintWriter out=res.getWriter();                HttpSession session=req.getSession(true);                Integer count=(Integer)session.getValue("tracker.count");                if(count==null)       ...

Hi! I'm Java...

Java is a computer programming language. It enables programmers to write computer instructions using English based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute. Who Created Java? In the early nineties, Java was created by a team led by James Gosling for Sun Microsystems. It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated webpages . Over the years it has evolved as a successful language for ...