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 7 of 7

Thread: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.
    I tried to use FileReader and BufferedReader objects, but the code doesn't work.

    How do I get my program to read .txt files in java?


  2. #2
    Member
    Join Date
    Dec 2013
    Posts
    40
    My Mood
    Amazed
    Thanks
    2
    Thanked 11 Times in 11 Posts

    Default Re: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    I suggest you employ Java's File and Scanner class. They are pretty easy to use for beginners.
    Who holds the KEY to all knowledge?

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    Show your code and any error messages or undesirable results received. There are more ways to get it wrong than to get it right, so it's important to see what you've done.

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

    Default Re: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    package functions;
    import java.io.File;
    import button.Actions;
    public class handling 
    {
    	public static void main (String[] args)
    	{
    		File dir = new File("data.txt");
    		if(dir.exists())
    		{
    			String[] files = dir.list();
    			System.out.println(files.length + " files found...");
    			for (int i = 0; i <files.length; i++)
    			{
    				System.out.println(files[i]);
    			}
    		}
    		else
    		{
    			System.out.println("Folder not found.");
    		}
    	}
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    Can you explain what the problem is for the posted code?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    The problem is that it prints: Folder not found.
    There is no error, but that is NOT the intended output.

  7. #7
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: How do I get my program to read .txt files? My code doesn't work. Can you please post your code? I use eclipse.

    Quote Originally Posted by alextrimplez View Post
    The problem is that it prints: Folder not found.
    There is no error, but that is NOT the intended output.
    Seems pretty straightforward. File.exists() is returning false because the "data.txt" file or directory does not exist.

    In English, as written, this program:

    1. Accesses the file or directory "data.txt", which will be found relative the "current working directory" of the JVM process.
    2. If the file or directory exists, it attempts to treat it like a directory, fetch the list of files within this directory, and print them out to the console.

    It is branching at the first step, because the file or directory "data.txt" cannot be found relative to the JVM current working directory.

    Ask yourself what the program is intended to do, and where on the filesystem it is expected to do it.

    Hint: there are methods to prove to yourself where the current working directory for an Eclipse run program happens to be, if you don't want to use absolute pathnames.
    Last edited by jdv; August 5th, 2014 at 11:21 AM. Reason: Reworded

Similar Threads

  1. Code doesn't work
    By Nicken99 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2014, 04:24 AM
  2. Code doesn't work despite being from book
    By Math2 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 16th, 2013, 08:24 PM
  3. How can I read txt files in java program??
    By sakura_smile in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 9th, 2012, 06:18 PM
  4. actionListner code doesn't work ????
    By sciences in forum AWT / Java Swing
    Replies: 4
    Last Post: February 27th, 2012, 06:46 AM
  5. Code is working in Eclipse but when exported to Runnable Jar doesn't work
    By jjain.jitendra@gmail.com in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 24th, 2011, 07:12 AM