tag
tag

Reputation:

Remove last character/line

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?

Upvotes: 5

Views: 18293

Answers (8)

putuyuwono
putuyuwono

Reputation: 119

Its quite easy.. You just need to tweak your code a bit.

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

I believe this code produce your desired function at minimum cost.

Upvotes: 1

Jason Day
Jason Day

Reputation: 8839

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);
}

Upvotes: 2

user56250
user56250

Reputation:

how about:

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

Upvotes: 6

Aaron Digulla
Aaron Digulla

Reputation: 328536

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.

Upvotes: 1

xtofl
xtofl

Reputation: 41509

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'.

Upvotes: 1

Romain Linsolas
Romain Linsolas

Reputation: 81577

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.

Upvotes: 4

Pierre
Pierre

Reputation: 35226

(...)
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);
(...)

Upvotes: 4

Shawn
Shawn

Reputation: 19793

How about

if (textArea.length > 0) textArea.Text = textArea.Text.Substring(0 ,textArea.Text.Length - 1)

Upvotes: 1

Related Questions