hey there, can you help me with something. I'm supposed to get data from a single text file and use it on 3 different jTextField. Then if I hit a save button, the data from the textfields will overwrite the existing data of the text file. Unfortunately, I can't make, and instead I used 3 text file. How can I get data from 1 text file? Please help! Here's my code:

public Action() {
   try  {
      bag = new GridBagLayout();
      c = new GridBagConstraints();
      
	  FileReader r1=new FileReader("name.txt");
	  FileReader r2=new FileReader("grade.txt");
	  FileReader r3=new FileReader("age.txt");
	  Scanner fr1=new Scanner(r1);
	  Scanner fr2=new Scanner(r2);
	  Scanner fr3=new Scanner(r3);
	  String storeAll1=""; 
	  String storeAll2=""; 
	  String storeAll3=""; 
	  
	  while(fr1.hasNextLine())  
     {  
      String temp1=fr1.nextLine()+"\n";  
      storeAll1=storeAll1+temp1;
	 }  
     
	  while(fr2.hasNextLine())  
     {  
      String temp2=fr2.nextLine()+"\n";  
      storeAll2=storeAll2+temp2;  
     }  
     
	while(fr3.hasNextLine())  
     {  
      String temp3=fr3.nextLine()+"\n";  
      storeAll3=storeAll3+temp3;  
     }  
     
	  
	  c.insets = new Insets(5, 5, 5, 5);
      panel = new JPanel();
      panel.setLayout(bag);
     
	  
              
      lblUser = new JLabel("Name");
	  lblUser.setBorder(LineBorder.createBlackLineBorder());	
	  c.gridx = 0;
	  c.gridy = 0;
	  panel.add(lblUser, c);
	  
      lblPass = new JLabel("Grade");
	  lblPass.setBorder(LineBorder.createBlackLineBorder());	
	  c.gridx = 0;
	  c.gridy = 1;
      panel.add(lblPass, c);
	  
      lblConfirm = new JLabel("Age");
	  lblConfirm.setBorder(LineBorder.createBlackLineBorder());	
	  c.gridx = 0;
	  c.gridy = 2;
      panel.add(lblConfirm, c);
	  
	  txt = new JTextField(storeAll1);
	  txt.setColumns(20);
	  c.gridx = 1;
	  c.gridy = 0;
      panel.add(txt, c);
      txt.setEditable(false);
	  
	  txt1 = new JTextField(storeAll2);
	  txt1.setColumns(20);
	  c.gridx = 1;
	  c.gridy = 1;
      panel.add(txt1, c);
	  txt1.setEditable(false);
	  
	  txt2 = new JTextField(storeAll3);
	  txt2.setColumns(20);
      c.gridx = 1;
	  c.gridy = 2;
      panel.add(txt2, c);
      txt2.setEditable(false);
	  
      btnEdit = new Button("EDIT", c, 2, 1);
	  btnEdit.setEnabled(true);
	  bag.setConstraints(btnEdit, c);
	  btnEdit.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e)
            {
                txt.setEditable(true);
				txt1.setEditable(true);
				txt2.setEditable(true);
				btnSave.setEnabled(true);
				btnEdit.setEnabled(false);
            }
        });      
	   panel.add(btnEdit);
	   
      btnSave = new Button("SAVE", c, 2, 2);
	  btnSave.setEnabled(false);
bag.setConstraints(btnSave, c);
	  btnSave.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e)
            {
                txt.setEditable(false);
				txt1.setEditable(false);
				txt2.setEditable(false);
				btnEdit.setEnabled(true);
				btnSave.setEnabled(false);
				
		try {
          String text = txt.getText();
          byte b[] = text.getBytes();
          FileOutputStream out = new FileOutputStream("name.txt");
          out.write(b);
          out.close();
		  
		  String text1 = txt1.getText();
          byte c[] = text1.getBytes();
          FileOutputStream out1 = new FileOutputStream("grade.txt");
          out1.write(c);
          out1.close();
		  
		  String text2 = txt2.getText();
          byte d[] = text2.getBytes();
          FileOutputStream out2 = new FileOutputStream("age.txt");
          out2.write(d);
          out2.close();
		  
         } catch(Exception exception) {
           System.out.println("Cannot write to text.txt");
         }


		}		
        }); 
        
      panel.add(btnSave);
	  
      frame = new JFrame("Action Listeners");
      frame.getContentPane().add(panel);
	  frame.setResizable(false);
      frame.setSize(500, 500);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);   
	  }
	  
	  catch(Exception exception)  
    {  
     //Print Error in file processing if it can't process your text file  
     System.out.println("Error in file");  
    }  
   }

What errors do you get. In the catch print the exception that you get in order to know what went wrong.
That message means nothing: System.out.println("Error in file")

catch (Exception e) {
   System.out.println("Error: "+e.getMessage());

   // or less elegant
   e.printStackTrace();
}
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.