Professional Documents
Culture Documents
theFileclass
for
typical
operations such as copying, moving,
renaming,
creating,
opening,
deleting, and appending to files. You
can also use theFileclass to get and
set file attributes orDate Time
information related to the creation,
access, and writing of a file.
STREAMS
Streamis the abstract base class of all
for
character
data.Byte
streamsare
intended for general purpose input and
output. Of course, fundamentally all data
consist of patterns of bits grouped into 8-bit
bytes. So, logically all streams could be
called "byte streams". However streams that
are intended for bytes that represent
characters are called "character streams"
and all others are called "byte streams".
Buffered Streams
Buffered
Streamcan be composed
around certain types of streams. It
provides implementations for reading and
writing bytes to an underlying data source
or repository. Use Binary Reader andBinary
Writer for reading and writing other data
types.
import java.io.*;
public class CopyFile{
private static void copyfile(String srFile, String
dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in
the specified directory.");
System.exit(0);
catch(IOException e){
System.out.println(e.getMessage());
FileOutputStream(f2,true);
copyfile("c:\\out.txt","d:\\kulafu.edi");
out.write(buf, 0, len);
int len;
while ((len = in.read(buf)) > 0){
in.close();
out.close();
System.out.println("File copied.");
}
import java.io.*;
class FileWrite
{
public static void main(String args[])
{
try{
// Create file
FileWriter fstream = new
FileWriter("out.txt");
BufferedWriter out = new
BufferedWriter(fstream);
out.write("pink");
//Close the output stream
out.close();
}catch (Exception e){//Catch
exception if any
System.err.println("Error: " +
e.getMessage());
}
}
}
import java.io.*;
public class ReadAccessFile{
public static void main(String[]
args) throws IOException{
BufferedReader in = new
BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter File name
: ");
String str = in.readLine();
File file = new File(str);
if(!file.exists())
{
System.out.println("File does not
exist.");
System.exit(0);
}
public
class Server
Socket serverSocket;
int bytesReceived, totalReceived = 0;
byte[] receivedData = new byte[2000000];
try
{
bytesSent.ToString());
serverSocket.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 2000);
try
do
{
bytesReceived = serverSocket.Receive(receivedData,
0, receivedData.Length, SocketFlags.None);
totalReceived += bytesReceived;
IPAddress ipAddress =
Dns.Resolve(Dns.GetHostName()).AddressList[0];
while(bytesReceived != 0);
catch(SocketException e)
if(e.ErrorCode == WSAETIMEDOUT)
{
// Data was not received within the given time.
else
{
Console.WriteLine("{0}: {1}\n",
e.GetType().Name, e.Message);
finally
{
Console.WriteLine("{0} bytes received.\n",
totalReceived.ToString());
finally
serverSocket.Shutdown(SocketShutdown.Both);
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyCharacters {
public static void main(String[] args) throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
inputStream = new FileReader("xanadu.txt");
outputStream = new FileWriter("characteroutput.txt");
int c;
while ((c = inputStream.read()) != -1) {
outputStream.write(c);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}