1

I'm trying to read a .java file with a scanner class, but that obviously doesn't work.

File file = new File("program.java");  
Scanner scanner = new Scanner(file);

I'm just trying to output the code of program.java. Any ideas? Assume that all the files are contained in one folder. Therefore there is no pathway needed.

Mark
  • 117
  • 1
  • 9
  • 1
    What is happening? Are you getting an error? Is the file in the right place? – Corey Ogburn Nov 20 '13 at 21:48
  • It outputs the file name. But I want the output of the code of program.java – Mark Nov 20 '13 at 21:50
  • Why do you have double parentheses around "program.java"? You only need one set, don't think that would fix your issue. – turbo Nov 20 '13 at 21:55
  • A java source file is basically a text file, so please have a look at: [SO: Best way to read a text file](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file) – ljgw Nov 20 '13 at 21:57

3 Answers3

2
  try {
        File file = new File("program.java");
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine())
        System.out.println(scanner.nextLine());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

You have got it right till the scanner object creation. Now all you have to do is check the scanner if it has more lines. If it does, get the next line and print it.

Adarsh
  • 3,413
  • 1
  • 17
  • 35
0

To read the content from a file in java you have to use FileInputStream.
Please see the below code:

File file = new File(("program.java"));  
FileInputStream fis = null;

try {
       fis = new FileInputStream(file);
       int content;
       while ((content = fis.read()) != -1) {
        System.out.print((char) content);
        }
       } catch (IOException e) {
        e.printStackTrace();
        } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

Please check.

Shreyos Adikari
  • 11,413
  • 19
  • 68
  • 76
0

You can use the BufferedReader object to read in your text file:

try {

    BufferedReader file = new BufferedReader(new FileReader("program.java"));
    String line;
    String input = ""; // will be equal to the text content of the file

    while ((line = file.readLine()) != null) 
        input += line + '\n';

    System.out.print(input); // print out the content of the file to the console

} catch (Exception e) {System.out.print("Problem reading the file.");}



Additional points:

You have to use the try-catch when reading in a file.

You could replace Exception (it will catch any error made in your code during run-time) to either:
IOException (will only catch an input-output exception) or
FileNotFoundException (will catch the error if the file is not found).

Or you could combine them, for example:

}
catch(FileNotFoundException e) 
{
    System.out.print("File not found.");
}
catch(IOException f) 
{
    System.out.print("Different input-output exception.");
}
catch(Exception g) 
{
    System.out.print("A totally different problem!");
}
Michael Yaworski
  • 12,398
  • 17
  • 59
  • 91