1


I'm new around here even though I am have been looking at these forums for ages and I finally need a bit of help; I have tried this;

FileResource file = new FileResource(new File("/a/d/r/e/s/s/file"));
TextArea text = new TextArea();
text.setValue(file);

This;

FileResource file = new FileResource(new File("/a/d/r/e/s/s/file"));
TextArea text = new TextArea();
text.setValue(file.toString());

And;

FileResource file = new FileResource(new File("/a/d/r/e/s/s/file"));
TextArea text = new TextArea();
text.setValue(file.getAbosoluteFile().toString());

And others which are to big to show;

How do I show the file

Auscyber
  • 72
  • 1
  • 2
  • 13
  • 2
    Possible duplicate of [How do I create a Java string from the contents of a file?](http://stackoverflow.com/questions/326390/how-do-i-create-a-java-string-from-the-contents-of-a-file) – shmosel Sep 13 '16 at 22:55
  • I don't think this is a duplicate, because this questioned is about filling a Vaadin component and not just a string. – Axel Meier Sep 14 '16 at 14:47
  • Hi @WillPierlot if any of the answers has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Axel Meier Sep 16 '16 at 09:16

3 Answers3

4

The easiest way would be to use the TextFileProperty:

TextArea text = new TextArea(new TextFileProperty(new File("/a/d/r/e/s/s/file")));

or the longer form:

TextArea text = new TextArea();
text.setPropertyDataSource(new TextFileProperty(new File("/a/d/r/e/s/s/file")));

What this piece of code does is it binds your Field TextArea to a Property. This is Vaadin's data binding mechanism. Property and Field synchronize each other automatically.

If you just want to display the file without editing it, consider using a Vaadin Label instead of the TextArea.

Axel Meier
  • 1,105
  • 2
  • 18
  • 28
  • 1
    Sadly TextFileProperty (and properties in general) seems to be removed from Vaadin 8. Is there an easy to use alternative that I am missing? – sirain Feb 20 '19 at 16:30
1
 final TextArea textField = new TextArea();
 textField.setSizeFull();
 this.addComponent(textField);
 try {
     final File file = new File("/path/to/file");
     final String fileAsString = FileUtils.readFileToString(file);
     textField.setValue(fileAsString);
 } catch (IOException e) {
     e.printStackTrace();
 }

You will need to have the IO component from Apache Commons available to be able to import FileUtils

Chris
  • 11
  • 1
0

Using the java standard library + java 8 streams

TextArea text = new TextArea();
String value = Files.readAllLines(Paths.get(file)).stream().collect(Collectors.joining())
text.setValue(value);   

In Java 7 you can use the readAllLines function iterating over the List generated, in case that the size of the file is really big,follow a different approach, as the one that is explained here http://www.baeldung.com/java-read-lines-large-file

discolojr
  • 595
  • 5
  • 5