Concept :Read the file character by character and append it to a String
FileToString.java
import java.io.BufferedReader;
import java.io.FileReader;
public class FileToString
{
public static void main(String[] args) throws Exception
{
String fileContent=readFileAsString("c:\\store\\readme.txt");
System.out.println(fileContent);
}
private static String readFileAsString(String filePath)throws Exception
{
String fileData = new String();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData+=readData;
buf = new char[1024];
}
reader.close();
return fileData.toString();
}
}
No comments:
Post a Comment