• Post Reply Bookmark Topic Watch Topic
  • New Topic

SongList.txt in Head First Java  RSS feed

 
Mike Skyriach
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello y'all.  I'm having trouble with this code on page 535 of Head First Java 2nd edition.  I've wrote a simple text document that contains song names and artist names, and using split() method to separate song/artist names by using "/".   Using FileReader class, File class, and BufferedReader class.  I've saved the .txt file to the folder where the code is.  JVM says it can't find the file to read.  So I'm assuming I placed the .txt file in the wrong place, where is the appropriate place to put it so JVM can read file from there?  Thanks
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You shou‍ld be able to find the file wherever it is. It is all a case of finding the correct path to its location.
Try using a file chooserJava™ Tutorials link to find the file instead. You might have to use null as the first argument to the showOpenDialog method.
 
Mike Skyriach
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You shou‍ld be able to find the file wherever it is. It is all a case of finding the correct path to its location.
Try using a file chooserJava™ Tutorials link to find the file instead. You might have to use null as the first argument to the showOpenDialog method.


Hmm ok.  I've done JFileChooser in some of the codes as written by the book.  I haven't even got around to actually use the API to piece together code snippets to make them work.  The first argument you said, I assume it has to be this line?


public void actionPerformed(ActionEvent e) {
    //Handle open button action.
    if (e.getSource() == openButton) {
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);   // <--- I assume instead of (FileChooserDemo.this), I put (null)?  I'm not too sure what you mean.

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //This is where a real application would open the file.
            log.append("Opening: " + file.getName() + "." + newline);
        } else {
            log.append("Open command cancelled by user." + newline);
        }
   } ...
}

And thank you for the welcoming me into the code ranch.  I can see how friendly this particular forum is for beginners.
 
Mike Skyriach
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JFileChooser is still complicated for me to do.  I figured out how to have it read the file like you said, all it needs is a path to find files.  I used this line

File file = new File("/home/mic/IdeaProjects/HelloIntelliJ/src/chap16/SongList.txt");
This line is a success.

Before it was like this:
File file = new File("SongList.txt");
This line, it couldn't find the file.

Thanks for the help
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use the code

...the file must be in the current directory.  This can change depending on how Java is executed.  When I run a program from Eclipse IDE, it uses the projects directory as the current directory.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is where you put null. Congratulations working out the path to your file; what you have is an absolute path which starts with the drive letter (e.g. C:\) on a Windows® box, or a / on a Mac/Unix/Linux box.
 
Mike Skyriach
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:When you use the code

...the file must be in the current directory.  This can change depending on how Java is executed.  When I run a program from Eclipse IDE, it uses the projects directory as the current directory.

Yes, so I was trying to run it in IntelliJ in Ubuntu 16.04.  I came off from using jGrasp for a year and half.  And I'm amazed and what things IntelliJ managed to do that I've missed in a year and half, but I guess, It's a good thing to teach me how to waste my time typing repetitive code only to find out that there's another IDE that will fill repetitive code out for you so fast.  I wonder where it is gonna check in Ubuntu 16.04 without telling it the path file to find the .txt file.   I assume it is gonna look into the openJDK folder?  I'm not sure myself where it is looking for the file in the current directory.

Campbell Ritchie wrote:Yes, that is where you put null. Congratulations working out the path to your file; what you have is an absolute path which starts with the drive letter (e.g. C:\) on a Windows® box, or a / on a Mac/Unix/Linux box.


Thank you.  I'm glad you did not tell me how to do the pathfile, so I had to look it up in the API and just guessed it myself to type "\path--\path--," only to find it is a line too long, so I saw that there was a parent String and a child String, and I put the .txt file in the parent String, and path directory in the child String, and got a error, and switched both, and got it working in a string concenation.  Probably my first time trying to stitch an API little thing on my own without the book's help.  There's so much to learn, really.  I don't know how far I will be able to get on my own.  I guess I should be patient and keep repeating code until I find the pattern in the API and how to stitch them together.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!