Results 1 to 8 of 8
  1. #1
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Reading external txt file then display contents to JTextArea

    I am working on a simple little diary program that should be able to load a file (based on input given by the user in the form of a JTextField). I want to be able to have a user type in the name of the file, then have the program look for that file, then load the contents into the JTextArea. It seems like I am able to CREATE the file okay, but if I edit the file and fill with garbage text, then try to LOAD the file, I get no data in the JTextArea. I have been trying different ways between SCANNER and BufferedReader and this seemed like the closest way to accomplish what I needed according to what I read. If someone can help me figure out what is stopping me from reading the file I would greatly appreciate it.


    Java Code:
    package com.sscce.ryan;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Main {
    
    	private String x;
    	private JFrame frame;
    	private JTextField name;
    	private JButton button, button2;
    	private BufferedReader br;
    	private BufferedWriter bw;
    	private JTextArea field;
    	
    	
    	public Main(){
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setMinimumSize(new Dimension(400,600));
    		frame.setVisible(true);
    		frame.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(15,15,15,15);
    		
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = i;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		name = new JTextField(10);
    		frame.add(name, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		button = new JButton("LOAD FILE");
    		button.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				loadFile();
    				setFieldText();
    			}
    		});
    		frame.add(button, c);
    		
    		c.gridx = 0;
    		c.gridy = 2;
    		button2 = new JButton("CREATE FILE");
    		button2.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				createFile();
    			}
    		});
    		frame.add(button2, c);
    		
    		c.gridx = 0;
    		c.gridy = 3;
    		field = new JTextArea(20, 40);
    		frame.add(field, c);
    		
    	}
    	// MAIN ENTRY
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				new Main();
    			}
    		});
    	}
    
    	
    	// LOADS FILE
    	public void loadFile(){
    		try{
    			br = new BufferedReader(new FileReader(name.getText() + ".txt"));
    			while((x = br.readLine()) != null){
    				x = x.concat(br.toString());
    			}
    			
    		}catch(IOException e){
    			e.printStackTrace();
    		}finally{
    			try {
    				br.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    
    		
    		}
    		
    	// CREATES A NEW FILE
    	public void createFile(){
    		try {
    			bw = new BufferedWriter(new FileWriter(name.getText() + ".txt"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			try {
    				bw.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}	
    	
    	// SHOULD SET THE TEXT OF FIELD TO BE TXT FROM INCOMING FILE
    	public void setFieldText(){
    		field.setText(x);
    	}
    }
    Last edited by rru96; 10-02-2013 at 05:32 AM. Reason: edited code

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Reading external txt file then display contents to JTextArea

    Where in your code do you actually read the file and add it to the JTextArea?

  3. #3
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Reading external txt file then display contents to JTextArea

    *Updated code, lines 95 and 96 are where I am trying to read the file.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Reading external txt file then display contents to JTextArea

    OK

    You read a line from the file. You then concat onto the end of the line just read a String representation of the BufferedReader. Why???? If reading the file was successful where do you add it to the JTextArea?

  5. #5
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Reading external txt file then display contents to JTextArea

    I see where a toString() on the BufferedReader was not what I needed, but I still am somewhat confused by everything. Below is my updated version but still not working.

    Java Code:
    package com.sscce.ryan;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Main {
    
    	private String x;
    	private JFrame frame;
    	private JTextField name;
    	private JButton button, button2;
    	private BufferedReader br;
    	private BufferedWriter bw;
    	private JTextArea field;
    	
    	
    	public Main(){
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setMinimumSize(new Dimension(400,600));
    		frame.setVisible(true);
    		frame.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(15,15,15,15);
    		
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = i;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		name = new JTextField(10);
    		frame.add(name, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		button = new JButton("LOAD FILE");
    		button.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				loadFile();
    			}
    		});
    		frame.add(button, c);
    		
    		c.gridx = 0;
    		c.gridy = 2;
    		button2 = new JButton("CREATE FILE");
    		button2.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				createFile();
    			}
    		});
    		frame.add(button2, c);
    		
    		c.gridx = 0;
    		c.gridy = 3;
    		field = new JTextArea(20, 40);
    		frame.add(field, c);
    		
    	}
    	// MAIN ENTRY
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				new Main();
    			}
    		});
    	}
    
    	
    	// LOADS FILE
    	public void loadFile(){
    		try{
    			br = new BufferedReader(new FileReader(name.getText() + ".txt"));
    			while((x = br.readLine()) != null){
    				x = br.readLine();
    				field.append(x);
    				field.setText(x);
    				
    			}
    			
    		}catch(IOException e){
    			e.printStackTrace();
    		}finally{
    			try {
    				br.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    
    		
    		}
    		
    	// CREATES A NEW FILE
    	public void createFile(){
    		try {
    			bw = new BufferedWriter(new FileWriter(name.getText() + ".txt"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			try {
    				bw.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}	
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Reading external txt file then display contents to JTextArea

    Now your code reads 2 lines but only adds one to the text area.

  7. #7
    rru96 is offline Senior Member
    Join Date
    Sep 2012
    Posts
    108
    Rep Power
    0

    Default Re: Reading external txt file then display contents to JTextArea

    Thank you so much Junky.

    Java Code:
    package com.sscce.ryan;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class Main {
    
    	private String x;
    	private JFrame frame;
    	private JTextField name;
    	private JButton button, button2;
    	private BufferedReader br;
    	private BufferedWriter bw;
    	private JTextArea field;
    	
    	
    	public Main(){
    		frame = new JFrame();
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setMinimumSize(new Dimension(400,600));
    		frame.setVisible(true);
    		frame.setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		Insets i = new Insets(15,15,15,15);
    		
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = i;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		name = new JTextField(10);
    		frame.add(name, c);
    		
    		c.gridx = 0;
    		c.gridy = 1;
    		button = new JButton("LOAD FILE");
    		button.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				loadFile();
    			}
    		});
    		frame.add(button, c);
    		
    		c.gridx = 0;
    		c.gridy = 2;
    		button2 = new JButton("CREATE FILE");
    		button2.addActionListener(new ActionListener() {
    			
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				createFile();
    			}
    		});
    		frame.add(button2, c);
    		
    		c.gridx = 0;
    		c.gridy = 3;
    		field = new JTextArea(20, 40);
    		frame.add(field, c);
    		
    	}
    	// MAIN ENTRY
    	public static void main(String[] args){
    		EventQueue.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				new Main();
    			}
    		});
    	}
    
    	
    	// LOADS FILE
    	public void loadFile(){
    		try{
    			br = new BufferedReader(new FileReader(name.getText() + ".txt"));
    			while((x = br.readLine()) != null){
    				field.append(x);
    				
    			}
    			
    		}catch(IOException e){
    			e.printStackTrace();
    		}finally{
    			try {
    				br.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    
    		
    		}
    		
    	// CREATES A NEW FILE
    	public void createFile(){
    		try {
    			bw = new BufferedWriter(new FileWriter(name.getText() + ".txt"));
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}finally{
    			try {
    				bw.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    	}	
    }
    Last edited by rru96; 10-02-2013 at 07:22 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Reading external txt file then display contents to JTextArea

    That still is not correct. Why are you concatenating x to itself?

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 07:35 PM
  2. Display partial file contents in JTextArea
    By tmoehlman in forum New To Java
    Replies: 0
    Last Post: 11-03-2009, 12:03 AM
  3. Replies: 12
    Last Post: 10-09-2009, 07:26 AM
  4. Replies: 17
    Last Post: 07-13-2009, 06:44 AM
  5. viewing the contents of a text file in JTextArea
    By warship in forum New To Java
    Replies: 0
    Last Post: 07-17-2007, 03:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •