Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By camickr

Thread: Read file and displayed contents on windows

  1. #1
    Rmcgrath is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Read file and displayed contents on windows

    Hi, I'm beginner in Java.
    I'm looking for some help with a basic code where it read a file and the contents displayed onto windows with eclipse swings.
    Code programming is made as my basic understanding java, but it seems it doesn't work, so any help it will be appreciated.

    unused import (is being used for test purpose)


    When code is executed it displayed null, I know could be from code structure, because it have ended and I call to displayed again in windows frames codes.

    --------------------------------
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    
    public class PruebaLector {
    	private File inputfile;
    	private BufferedReader archivo;
    	private String lineaLeida;
    	private String Linea1;	
    	private JFrame ventana;
    	private JPanel panel;
    	private JLabel etiqueta;
    	private JScrollPane cuadro; 
    	private JTextArea texto;
    	private JTextField texto1;
    public PruebaLector(){
    		gui();
    	}
    	public void gui(){
    		try {
    			 inputfile=new File("C:/Users/Mc GRATH Ricardo/Desktop/Prueba de hw.html");
    			 archivo=new BufferedReader(new FileReader(inputfile));
    			// Define variables
    		        boolean eof = false;
    		        //String lineaLeida = "";
    		     // Lee el archivo "in" de forma eficiente e imprime los datos en pantalla
    		        while ( !eof )
    		        {
    		        	// Lee una linea entera
    		            lineaLeida = archivo.readLine();
    		         // Imprime la linea en pantalla
    		            if( lineaLeida != null )
    		            		         
    		            //System.out.println( lineaLeida );
    		            // Si llego al final del archivo, termina la ejecución
    		            //else
    		            //else eof = true;
    		            System.out.println(lineaLeida);
    		        
    		        else eof=true;
    		        }
    		        System.out.println("antes de seguir"+lineaLeida);
    		}
    			catch(Exception e){
    				JOptionPane.showMessageDialog(null,"No se puede mostrar contenido del archivo");
    						
    			}
    try{
    			
    			ventana=new JFrame("ventana Principal");
    			ventana.setSize(600,400);
    			ventana.setVisible(true);
    			panel=new JPanel();
    			panel.setBackground(Color.RED);
    			ventana.add(panel);
    			//etiqueta.setVerticalTextPosition(200);
    			//etiqueta.setHorizontalTextPosition(200);
    			//etiqueta.setSize(300, 300);
    			//panel.add(etiqueta);
    			texto=new JTextArea();
    			texto.setBackground(Color.white);
    			texto.setColumns(50);
    			texto.setLineWrap(true);
    			texto.setRows(15);
    			texto.setWrapStyleWord(true);
    			texto.setText("Mostrando contenido de "+Linea1);
    			panel.add(texto);
    			
    }finally{
    		//archivo.close();
    	}
    	}
    	public static void main(String[] args) {
    	new PruebaLector();
    
    	}
    
    }
    Last edited by pbrockway2; 03-01-2013 at 11:28 PM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Read file and displayed contents on windows

    Hi Rmcgrath, welcome to the forums!

    I have added "code tags" to your post. When you post code put [code] at the start of the code and [/code] at the end. That way the indentation will be preserved. Also it's a good idea to use spaces rather than tabs (or a mixture) to indent. The idea is that the code should be as readable as possible.

    Java Code:
    texto.setText("Mostrando contenido de "+Linea1);
    I don't see that you have assigned anything to the variable Linea1. (Following Java coding standards the variable should really be linea1.)

  3. #3
    Rmcgrath is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Read file and displayed contents on windows

    Dear

    At first many thanks for your recommendation and added code tags.
    By the other way sorry because statement of the variable Linea1 it becomes to try to associated with "lineaLeida"
    As first part of the code is open a file and set the contents of the file onto "lineaLeida" so the purpose is to pass the contents to windows and displayed on JTextArea called texto.
    So idea of the code it can be resumed as the following;

    Open file called "Prueba de hw.html" -> displayed the contents on windows

    Thanks in advances, best regards

    Ricardo Mc GRATH

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,226
    Rep Power
    6

    Default Re: Read file and displayed contents on windows

    use the read(...) method of JTextArea to read data from a file into the textarea.
    DarrylBurke likes this.

  5. #5
    Rmcgrath is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: Read file and displayed contents on windows

    Dear

    I have tried with red, but I couldn't find the way how to implement.
    Well after spending time by associated to a string the contents of Buffered Reader, and class heritage it displayed the last line of the file.
    So I'm stacking if regardless to method implements or my limited acknowledge.

    I attach project class code.

    Thanks best regards

    Mc GRATH Ricardo
    Attached Files Attached Files

Similar Threads

  1. How to read the contents of uploaded file?
    By HardRock in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-25-2012, 11:07 AM
  2. Change JList contents after it's already been displayed?
    By LucienMontierre in forum AWT / Swing
    Replies: 2
    Last Post: 05-04-2012, 05:10 AM
  3. how to read a file and change its contents.
    By mointariq in forum New To Java
    Replies: 1
    Last Post: 04-30-2012, 07:19 PM
  4. Read and Write depending on file contents
    By paulhenderson in forum Java Applets
    Replies: 3
    Last Post: 02-15-2010, 12:38 PM
  5. Replies: 2
    Last Post: 05-11-2009, 10:07 AM

Posting Permissions

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