I have this program that lets you open a file and it reads it in to a JTextArea
all at once using the following code:
try{
String fileContents = new Scanner(new File(fileName)).useDelimiter("\\Z").next();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}
myTextArea.setText(fileContents);
and this works. But my question is how can I read this into my fileContents
string and still have it add in the line breaks every time I get a new-line character?
Here is what I have, but this puts it all in one line in my textArea:
try{
Scanner contentsTextFile = new Scanner(new File(fileName));
while(contentsTextFile.hasNext()){
fileContents = contentsTextFile.useDelimiter("\r\n|\n").nextLine();
}
}catch(FileNotFoundException ex){
ex.printStackTrace();
}
myTextArea.setText(fileContents);
What I would like to see is this line of text get a new line using a delimiter that only reads one line at a time instead of the entire file.
Can someone help?