[ale] OT: java filereader question
Denny Chambers
dchambers at bugfixer.net
Thu Apr 8 21:32:01 EDT 2004
ahh ok i see
try{
FileReader fr = new FileReader("file1");
}
catch(FileNotFoundException fnfe){
//handle exception
}
J.M. Taylor wrote:
>Sun's 1.4.1_02, and your suggestion is *exactly* what I'm trying. I even
>backed off and just tried to do
>FileReader fr = new FileReader("file1");
>
>and then I took out *all* of my code except where I instantiate the
>FileReader inside the method...still I'm getting:
>unreported exception java.io.FileNotFoundException; must be caught or
>declared to be thrown
> FileReader fr = new FileReader("file1");
> ^
>
>This is the skeleton of the code I'm using...FileReader works fine inside
>of main...there is definitely something major I'm missing here...
>-----
>import java.util.*;
>import java.io.*;
>
>public class mergesort
>{
> public static void main(String[] args) throws java.io.IOException
> {
> /*--snip---*/
> doMergeSort(1,0,100);
> }
>
> public static void doMergeSort(int blsize, int fileset, double filesize)
> {
> if (blocksize < filesize/4)
> {
> FileReader fr = new FileReader("file1");
> }
> }
>}
>
>Thanks very much for looking at this...I'm afraid Java is still more than
>a little bit of black magic to me right now.
>
>jenn
>
>
>
>On Thu, 8 Apr 2004, Denny Chambers wrote:
>
>
>
>>First, what version of java are you using? (java -version)
>>
>>If you using Sun/IBM/Blackdown versions 1.1 or later, then this should work:
>>
>>String filename = (conditional ? "file1" : "file2");
>>FileReader fr = new FileReader(filename);
>>
>>Now file1 and file2 should be a string representation of the relative or absolute path the files in question
>>
>>FileReader has a constructor FileReader(String fileName)
>>
>>
>>Denny
>>
>>
>>
>>J.M. Taylor wrote:
>>
>>
>>
>>>Greetings all,
>>>
>>>I need to evaluate a condition and decide whether to open file1 or file2
>>>based on that condition. Obviously I can do
>>>if (conditional) {
>>> FileReader fr = new FileReader("file1");
>>>}
>>>else {
>>> FileReader fr = new FileReader("file2");
>>>}
>>>
>>>However, with my undisciplined scripting language background I would like
>>>to do something like
>>>
>>>File filename = (conditional ? "file1" : "file2");
>>>FileReader fr = new FileReader(filename);
>>>
>>>I can do the ternary operator if I declare filename as a String, but then
>>>FileReader won't open it (the parameter must be a File object).
>>> new FileReader((File)filename)
>>>doesn't work either.
>>>
>>>Nor does
>>>FileReader fr = (conditional ? new FileReader("file1") : new
>>>FileReader("file2"));
>>>
>>>Nor does
>>>FileReader fr = new FileReader((conditional ? "file1" : "file2"));
>>>
>>>I don't use Java very much, but I like to understand what's going on (and
>>>why I can and can't do things from language to language) as much as
>>>possible. If anybody out there in aleland uses Java and can help me out,
>>>I would greatly appreciate it. I won't be offended if you tell me why I'm
>>>being brain-dead, if that is in fact the case.
>>>
>>>Thanks
>>>jenn
>>>
>>>
>>>
>>>
>>>
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3190 bytes
Desc: S/MIME Cryptographic Signature
More information about the Ale
mailing list