• Post Reply Bookmark Topic Watch Topic
  • New Topic

making external text file readable  RSS feed

 
Adrian Lukaszek
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello. I'm new on the forum, so my apologies for bad formatting. Does someone know how can I make following code able to read text files from external folders(it works just when file is located in the same folder as program). I would really appreciate if someone can do this just slightly amending the code(not using BufferedReader and FileReader). Thanks in advance



 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like that code is specifically designed to read a file from the same folder as the class the code is located in, mostly. So if you don't want that, just specify the path to where your file is actually located:



If you don't know where your file is actually located, then you've got a problem. Searching the disk might work if you had some idea of where it might be located, but searching your entire file system isn't going to be practical. You might have to ask the user where the file is... but don't let's go down that road until you say you actually have this problem.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags to your post. Always use the tags: doesn't it look better now
Don't call your Scanner file. I personally would call it fileScan but you can probably think of a better name which makes it obvious it is a Scanner not a File.
What does that mean about replacing the slash?
You have a serious error: you are opening a Scanner but not closing it. you should always close a Scanner/Reader/Writer/Formatter pointing at everything … other than the three Streams which are fields of the System class. (You will get no end of trouble if you close them.) The best way to do that (since Java7) is try‑with‑resources.

Find out about file choosers. There are lots of other ways to do this, but if you have a file chooser, you can use it to navigate to different folders and find a file. When you have chosen a file, you can return a reference to the File object and that can be passed to a Scanner. Then you can read whatever you want from the file. But it must be text, because Scanners only read text, not binary files.
You can pass the same File object to a buffered reader and file reader if you prefer. I presume you know about the Java™ Tutorials section about IO. That is worth a read.

Don't use Math#pow(x, 2). Use x * x instead.
 
Adrian Lukaszek
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys!!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome Show us a version which works please.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!