• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

making external text file readable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Sheriff
Posts: 27452
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot guys!!
 
Campbell Ritchie
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome Show us a version which works please.
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic