First Let's discuss important file handling methods in java. which is important for file handling.
->Create a new file;
1) File f= new File()
2) File f =new File("pathname"); /* pathname should be like this "C:\\dir1\\dir2\\file.txt" */
3) File f = new File("directoryname",filename);
4) File getName() /* for retrieval of filename */
5) mkdir() for creating directory.
filename.mkdir();
6) mkdirs() for creating all parents directory of filename.
filename.mkdirs()
7) list() for listing all the directories inside any directory. In java directory & files r synonym to each other.
Methods for Input - Output to a File
-->By Stream Classes

Methods for reading file (InputStream Classes)
int read() throws IOException /* Returns next Byte from InputStream */
int read(byte a[]) throws IOException /*Returns some byte from InputStream & Saves data into byte array a */
As it is for OutputStream Classes
void write(int a) throws IOException /* Write a into file */
void write(byte a[]) throws IOException /* Write some no. of bytes to OutputStream */
Program to input content into a File
import java.io.*;
public class InputClassByStream {
public static void main(String args[]) throws IOException
{
String s="new file i m -1 innocent";
File f=new File("C:\\Users\\op\\Desktop\\file.txt"); \* Create File Object *\
FileOutputStream fi=new FileOutputStream(f);
byte b[]=s.getBytes(); \* Convert String into Bytes array *\
fi.write(b);
byte b1=33;
fi.write(b1);
}
}
Program show output from a File
public class OutputClassByStream{
public static void main(String args[]) throws IOException
{
File f=new File("C:\\Users\\op\\Desktop\\file.txt");
FileInputStream fo=new FileInputStream(f);
int i;
while((i=fo.read())!=-1){ \* Returns -1 at the end of Stream *\
System.out.print((char)i); \* cast into char value *\
}
}
}
/* InputSreamReader i = new InputStreamReader(filename,"UTF8"); */
This line we use when we require stream into particular character set. Here UTF8 character set.
Character Stream
Click on image to enlarge it and you will see actual path in the reader classes. They derived from Object class going downward and end up with filereader & filewriter.
FileReader is used to read stream of characters from a file.
CharArrayReader is used to read stream from array. Here array act as inputstream.
BufferedReader is used to taking character stream from anywhere(Console,arrays). It is used to hold large piece of data into a buffer. Which minimize IO time.
FilterReader is used to filter output based on the filteration apply on input stream.
InputStreamReader is used to convert byte streams into specific charset.
Read Character From Command Prompt, Store in a File and print output on command prompt.
import java.io.*;
public class IOClassByStream{
public static void main(String args[]) throws IOException
{
File f = new File("C:\\Users\\op\\Desktop\\file.txt");
FileWriter fo=new FileWriter(f); // for Writiing File
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
FileReader fi=new FileReader(f); // for Reading File
int ch;
while((ch=br.read())!='e')
fo.write(ch); // Writing Characters to file
while((ch=fi.read())!=-1){
System.out.print(fi.read()); // Reading String contents from file
}
}
}
If you r facing problem in any part of program then please inform us. We will get back to you shorter. This program is easiest way to get input from console and feed content into the file.



0 comments:
Post a Comment