Saturday, November 1, 2014

Java write Pro



Write double to a file using Data Output Stream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteDoubleToFile
{
 public static void main(String[] args)
{
 String strFilePath = "C://FileIO//WriteDouble.txt";
 try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream dos = new DataOutputStream(fos);
      
double d = 165;
dos.writeDouble(d);
dos.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
    }
  }
}

Write float to a file using Data Output Stream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteFloatToFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//WriteFloat.txt";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream dos = new DataOutputStream(fos);
float f = 5.314f;
dos.writeFloat(f);
dos.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
     }
   }
}

Write int to a file using Data Output Stream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteIntToFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//WriteInt.txt";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream dos = new DataOutputStream(fos);
int i= 100;
dos.writeInt(i);
dos.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
    }
  }
}

Write long to a file using Data Output Stream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteLongToFile
{
public static void main(String[] args)
{
String strFilePath = "C://FileIO//WriteLong.txt";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream dos = new DataOutputStream(fos);
     
long l = 65;
dos.writeLong(l);
dos.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
    }
  }
}

Write short to a file using Data Output Stream

import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class WriteShortToFile
{
public static void main(String[] args)
 {
String strFilePath = "C://FileIO//WriteShort.txt";
try
{
FileOutputStream fos = new FileOutputStream(strFilePath);
DataOutputStream dos = new DataOutputStream(fos);
short s = 1;
dos.writeShort(s);
dos.close();
}
catch (IOException e)
{
System.out.println("IOException : " + e);
     }
   }
}

No comments:

Post a Comment