Results 1 to 5 of 5
- 03-01-2013, 10:54 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 3
- Rep Power
- 0
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
- 03-01-2013, 11:35 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
I don't see that you have assigned anything to the variable Linea1. (Following Java coding standards the variable should really be linea1.)Java Code:texto.setText("Mostrando contenido de "+Linea1);
- 03-02-2013, 04:19 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 3
- Rep Power
- 0
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
- 03-02-2013, 05:16 PM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Re: Read file and displayed contents on windows
use the read(...) method of JTextArea to read data from a file into the textarea.
- 03-05-2013, 09:50 PM #5
Member
- Join Date
- Mar 2013
- Posts
- 3
- Rep Power
- 0
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
Similar Threads
-
How to read the contents of uploaded file?
By HardRock in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 06-25-2012, 11:07 AM -
Change JList contents after it's already been displayed?
By LucienMontierre in forum AWT / SwingReplies: 2Last Post: 05-04-2012, 05:10 AM -
how to read a file and change its contents.
By mointariq in forum New To JavaReplies: 1Last Post: 04-30-2012, 07:19 PM -
Read and Write depending on file contents
By paulhenderson in forum Java AppletsReplies: 3Last Post: 02-15-2010, 12:38 PM -
Read file from directory, update contents of the each file
By svpriyan in forum New To JavaReplies: 2Last Post: 05-11-2009, 10:07 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks