Reputation: 2145
I'm using this code to read a file in java using the filereader object. However the application throws an exception stating that it is not able to find the file.Can anyone help this novice guy just into java programming
import java.util.*;
import java.io.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.PrintWriter;
public class CFileReader {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException
{
String objLine;
String strInputFileName;
FileReader objReader = null;
Scanner objScanner;
File objFile;
if(args.length > 0)
{
for(int i = 0;i < args.length ;i++)
{
//Gets the arguments into the strInputFileName variable
strInputFileName = args[i];
System.out.println("Filename entered was : " + strInputFileName);
//Create a file object which points to the filename i.e strInputFileName
objFile = new File(strInputFileName);
//Create a FileReader object with the File object i.e objFile as the input parameter
objReader = new FileReader(objFile);
System.out.println("Filereader object is : " + objReader.toString());
//Create a scanner object with FileReader as input parameter
objScanner = new Scanner(objReader);
System.out.println(args);
//Scans the file if it has next line
while(objScanner.hasNextLine())
{
//Store the contents i.e. first line in the objLine Variable
objLine = objScanner.nextLine();
//prints the contents
if(objLine.indexOf(i) > 0)
{
System.out.println(objLine);
}
}
}
}
else
{
//Warn the user to enter the command line arguments if he has not entered
System.out.println("Please Enter Command line args");
}
}
}
To compile the program i use
javac CFileReader.java
and to run
java CFileReader "C:\\Hello.txt"
where Hello.txt is a simple text file with some contents
Thanks a lot in advance
Upvotes: 0
Views: 852
Reputation: 934
I generally put in the following lines between my File object and its use:
if (!objFile.isFile() || !objFile.exists()) {
System.out.println("Could not find file: " + objFile.getPath().toString();
}
this will often show a problem in the string format of the file name.
Upvotes: 0
Reputation: 12344
Check that your input file actually exists. When I ran your code on my machine, I didn't have permission to create a file in the root of C:. How did you create your test file?
When I moved the file to another drive, e.g. "t:\hello.txt" it worked the first time.
Upvotes: 0
Reputation:
I compiled and tested this code myself on Windows, and it works fine. Either the file you are after does not exist, or you are making a mistake when you type in the name of the file on the command line.
Upvotes: 0
Reputation: 25686
You don't need the \\
. Java doesn't parse escape sequences in input strings, and the single backslash is a path separator in the Windows shell.
Upvotes: 1
Reputation: 547
That code works in my MacOS system, The problem is the way in what your write the path of you file in Windows. In my case:
java CFileReader /home/jenaiz/test.txt
Upvotes: 0
Reputation: 21409
In addition to the args[0] error pointed out by @Adel, you also don't have to escape the "\" in the command line... "C:\Hello.txt" is sufficient.
Upvotes: 2