Reading char by char
public class ReadingFiles {
int read1;
public static void main(String args[]) throws IOException
{
ReadingFiles readFile=new ReadingFiles();
readFile.readCharbyChar();
}
public void readCharbyChar() throws IOException
{
File file= new File("F:\\eclipse\\Practice\\BookWriting\\src\\test\\java\\org\\book\\fileReader.txt");
InputStream inst= new FileInputStream(file) ;
while((read1=inst.read())!=-1)
{
System.out.println((char)read1);
}
}
}
Outcome-
Reading by line by line
public class ReadingFiles {
String readstr;
public static void main(String args[]) throws IOException
{
ReadingFiles readFile=new ReadingFiles();
readFile.readyLineByLine ();
}
public void readyLineByLine() throws IOException
{
FileReader fr1= new FileReader("F:\\eclipse\\Practice\\BookWriting\\src\\test\\java\\org\\book\\fileReader.txt");
BufferedReader bufRead= new BufferedReader(fr1);
//while((read1= bufRead.read())!=-1)
while((readstr= bufRead.readLine())!=null)
{
System.out.println(readstr);
}
}
}
Output-
Reading complete text message in file.
public class ReadingFiles {
public static void main(String args[]) throws IOException
{
ReadingFiles readFile=new ReadingFiles();
readFile.readFileText();
}
public void readFileText() throws IOException
{
String file1=new String(Files.readAllBytes(Paths.get("F:\\eclipse\\Practice\\BookWriting\\src\\test\\java\\org\\book\\fileReader.txt")));
System.out.println(file1);
}
}
Output-
No comments:
Post a Comment