tags:

views:

1280

answers:

7

Hello,

I have a snippet of code that prints text from a file to a JTextArea called textArea.

Unfortunately the method I'm using goes line by line (not ideal) and so I have to append each line with a \n

This is fine for now but a new line is created at the end.

The code I have is as follows:

class menuOpen implements ActionListener {
 public void actionPerformed(ActionEvent e)
  {
  try {
   File filePath = new File("c:\\test.txt");
   FileInputStream file = new FileInputStream(filePath);
   BufferedReader br = new BufferedReader(new InputStreamReader(file));
   String displayText;

   while ((displayText = br.readLine()) != null) {
    textArea.append(displayText + "\n");
   }

  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  } catch (IOException e1) {
   e1.printStackTrace();
  }
 }
}

Can anyone help me get rid of that last line?

A: 

How about

if (textArea.length > 0) textArea.Text = textArea.Text.Substring(0 ,textArea.Text.Length - 1)
Shawn Simon
+2  A: 
(...)
FileReader r= new FileReader(filePath);
StringBuilder b=new StringBuilder();
int n=0;
char array[]=new char[1024];
while((n=r.read(array))!=-1) b.append(array,0,n);
r.close();
String content=b.toString();
textArea.setText(content.substring(0,content.lengt()-1);
(...)
Pierre
This seems to work perfectly, I'll test it out a bit
+2  A: 

Another idea:

boolean firstLine = true;
while ((displayText = br.readLine()) != null) {
    if (firstLine) {
        firstLine = false;
    } else {
        textArea.append("\n");
    }
    textArea.append(displayText);
}

The idea is to append a line break before the new line to display, except for the first line of the file.

romaintaz
A: 

Apparently you want a newline between two lines, not after each line. This means you should have at least two lines:

if (d = br.readLine()) != null ) {
    textArea.append(displayText);
    while (d = br.readLine()) != null ) {
        textArea.append( "\n" + displayText);
    }
}

Of course, it looks more complex. That's because 'between' is more complex than 'after'.

xtofl
A: 

In your loop:

while ((displayText = br.readLine()) != null) {
    if (textArea.length() > 0)
        textArea.append("\n");
    textArea.append(displayText);
}

i.e. if there is already some text in your textarea, insert a newline.

Aaron Digulla
A: 

The easiest way is to not use BufferedReader.readLine(). For example:

BufferedReader in = new BufferedReader(new FileReader(filePath));
char[] buf = new char[4096];
for (int count = in.read(buf); count != -1; count = in.read(buf)) {
    textArea.append(new String(buf, 0, count));
}

EDIT

I should have seen this before, but a much better way is to let the JTextArea read the file:

BufferedReader in = new BufferedReader(new FileReader(filePath));
textArea.read(in, null);

This will still read in the newline at the end, but it will normalize all the line endings in your text (see the javadocs for DefaultEditorKit for an explanation of how line endings are handled). So you can get rid of the trailing newline with something like this:

// line endings are normalized, will always be "\n" regardless of platform
if (textArea.getText().endsWith("\n")) {
    Document doc = ta.getDocument();
    doc.remove(doc.getLength() - 1, 1);
}
Jason Day
A: 

how about:

text.substring(0,text.lastIndexOf('\n'));
radekg