How to read file line by line in Java How to read and write in text file in Java Java File Reading and Writing Files in Java Reading a text file in Java
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
try {
FileReader fro = new FileReader("C:\\myFile.txt");
BufferedReader bro = new BufferedReader(fro);
String strline=bro.readLine();
String stringFromFile ="";
while( strline!= null ) // end of the file
{
stringFromFile+= strline;
strline= bro.readLine( );// read next line
}
System.out.println(stringFromFile);
bro.close( );
}
catch( FileNotFoundException filenotfoundexp ){
System.out.println( "myFile.txt, does not exist" );
}
catch(IOException ioexception){
ioexception.printStackTrace( );
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
try {
FileReader fro = new FileReader("C:\\myFile.txt");
BufferedReader bro = new BufferedReader(fro);
String strline=bro.readLine();
String stringFromFile ="";
while( strline!= null ) // end of the file
{
stringFromFile+= strline;
strline= bro.readLine( );// read next line
}
System.out.println(stringFromFile);
bro.close( );
}
catch( FileNotFoundException filenotfoundexp ){
System.out.println( "myFile.txt, does not exist" );
}
catch(IOException ioexception){
ioexception.printStackTrace( );
}
}
}
0 comments:
Post a Comment