Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!74.125.46.80.MISMATCH!postnews.google.com!p16g2000vbs.googlegroups.com!not-for-mail From: bruce Newsgroups: comp.lang.java.programmer Subject: Re: Reading a text file Date: Fri, 11 Feb 2011 20:08:08 -0800 (PST) Organization: http://groups.google.com Lines: 221 Message-ID: References: <19a4db14-7d28-465f-9f49-bdcee93a0ac7@s3g2000vbf.googlegroups.com> NNTP-Posting-Host: 72.145.249.140 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1297483688 30766 127.0.0.1 (12 Feb 2011 04:08:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 12 Feb 2011 04:08:08 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: p16g2000vbs.googlegroups.com; posting-host=72.145.249.140; posting-account=mf1brAkAAABFHW7KlcqUMF6vmODE2KZM User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729; .NET4.0C),gzip(gfe) Xref: csiph.com comp.lang.java.programmer:25706 On Feb 11, 10:42=A0pm, Eric Sosman wrote: > On 2/11/2011 9:55 PM, bruce wrote: > > > > > On Feb 11, 9:51 pm, Eric Sosman =A0wrote: > >> On 2/11/2011 9:26 PM, bruce wrote: > > >>> I am reading a file with > > >>> =A0 =A0 =A0 BufferedReader reader =3D null; > >>> =A0 =A0 =A0 StringBuffer contents =3D new StringBuffer(); > >>> =A0 =A0 =A0 while ((text =3D reader.readLine()) !=3D null) { > >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 contents.append(text) > >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 .append(System.getPropert= y( > >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 "line.separator")= ); > >>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 } > > >>> This code reads the file fine. =A0My problem is that when I put the d= ata > >>> into a text, it is displayed with a space character before ever > >>> character. (bruce =3D> =A0 =A0b r u c e) > > >>> What am I doing wrong?? > > >> =A0 =A0 =A0 Something in the "it is displayed" code would be my guess. > > > This is how I am displaying the input text: > > > txtText.setText(contents.toString()); > > > Is that the wrong way to insert the file data (contents) into the text > > area (txtText)? > > =A0 =A0 =A0Sorry: I can't get your code to compile, much less run and > exhibit desirable or undesirable behavior. > > =A0 =A0 =A0(In other words, STOP dripping your code out line by line by > incomplete out-of-context line! =A0"Doctor, it hurts!" =A0"What hurts?" > "Even worse than yesterday!" =A0Please provide code that is SHORT and > SELF-CONTAINED and COMPLETE and COMPILABLE. =A0Yes, including the > package and imports and class lines: Something someone who feels like > being helpful can dump AS IS through his Java compiler and see what > happens. =A0I can't debug what I can't see. =A0Stop teasing!) > > -- > Eric Sosman > esos...@ieee-dot-org.invalid I am sorry but including all the code is hard to do. I am using NetBeans and it generates a lot of support code. I don't now if you can compile it or not. This program is a test program to teach me how to use the JFileChooser. I know you're trying to help and I do appreciate it.. This is the best I can do. It does include the NetBeans generated code. package VitalRecords; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JFrame; /** * * @author Bruce */ public class VitalRecords extends JFrame { JFileChooser fc; BufferedReader reader =3D null; StringBuffer contents =3D new StringBuffer(); /** Creates new form VitalRecords */ public VitalRecords() { initComponents(); fc =3D new JFileChooser(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { jScrollPane1 =3D new javax.swing.JScrollPane(); txtText =3D new javax.swing.JTextArea(); lblFileName =3D new javax.swing.JLabel(); jMenuBar1 =3D new javax.swing.JMenuBar(); jMenu1 =3D new javax.swing.JMenu(); mnuOpenFile =3D new javax.swing.JMenuItem(); jMenu2 =3D new javax.swing.JMenu(); =20 setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); txtText.setColumns(20); txtText.setRows(5); jScrollPane1.setViewportView(txtText); lblFileName.setText("File Name: "); jMenu1.setText("File"); mnuOpenFile.setText("Open File"); mnuOpenFile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { mnuOpenFileActionPerformed(evt); } }); jMenu1.add(mnuOpenFile); jMenuBar1.add(jMenu1); jMenu2.setText("Edit"); jMenuBar1.add(jMenu2); setJMenuBar(jMenuBar1); javax.swing.GroupLayout layout =3D new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( =20 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 703, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(lblFileName) .addContainerGap(625, Short.MAX_VALUE)) ); layout.setVerticalGroup( =20 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(13, 13, 13) .addComponent(lblFileName) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement= .UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)) ); pack(); }// private void mnuOpenFileActionPerformed(java.awt.event.ActionEvent evt) { int returnVal =3D fc.showOpenDialog(VitalRecords.this); System.out.println(returnVal); if (returnVal =3D=3D JFileChooser.APPROVE_OPTION) { File file =3D fc.getSelectedFile(); String fileLabel =3D lblFileName.getText(); String fileName =3D fc.getName(file); lblFileName.setText(fileLabel + fileName); try { reader =3D new BufferedReader(new FileReader(file)); } catch (FileNotFoundException ex) { =20 Logger.getLogger(VitalRecords.class.getName()).log(Level.SEVERE, null, ex); } String text =3D null; try { while ((text =3D reader.readLine()) !=3D null) { contents.append(text) .append(System.getProperty( "line.separator")); } } catch (IOException ex) { =20 Logger.getLogger(VitalRecords.class.getName()).log(Level.SEVERE, null, ex); } txtText.setText(contents.toString()); System.out.println(contents.toString()); } } /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new VitalRecords().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JMenu jMenu1; private javax.swing.JMenu jMenu2; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lblFileName; private javax.swing.JMenuItem mnuOpenFile; private javax.swing.JTextArea txtText; // End of variables declaration }