Re: file reader returning null when file is not null content
jason wrote:
File FileChecker=new File("/Users/Jason/
Desktop/ad_log.txt");
Please use much milder indentation for readability. Four spaces is about the
maximum per indent level for Usenet.
The Java coding convention calls for variables (other than constants) to be
spelled in camel case (a form of mixed case) with the first letter lower case.
I understand you to say that the file already exists, and contains data. (It
doesn't really make sense in Java terms to refer to a file as being 'null';
variables can be 'null'.)
if (!FileChecker.exists()){
FileChecker.createNewFile();
}else{
//build existing records.
//Existing_Records=
getContents(FileChecker);
}
//here i [sic] check if the file exists. if it doesn't i [sic] make a new one.
otherwise i [sic] want to read my old one, using getContents.
//getContents:
static public void getContents(File args) throws Exception{
String[] TempLine;
Scanner freader = new Scanner(args);
//BufferedWriter writer = new BufferedWriter(new
FileWriter(toFile));
//... Loop as long as there are input lines.
String line = null;
while (freader.hasNextLine()) {
line = freader.nextLine();
System.out.println(line);
}
//... Close reader and writer.
freader.close(); // Close to unlock.
// Close to unlock and flush to disk.
There's nothing to "flush to disk" from an input.
}
for some reason that i [sic] cannot figure out this is returning null.
the file is definitely not null and contains very long strings.
the strings contain new line by using:
System.out.println("what i write to my file");
What exactly do you mean by "returning null"? There is no 'return' of any
value in the code that you show.
If you mean to say that you see no visible output, it is possible that
'System.out' is not flushed or there is some other problem with getting the
output visible to you. The business about "returning null" is certainly a red
herring.
This is extremely difficult to answer with the inaccurate terminology and lack
of an SSCCE (Simple, Self-Contained Compilable Example).
<http://sscce.org/>
You should show both the writes to the file and the attempt to read it.
--
Lew