Reputation: 79
My code:
JFileChooser opChooser=new JFileChooser();
FileNameExtensionFilter filter=new FileNameExtensionFilter("XML File", "xml");
opChooser.setFileFilter(filter);
int returnVal=opChooser.showOpenDialog(null);
File chosenFile=opChooser.getSelectedFile();
try
{
if (returnVal==JFileChooser.APPROVE_OPTION)
{
BufferedReader br=new BufferedReader(new FileReader(chosenFile));
currentDirectory="";
textPane.setText("");
textPaneError.setText("");
currentDirectory=chosenFile.getAbsolutePath();
String data = "";
while ((br.readLine())!=null)
{
data += br.readLine();
}
doc.insertString(0, data, null);
br.close();
}
}
catch (IOException | BadLocationException ex)
{
JOptionPane.showMessageDialog(null, "ERROR!!!");
}
}
My xml file that i want open in my app:
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Result:
<from>Jani</from><body>Don't forget me this weekend!</body>null
I would be grateful if someone could explain me why the result is not like a xml file? Where are the first two line, and why the last string inserted is null?
Upvotes: 0
Views: 58
Reputation: 5264
You are calling br.readLine()
in the while condition without setting it to a value
try this:
String data = "";
String temp = "";
while ((temp = br.readLine()) != null)
{
data += temp;
}
Upvotes: 1