Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2007

Tip: Looking for answers? Try searching our database.

[File]Reading a file

Thread view: 
Daniel Moyne - 09 Jan 2007 15:57 GMT
I am puzzled as the prog does not fin the file ; this is the code :

JFileChooser chooser = new
JFileChooser("/media/USER_data/dmoyne_data/GENEALOGIE_data/GenJ_data/GenJ_data/actes/Menotey/tmp");
int FileSelectorIntValue = chooser.showOpenDialog(null);
if(FileSelectorIntValue == JFileChooser.APPROVE_OPTION) {
       String FileNameStringValue=chooser.getSelectedFile().getName();
       println("You chose to open this file: "+FileNameStringValue);
       BufferedReader entree=new BufferedReader(new
FileReader(FileNameStringValue));
       String ligne;
       do {
               ligne=entree.readLine();
               if (ligne != null) {
                       println("ligne :"+ligne);
               }
       } while (ligne != null) ;
       entree.close();
       println("fin lecture fichier");
       }
else {
       println("You have not selected a file : abortion");
       }

Once I have selected my text file with the File Selector I get this error
message :

java.lang.RuntimeException: report failed
       at genj.report.Report.start(Report.java:940)
       at genj.report.ReportView$ActionStart.execute(ReportView.java:450)
       at genj.util.swing.Action2$CallAsyncExecute.run(Action2.java:487)
       at java.lang.Thread.run(Thread.java:595)
Caused by: java.io.FileNotFoundException: batch.txt (No such file or
directory)
       at java.io.FileInputStream.open(Native Method)
       at java.io.FileInputStream.<init>(FileInputStream.java:106)
       at java.io.FileInputStream.<init>(FileInputStream.java:66)
       at java.io.FileReader.<init>(FileReader.java:41)
       at ReportPluginBatchEntry.start(ReportPluginBatchEntry.java:80)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:585)
       at genj.report.Report.start(Report.java:931)
       ... 3 more

Basically that my "batch.txt" file as selected with the File Selector cannot
be found.

Is this a path problem because the FileNameStringValue as returned by the
filer is simply the file name ; if I have to concatenate the path how do I
get it from the File selector to do :
FileNameStringValue=FilePathStringValue+FileNameStringValue;

Regards.
Eric Sosman - 09 Jan 2007 16:30 GMT
Daniel Moyne wrote On 01/09/07 10:57,:
> I am puzzled as the prog does not fin the file ; this is the code :
>
[quoted text clipped - 7 lines]
> FileReader(FileNameStringValue));
> [...]

   Use getPath() or getAbsolutePath() instead of getName().
getName() returns only the name of the file, without any of
the directory specifications:

    File f = new File("/a/b/c/d/foo.txt");
    String s = f.getName();  // yields just "foo.txt"

Signature

Eric.Sosman@sun.com

Eric Sosman - 09 Jan 2007 17:39 GMT
Eric Sosman wrote On 01/09/07 11:30,:
> Daniel Moyne wrote On 01/09/07 10:57,:
>
[quoted text clipped - 16 lines]
>     File f = new File("/a/b/c/d/foo.txt");
>     String s = f.getName();  // yields just "foo.txt"

   There's an even better way that I completely overlooked
(I'm having a stupidity attack today): Instead of fooling
around with strings, just use the File object the chooser
gave you:

    File chosenFile = chooser.getSelectedFile();
    ...
    BufferedReader ... (new FileReader(chosenFile));

Signature

Eric.Sosman@sun.com

Daniel Moyne - 09 Jan 2007 23:33 GMT
> Eric Sosman wrote On 01/09/07 11:30,:
>> Daniel Moyne wrote On 01/09/07 10:57,:
[quoted text clipped - 26 lines]
> ...
> BufferedReader ... (new FileReader(chosenFile));
Thanks Eric,
this was the way to go ; I started from an example where the file was
supposed to be located in th current directory which with a File Selector
has no real use !
Thanks again.
Daniel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.