Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Can't Detect .txt File

    I'm working on the advanced I/O chapter in my Java Book, and I want to read from a .txt file, but no matter what I do, Netbeans just doesn't seem to realize I put it there:

    Can't Detect .txt File-txtfiledoesnotexist.png

    Here is the code of the .java file I'm trying to make read the .txt file:

    Java Code:
    package chapter10;
    import java.io.*;   // For File class and FileNotFoundException
    import java.util.*; // For Scanner and InputMismatchException
    import java.text.DecimalFormat;  // For the DecimalFormat class
    
    /**
     *  This program demonstrates how multiple exceptions can
     *  be caught with one try statement.
     */
    
    public class SalesReport
    {
       public static void main(String[] args)
       {
          String filename = "SalesData.txt"; // File name
          int months = 0;                    // Month counter
          double oneMonth;                   // One month's sales
          double totalSales = 0.0;           // Total sales
          double averageSales;               // Average sales
    
          // Create a DecimalFormat object to format output.
          DecimalFormat dollar =
                       new DecimalFormat("#,##0.00");
    
          try
          {
             // Open the file.
             File file = new File(filename);
             Scanner inputFile = new Scanner(file);
             
             // Process the contents of the file.
             while (inputFile.hasNext())
             {
                // Get a month's sales amount.
                oneMonth = inputFile.nextDouble();
                
                // Accumulate the amount.
                totalSales += oneMonth;
                
                // Increment the month counter
                months++;
             }
    
             // Close the file.
             inputFile.close();
    
             // Calculate the average.
             averageSales = totalSales / months;
    
             // Display the results.
             System.out.println("Number of months: " + months);
             System.out.println("Total Sales: $" +
                                dollar.format(totalSales));
             System.out.println("Average Sales: $" +
                                dollar.format(averageSales));
          }
          catch(FileNotFoundException e)
          {
             // The file was not found.
             System.out.println("The file " + filename +
                                " does not exist.");
          }
          catch(InputMismatchException e)
          {
             // Thrown by the Scanner class's nextDouble
             // method when a nonnumeric value is found.
             System.out.println("Nonnumeric data " +
                                "found in the file:" +
                                e.getMessage());
          }
       }
    }

    What does a guy have to do to make Netbeans detect a .txt file?!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Can't Detect .txt File

    Please don't post graphical images of your IDE. In most cases it doesn't really help. Just tell us it can't find the file.

    You need to determine what the default directory is that you program is executing and where the file actually is.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Can't Detect .txt File

    Quote Originally Posted by jim829 View Post

    You need to determine what the default directory is that you program is executing and where the file actually is.

    Regards,
    Jim
    But you saw in my forbidden graphical image. The .txt file is in the "chapter10" package. I also checked my project's resources folder on my computer, and it's in there too. So why would it not be detected? In other words, Jim, what steps do YOU take when YOU'RE you're adding a .txt file to one of YOUR java projects?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Can't Detect .txt File

    You could simply check if the file exists() method to see if the file is there. Although you can see the file with your eyes, but a computer cannot locate the file if you didn't supply the correct path. When you only supply the name of the file it will try to look from the location where the program is executed. If it is under the chapter10 package then you have to tell the computer to look for the file inside the chapter10 directory.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,848
    Rep Power
    5

    Default Re: Can't Detect .txt File

    Quote Originally Posted by SamJava_the_Hut View Post
    In other words, Jim, what steps do YOU take when YOU'RE you're adding a .txt file to one of YOUR java projects?
    Well I learned how it actually works so I don't have to make wrong assumptions.

    Your file is in the chapter 10 package. Yet you are trying to open the file from the local file system, not from the application classpath. So Java will do what you instruct it to do - look for the file in the directory where Java is run. That is not in the chapter10 package directory, but likely the bin or target directory that your IDE has configured to output the compiled classes to.

    So now you know you have to load the file from the classpath. I am not going to spill out how you do that, try to research it yourself first because this is a good moment to get familiar with doing that. I'll give you a hint that makes it easier: you want to know how to load resources from the classpath, not files.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Join Date
    Jul 2013
    Location
    Wisconsin, USA
    Posts
    60
    Rep Power
    0

    Default Re: Can't Detect .txt File

    Quote Originally Posted by gimbal2 View Post
    That is not in the chapter10 package directory, but likely the bin or target directory that your IDE has configured to output the compiled classes to. So now you know you have to load the file from the classpath.
    I placed the text file in the "Chapter10" project folder (with the folders src, nbproject, and build; and the build and manifest files), and it worked! So it's safe to say that the classpath means the inside of the main project folder, right?

Similar Threads

  1. Detect change to file(s) using getFileSystemView
    By Bartman in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2013, 08:36 PM
  2. InputStream is unable to detect file
    By HardRock in forum Eclipse
    Replies: 2
    Last Post: 06-24-2012, 11:38 AM
  3. Easiest way to detect beeps in audio file
    By bill17 in forum New To Java
    Replies: 0
    Last Post: 11-26-2011, 04:46 PM
  4. How can I detect the end of a line in a file ?
    By ze snow in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 10:57 AM
  5. Detect new file entry
    By rdemon in forum Advanced Java
    Replies: 4
    Last Post: 01-08-2010, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •