Reading the text content from file with out loop through.
Sample Program:
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ReadFile {
public static void main(String[] args) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get("C:\\filename.txt"));
String fileContent = new String(encoded, Charset.defaultCharset());
System.out.println(fileContent);
}
}
Sample Program:
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ReadFile {
public static void main(String[] args) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get("C:\\filename.txt"));
String fileContent = new String(encoded, Charset.defaultCharset());
System.out.println(fileContent);
}
}
Reading the text content from File using 'while' loop.
Sample Program:
import java.io.BufferedReader;
import java.io.FileReader;
public class Read2File {
public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("C:\\filename.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String fileLine = null;
StringBuffer temp = new StringBuffer();
while((fileLine = bufferedReader.readLine()) != null) {
temp.append(fileLine+"\n");
}
System.out.println(temp);
bufferedReader.close();
fileReader.close();
fileReader.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment