Question:

Reading external file into Java application.?

by  |  earlier

0 LIKES UnLike

I'm trying to read an external file into my application and below is what I roughly wrote. I checked around and it looks alright to me but the system just couldn't find the file I have allocated.

inputFileName1 is the variable that will hold the file name when the user is asked to input them.

I have a file called hello.txt (used for testing) under C:\ but apparently the system kept on saying IOException error that the file isn't there when it is down there!

File file = new File("C:\\"+inputFileName1);

Scanner fromFile1 = new Scanner(file);

String var1 = fromFile1.next();

System.out.println("This is from the file: "+var1);

Anyone can help to solve this problem? Thanks! :S

 Tags:

   Report

1 ANSWERS


  1. Your code worked for me. I created a hello.txt file and it output the text in the file properly. You are throwing the exception right?

    here's my code that worked:

       import java.util.*;

       import java.io.*;

        public class fileInput {

           public static void main(String[] arg) throws FileNotFoundException

          

           {

          

          String inputFileName1="hello.txt";

          

             File file = new File("C:\\"+inputFileName1);

             Scanner fromFile1 = new Scanner(file);

             String var1 = fromFile1.next();

             System.out.println("This is from the file: "+var1);

          

          

          

          }

       }

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.