[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 801
  • Last Modified:

how do I read a file in notepad after saving it through a java program

I am writing some data through a java .jar file on to a disc. How do I read the saved file in a notepad? It shows lot of grabage when I open the file in notepad. Here is my code
myFileChooser.showSaveDialog(this);
        path = myFileChooser.getSelectedFile().getPath();
 
        String data = dataDisplay.getText();
 
        try
        {
        PrintWriter outdata = new PrintWriter
                (new FileWriter(path));
        outdata.println(data);
        outdata.close();
 
        }
        catch (IOException err1)
        {
            dataDisplay.append("problem saving Object to disc:"
                    +err1.getMessage()+ '\n');
        }
        catch(Exception err2)
        {
            dataDisplay.append(err2.getMessage());
        }
    }                                         
 
    private void mnuOpenActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // Loading already saved data handling code here:
 
        myFileChooser.showOpenDialog(this);
        path = myFileChooser.getSelectedFile().getPath();
 
 
try
        {
            BufferedReader inmyFile = new BufferedReader(new FileReader(path));
 
dataDisplay.append(inmyFile.readLine());
         inmyFile.close();
}
 
catch(Exception err3)
       {
            dataDisplay.append("\nError loading array" + err3.getMessage()+'\n');
        }

Open in new window

0
chandanchoubey
Asked:
chandanchoubey
  • 2
  • 2
2 Solutions
 
objectsCommented:
what garbage are you seeing.
can u post the txt file

0
 
__geof__Commented:
2 things that can be wrong:
1. dataDisplay.getText(); does not return a readable String. I would check if your data variable holds some text that you can read
2. Notepad doesn't understand that your file is a text file. Does the file have .txt extension? Do you have the same problem in WordPad?
0
 
chandanchoubeyAuthor Commented:
The program is actually a calculator, all numbers which are added, subtracted, multiplied or divided are displayed in a text area (dataDisplay). I am saving all numbers using FileWriter. When I open the saved file in notepad it comes something like following
’ t +6
*9
/85
*85
-9
/9
*87
0
 
__geof__Commented:
Did you get the ouput of dataDisplay.getText()? In a debug console or System.out.println(dataDisplay.getText());

It looks almost correct to me. What are you expecting in there? The result of the calculation? It looks like it prints the calculation itself so it sems that you should look into dataDisplay.getText()
0
 
objectsCommented:
the problem is in your textarea and not your file by the looks
check you aren't adding junk to the text area (as I posted in your previous q)

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now