Skip to main content

Java Sample: Reading & Writing Operation using a single file


import java.io.*;
import java.math.*;
class jReadWrite
{   public static void main(String args[])
    {
        DataInputStream dis=null;
        DataOutputStream dos=null;
        File inFile=new File("rand.txt");
        try //Writing Content into File
        {
            dos=new DataOutputStream(new FileOutputStream(inFile));
            for(int i=0;i<=20;i++)
            {
                dos.writeInt((int)i);
            }
        }catch(Exception e){}
        
        try //Reading Content from File
        {
            dis=new DataInputStream(new FileInputStream(inFile));
            for(int i=0;i<=20;i++)
            {
                int n=dis.readInt();
                System.out.print(n+" ");
            }
        }catch(Exception ex){}
        
        try // Closing File
        {
            dis.close();
            dos.close();
        }catch(Exception ey){}
    }
}

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 ...