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?
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.
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?
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?
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.
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."); } } }
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.
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