Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: My program does not find the file MyFriends.txt

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default My program does not find the file MyFriends.txt

    import java.util.Scanner;   // Needed for Scanner class
    import java.io.*;           // Needed for File class
     
    /**
       This program reads the first line from a file.
    */
     
    public class ReadFirstLine
    {
       public static void main(String[] args) throws IOException
       {
          // Create a Scanner object for keyboard input.
          Scanner keyboard = new Scanner(System.in);
     
          // Get the file name.
          System.out.print("Enter the name of a file: ");
          String filename = keyboard.nextLine();
     
          // Open the file.
          File file = new File(filename);
          Scanner inputFile = new Scanner(file);
     
          // Read the first line from the file.
          String line = inputFile.nextLine();
     
          // Display the line.
          System.out.println("The first line in the file is:");
          System.out.println(line);
     
          // Close the file.
          inputFile.close();
       }
    }

    Enter the name of a file: MyFriends.txt
    Exception in thread "main" java.io.FileNotFoundException: MyFriends.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at ReadFirstLine.main(ReadFirstLine.java:21)
    Java Result: 1


  2. #2
    Junior Member
    Join Date
    Oct 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program does not find the file MyFriends.txt

    Hi,

    The problem is that it doesnt know where to search the file from. Try the following.

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.Scanner;

    public class ReadFirstLine
    {
    public static void main(String[] args) throws IOException
    {
    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    // Get the file name.
    System.out.print("Enter the name of a file: ");
    String filename = keyboard.nextLine();
    //System.out.print(filename);
    // Open the file.
    File file = new File(filename);
    String folder = "D:\\CP\\Test\\";
    //System.out.println(folder + file.getPath());
    String filepath = folder + file.getPath();
    Scanner inputFile = new Scanner(new BufferedReader(new FileReader(filepath)));

    // Read the first line from the file.
    String line = inputFile.nextLine();

    // Display the line.
    System.out.println("The first line in the file is:");
    System.out.println(line);

    // Close the file.
    inputFile.close();
    }
    }

    Enjoy

Similar Threads

  1. Fixing a program that reads a .txt file java beginner I need help ASAP
    By coding2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 12th, 2014, 04:45 PM
  2. Program to find the number of occurrences of words in the text file
    By Dinesh Raja in forum What's Wrong With My Code?
    Replies: 56
    Last Post: January 10th, 2014, 11:03 AM
  3. My program can't find file, even it being there
    By coltson in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 13th, 2013, 01:36 AM
  4. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  5. Program to read .TXT file one line at a time
    By JustForLawls in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2012, 02:02 PM