0

I am trying to read from a text file. I have the file created, it only needs to have one record, but it could have more. I keep getting errors. I am a Geography student, not an IT guy, I am hoping to figure out the next step once I get this. Here is my example, that doesn't work:

import java.io.*;
import java.util.*;
import java.lang.*;
public class Driver
{
       public static void main(String[] args) throws IOException
       {
            File data;
            String fileName = null; // User input file name

            Scanner input;
            input = new Scanner(System.in); 

            System.out.println("Enter file name (ie: text.txt): ");
            data = new File(input.next());

            Scanner read;
            read = new Scanner(data);

            fileName = read.nextLine();

            System.out.println(fileName);
        }
 }
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
PhilipG
  • 9
  • 1
  • 1
    What error do you get? [I got none](http://melpon.org/wandbox/permlink/OWVpKLf6enn6osN5). – MikeCAT Mar 09 '16 at 02:19
  • Possible duplicate of [Reading a plain text file in Java](http://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java) – Kent Cooper Mar 09 '16 at 02:22
  • 1
    `I keep getting errors` What errors? Please post the errors that you're having. Thank you. – Pang Mar 09 '16 at 02:25
  • java.io.FileNotFoundException: text.txt (The system cannot find the file specified.)(in java.io.FileInputStream) – PhilipG Mar 09 '16 at 02:46
  • I don't get errors in compiling, but after I enter the file name, which I know exists. I have a copy of it in the java program folder and elsewhere on my c:\ drive, it doesn't find it in either place. – PhilipG Mar 09 '16 at 02:48
  • ...and Kent Cooper, I didn't find the issue I am having. There are features my instructor is not allowing us to use at this point, I know there is probably a very simple solution, but I have to use the what I have. – PhilipG Mar 09 '16 at 02:52
  • Put the text file where you have class files, and it will run.. – ELITE Mar 09 '16 at 02:56

2 Answers2

0

I believe an error that you are having is not referencing the correct place when trying to access the file. If you just type in example.txt, the java compiler has no idea where to find this file. Try this:

  • Right click under the package
  • Create new folder, and call it 'texts'
  • Open file explorer
  • Paste all your .txt files into this new folder 'texts'
  • Replace fileName = read.nextLine(); with fileName = "texts/" + read.nextLine();

After this you should be good to go!

CrazedCoder
  • 294
  • 3
  • 14
  • Compiler will find the file where the class files lies or you can check current user directory where it searches using `System.getProperty("user.dir");` method. – ELITE Mar 09 '16 at 03:06
  • ELITE - Unfortunately, the instructor wouldn't allow me to do it that way. – PhilipG Mar 09 '16 at 03:27
  • CrazyCoder - I tried that and it didn't work, still get the same errors. – PhilipG Mar 09 '16 at 03:30
  • PhilipG You should try posting your code, and snips of your txt files, and workspace for more helpful answers – CrazedCoder Mar 10 '16 at 22:26
0

You have two options.

  • Put text.txt file in same folder where your class files are.

  • Get full path of the file from user like C:\text.txt.

It'll run.

ELITE
  • 5,815
  • 3
  • 19
  • 29