JPR Notes 6.managing Input Output Files in Java 1
JPR Notes 6.managing Input Output Files in Java 1
In Java, a stream is a path along which the data flows. Every stream has
a source and a destination. We can build a complex file processing sequence
using a series of simple stream operations. Two fundamental types of streams
are Writing streams and Reading streams. While an Writing streams writes
data into a source(file) , an Reading streams is used to read data from
a source(file).
Stream Classes
Types of Streams
The java.io package contains a large number of stream classes that provide
capabilities for processing all types of data. These classes may be categorized into
two groups based on the data type on which they operate.
read(byte b[ ], int n, int m) To read m bytes into b starting from n’th byte
skip(n) To skip over and discard n bytes from the input stream
write(byte b[ ], int n, int m) To write m bytes from array b starting from n’th byte
try {
in = new FileInputStream("input.txt");
out = new FileOutputStream("output.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
}finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
First create a file input.txt and write some content int it.
After running the program output.txt will automatically created.
int charCount = 0;
int wordCount = 0;
int lineCount = 0;
try
{
lineCount++;
currentLine = reader.readLine();
}
import java.io.Serializable;
public class Student implements Serializable{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
import java.io.*;
class Persist{
public static void main(String args[])throws Exception{
Student s1 =new Student(211,"ravi");
out.writeObject(s1);
out.flush();
System.out.println("success");
}
}
Collections in Java
The Collection in Java is a framework that provides an architecture to store and manipulate the
group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, manipulation,
deletion, etc. can be achieved by Java Collections.
class Test
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
/*............ArrayList..............*/
arrL.add(1);
arrL.add(2);
arrL.add(3);
arrL.add(4);
System.out.println(arrL);
System.out.println(Arrays.toString(arr));
}
java.util.Date
The java.util.Date class represents date and time in java. It provides constructors and methods to
deal with date and time in java.
java.util.Date Constructors
System.out.println(date);
}}
2. getDay()
1. setTime():
void setTime(long time): the parameter time - the number of milliseconds. Sets this
Date object to represent a point in time that is time milliseconds after January 1,
1970 00:00:00 GMT 2.getDay()
int getDay():Returns the day of the week represented by this date. The returned
value (0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 =
Friday, 6 = Saturday) represents the day of the week that contains or begins with
the instant in time represented by this Date object, as interpreted in the local time
zone.
import java.util.*;
try{
set.add(count[i]);
System.out.println(set);
System.out.println(sortedSet);
catch(Exception e){}
}}