This is my program for a notepad!
I am getting error at the part where I'm trying to extract the text from another .txt file!!
I used the method realLine() but it only reads one line!!
I want to know what other methods can I use here to extract the entire content!!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; 
import java.io.*;
class MenuDemo extends JFrame implements ActionListener {
Container con= getContentPane();

        JMenuBar bar = new JMenuBar();
        JMenu file = new JMenu("File");
        JMenu edit = new JMenu("Edit");
        JMenu format = new JMenu("Format");
        JMenu view = new JMenu("View");
        JMenu help = new JMenu("Help");

        JMenuItem mi1= new JMenuItem("New");
        JMenuItem mi2= new JMenuItem("Save");
        JMenuItem mi3= new JMenuItem("Save as..");
        JMenuItem mi4= new JMenuItem("Open");
        JMenuItem mi5= new JMenuItem("Page setup");
        JMenuItem mi6= new JMenuItem("Print");
        JMenuItem mi7= new JMenuItem("Exit");
        JMenuItem mi8= new JMenuItem("undo");
        JMenuItem mi9= new JMenuItem("cut");
        JMenuItem mi10= new JMenuItem("copy");
        JMenuItem mi11= new JMenuItem("paste");
        JMenuItem mi12= new JMenuItem("Delete");
        JMenuItem mi13= new JMenuItem("wordwrap");
        JMenuItem mi14= new JMenuItem("Font");
        JMenuItem mi15= new JMenuItem("Status bar");
        JMenuItem mi16= new JMenuItem("View help");
        JMenuItem mi17= new JMenuItem("about notepad");

       TextArea t= new TextArea();
       JFileChooser ch= new JFileChooser("");

MenuDemo(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
requestFocus(true);
setTitle("Notepad");

        setSize(300,300);
        setLocation(100,100);
        setJMenuBar(bar);
        t.setBounds(0,0,290,290);
        bar.add(file);
        bar.add(edit);
        bar.add(format);
        bar.add(view);
        bar.add(help);
        file.add(mi1);
        file.add(mi2);
        file.add(mi3);
        file.add(mi4);
        file.add(mi5);
        file.add(mi6);
        file.add(mi7);
        edit.add(mi8);
        edit.add(mi9);
        edit.add(mi10);
        edit.add(mi11);
        edit.add(mi12);
        format.add(mi13);
        format.add(mi14);
        view.add(mi15);
        help.add(mi16);
        help.add(mi17);
        add(t);
       mi1.addActionListener(this);
       mi2.addActionListener(this);
       mi3.addActionListener(this);
       mi4.addActionListener(this);
       mi7.addActionListener(this); 
       setVisible(true);
    }   
public void actionPerformed(ActionEvent e){
String s1="";

if(e.getSource()==mi1){
if(!t.getText().equals("")){
int j=JOptionPane.showConfirmDialog(null,"Do you want to save any changes to the document?");

if(j==0){
    try{
    ch.showSaveDialog(null);
    s1=t.getText();
        File selectedFile=ch.getSelectedFile();    

    selectedFile.createNewFile();
    System.out.println("New file is created...");
    PrintStream out= new PrintStream(new FileOutputStream(selectedFile));

    out.println(s1);
    out.flush();
    out.close();
    }catch(Exception ex){}    
}//save



else if(j==1){
    t.setText("");

}


}}

if(e.getSource() ==mi2){
    try{
    ch.showSaveDialog(null);
    s1=t.getText();
        File selectedFile=ch.getSelectedFile();    

    selectedFile.createNewFile();
    System.out.println("New file is created...");
    PrintStream out= new PrintStream(new FileOutputStream(selectedFile));

    out.println(s1);
    out.flush();
    out.close();
    }catch(Exception ex){}    
}//save

if(e.getSource()==mi3){
try{
    ch.showSaveDialog(null);
    s1=t.getText();
        File selectedFile=ch.getSelectedFile();    

    selectedFile.createNewFile();
    System.out.println("New file is created...");
    PrintStream out= new PrintStream(new FileOutputStream(selectedFile));

    out.println(s1);
    out.flush();
    out.close();
    }catch(Exception ex){}    
}//saveas



if(e.getSource() == mi4){
try{
    ch.showOpenDialog(null);
    File selectedFile  = ch.getSelectedFile();
    DataInputStream in= new DataInputStream(new FileInputStream(selectedFile));
    String str=in.readFully();
    in.close();
    t.setText(str);
  }catch(Exception ex){}  

}//open


if(e.getSource() ==mi7)
System.exit(0);

}//exit
    public static void main(String args[]){
    MenuDemo md = new MenuDemo();
    }
}

Just use readLine inside a simple loop to read all the lines and append them to your edit buffer.

then the condition for loop will be? less then or equal to number of lines! How can we determine the number of lines?

readLine returns a null when you reach end of file, so you loop reading lines until you get a null.

nailed it :D
thankyou ^_^

That's great! Please consider marking this thread as "solved" so people will know there is an answer here.

Done :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.