I am trying to read a .java file in JTextArea and no matter what method I use to read in the file, the formatting is never saved. The actual code is ok, but the comments are always confused. Here are my attempts.
public void readFileData(File file)
{
Scanner fileScanner = null;
try
{
fileScanner = new Scanner(file);
while(fileScanner.hasNextLine())
{
String line = fileScanner.nextLine();
output.append(line + newline);
}
}
catch(FileNotFoundException fnfe)
{
System.err.println(fnfe.getMessage());
}
}
public void readFileData(File file)
{
Scanner fileScanner = null;
try
{
fileScanner = new Scanner(file);
fileScanner.useDelimiter("\\Z");
String fullText = fileScanner.next();
output.append(fullText + newline);
}
catch(FileNotFoundException fnfe)
{
System.err.println(fnfe.getMessage());
}
}
public void readFileData(File file)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
String line = "";
while((line = reader.readLine()) != null)
{
output.append(line + newline);
}
}
Is there a way to keep the formatting the same?
PS - also posted on http://www.coderanch.com/t/539685/java/java/keep-formatting-while-reading-files#2448353
Hunter
source
share