Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTextPane...can't read files  RSS feed

 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
instead of dwelling in my focus and style problems, i decided to try to do the file IO stuff only to run into another problem.
this code from my last project works fine

in my new project, all i want to do is open a text file. it should be very simple but it is not working right and i have no idea why.
this doesn't work. it shows the filechooser but when i choose a file it is not displayed in the JTextPane.

however if i put a text file in the same directory and do like i did before, it can read it
URL url = getClass().getResource("test.txt");
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your culprit is mostly in the file.toString(). Is the file an absolute or relative path? If it's relative, it's relative to the class file or your working directory. (I think the latter).
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed the latter.

There is a simpler way to get a URL from a file:
Beware the MalformedURLException though; it should never be thrown but you still need to catch it.
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Rob. that looks like just what i need
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!