Read int from
file using Data Input Stream
import
java.io.DataInputStream;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
public class
ReadIntFromFile
{
public static void
main(String[] args)
{
String strFilePath =
"C://FileIO//readInt.txt";
try
{
FileInputStream fin =
new FileInputStream(strFilePath);
DataInputStream din =
new DataInputStream(fin);
int i = din.readInt();
System.out.println("int
: " + i);
din.close();
}
catch(FileNotFoundException
fe)
{
System.out.println("FileNotFoundException
: " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException
: " + ioe);
}
}
}
Read long from
file using Data Input Stream
import
java.io.DataInputStream;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
public class
ReadLongFromFile
{
public static void
main(String[] args)
{
String strFilePath =
"C://FileIO//readLong.txt";
try
{
FileInputStream fin =
new FileInputStream(strFilePath);
DataInputStream din = new DataInputStream(fin);
long l =
din.readLong();
System.out.println("long
: " + l);
din.close();
}
catch(FileNotFoundException
fe)
{
System.out.println("FileNotFoundException
: " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException
: " + ioe);
}
}
}
Read short from
file using Data Input Stream
import
java.io.DataInputStream;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
public class
ReadShortFromFile
{
public static void
main(String[] args)
{
String
strFilePath = "C://FileIO//readShort.txt";
try
{
FileInputStream fin =
new FileInputStream(strFilePath);
DataInputStream din =
new DataInputStream(fin);
short s =
din.readShort();
System.out.println("short
: " + s);
din.close();
}
catch(FileNotFoundException
fe)
{
System.out.println("FileNotFoundException
: " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException
: " + ioe);
}
}
}
Read unsigned
byte from file using Data Input Stream
import
java.io.DataInputStream;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.IOException;
public class ReadUnsignedByteFromFile
{
public static void
main(String[] args)
{
String strFilePath =
"C://FileIO//readUnsignedByte.txt";
try
{
FileInputStream fin =
new FileInputStream(strFilePath); DataInputStream din = new
DataInputStream(fin);
int i =
din.readUnsignedByte();
System.out.println("Unsinged
byte value : " + i);
din.close();
}
catch(FileNotFoundException
fe)
{
System.out.println("FileNotFoundException
: " + fe);
}
catch(IOException ioe)
{
System.out.println("IOException
: " + ioe);
}
}
}
Create Data
Output Stream from File Output Stream
import
java.io.DataOutputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
public class
CreateDataOutputStream
{
public static
void main(String[] args)
{
String
strFilePath = "C://FileIO//demo.txt";
try
{
FileOutputStream fos =
new FileOutputStream(strFilePath);
DataOutputStream dos =
new DataOutputStream(fos);
}
catch(FileNotFoundException
fe)
{
System.out.println("FileNotFoundException
: " + fe);
}
}
}
Determine
number of bytes written to Data Output Stream
import
java.io.DataOutputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
public class
NumberOfBytesWritten
{
public static void main(String[] args)
{
String strFilePath =
"C://FileIO//NumberOfBytes.txt";
try
{
FileOutputStream fos =
new FileOutputStream(strFilePath);
DataOutputStream dos =
new DataOutputStream(fos);
String str =
"Example to show number of bytes written to stream";
dos.writeBytes(str);
int bytesWritten =
dos.size();
System.out.println("Total
" + bytesWritten + " bytes are written to stream.");
dos.close();
}
catch (IOException e)
{
System.out.println("IOException
: " + e);
}
}
}
No comments:
Post a Comment