Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By kneitzel

Thread: How to read a File correctly?

  1. #1
    gorgon777 is offline Member
    Join Date
    Jan 2014
    Posts
    15
    Rep Power
    0

    Default How to read a File correctly?

    Hi,

    So i am creating a File object which has a text file passed to it. I then try to do logic with it using a BufferedReader. However, I get a FileNotFoundException on the using my code below. The Error is on the BufferedReader line. Is this the wrong approach?
    Java Code:
    System.out.println("--Reading text file--");
    File file = getFile(c,fileName) // Returns a File object.
    System.out.println(file); // Shows me the file is looking correct. Displays contents to console.
    BufferedReader br = new BufferedReader(new FileReader(file));
    System.out.println("BUFFERED");
    while((line = br.readLine()) != null) {
    try {
         // Do Logic
    }
    catch(Exception ex){
         ex.printStackTrace();
    }
    
    br.close();

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: How to read a File correctly?

    FileNotFound is pretty obvious don't you think? Java is not lying to you; the file really isn't there/visible.

    A File object does not have to point to an existing file; check out its many methods, there are a few in there that you can use to check if the file exists, if it is actually a file or a directory, etc. Likely you have a typo in the name, or you are using a relative path while you should be using an absolute path.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    gorgon777 is offline Member
    Join Date
    Jan 2014
    Posts
    15
    Rep Power
    0

    Default Re: How to read a File correctly?

    Quote Originally Posted by gimbal2 View Post
    FileNotFound is pretty obvious don't you think? Java is not lying to you; the file really isn't there/visible.

    A File object does not have to point to an existing file; check out its many methods, there are a few in there that you can use to check if the file exists, if it is actually a file or a directory, etc. Likely you have a typo in the name, or you are using a relative path while you should be using an absolute path.
    The file is definitely there because otherwise, the previous getFile() method would have returned null, and then my System.out.println(file) would not have shown the contents of the text file right? At the moment, getFile() does return a file, and System.out.println(file) does show the text file contents in the console window.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: How to read a File correctly?

    Quote Originally Posted by gorgon777 View Post
    The file is definitely there because otherwise, the previous getFile() method would have returned null, and then my System.out.println(file)
    Makes no sense, System.out.println(File) assuming it is java.io.File does not print the content of the file.

    I can't see what getFile() does since you refuse to post it, but what I can tell you is that Java is not lying to you: the file does -not- exist or is not visible to the Java process.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    gorgon777 is offline Member
    Join Date
    Jan 2014
    Posts
    15
    Rep Power
    0

    Default Re: How to read a File correctly?

    You are totally right, there is something wrong with the file object itself. Apparently you cannot have a file path within the file at the end, as below

    D:\testing or http://hart.flavour.com/mainpage

    The File method goes to name itself as the directory mentioned in either of the above types of paths. When i tried doing file.CanRead() and a file.isFile(), I got a returning boolean of false. Do you know how you could create the file successfully? Here is the getFile() for reference. It retrieves the file from the SharePoint:
    Java Code:
    public File getFile(CopySoap port, String sourceFile) {
       try {
          logger.info("Creating Temporary File: " + sourceFile);
          InputStream in = null;
          Holder<Long> h =  new Holder<Long>();
          Holder<FieldInformationCollection> hf =  new Holder<FieldInformationCollection>();
          Holder<byte[]> d = new Holder<byte[]>();
          port.getItem(sourceFile, h, hf, d);
          if(d.value != null) {
             // replace color with item actual color
             String data = new String(d.value);
             File file =  new File(data);
             logger.info("--FILE CREATED IN MEMORY--");
             logger.info("Retrieved file successfully");
             logger.info("Long holder: " + h.value);
             return file;
          }
          else {
             System.out.println("Document does not exist....");
             logger.debug("Long holder: " + h.value);
             return null;
          }
          }
          catch (Exception ex) {
             System.out.println("-ERROR-");
             System.out.println(ex.toString());
             return null;
          }
    }
    Last edited by gorgon777; 02-13-2014 at 12:38 PM.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,900
    Rep Power
    5

    Default Re: How to read a File correctly?

    I have no idea what you're talking about. The only thing I can say is that you are listing things that do not look like filenames but rather the name of a directory and an URL which will never work for a File object.

    But now you give the getFile() it becomes apparent that this is webservice-related and the File is probably NOT a java.io.File, or it shouldn't be. Why oh why you didn't think that would be interesting to mention, I will never know. I am pretty much done here, this is going to be a long and tiresome thread if you keep this up.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to read a File correctly?

    What is your getFile exactly doing? My guess is, that d is holding the data?

    I would guess that you want your function to receive data and store it inside sourcefile. But that can be said if you simply debug it so you can tell us what is inside data after String data = new String(d.value);

    With kind regards,

    Konrad
    gorgon777 likes this.

  8. #8
    gorgon777 is offline Member
    Join Date
    Jan 2014
    Posts
    15
    Rep Power
    0

    Default Re: How to read a File correctly?

    Quote Originally Posted by kneitzel View Post
    What is your getFile exactly doing? My guess is, that d is holding the data?

    I would guess that you want your function to receive data and store it inside sourcefile. But that can be said if you simply debug it so you can tell us what is inside data after String data = new String(d.value);

    With kind regards,
    Konrad
    Thanks Konrad, your simple pointer let me to find the issue:) 'd.value' was correct, but apparently I was unable to directly assign to a File object from the SharePoint. I have now resolved this by first downloading the file to my hard drive, then using the BufferedReader to read the file, as using the File taken directly from SharePoint let to some permission issue I reckon.

    gimbal2 - Thanks for trying, appreciate that I may not have worded my post 100% clearly. I guess I didn't believe that taking the file from a webservice would cause an issue as long as the file was a valid text file.
    Last edited by gorgon777; 02-13-2014 at 02:41 PM.

  9. #9
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: How to read a File correctly?

    I am glad that I was able to help you solve your issue.

    With kind regards,

    Konrad

Similar Threads

  1. Why won't this write to file correctly
    By zlloyd1 in forum New To Java
    Replies: 1
    Last Post: 01-26-2013, 09:38 AM
  2. ImageIO doesn't correctly read some standard JPG Files
    By francojava1 in forum Advanced Java
    Replies: 2
    Last Post: 10-18-2010, 04:33 PM
  3. How correctly open text file?
    By artemff in forum CLDC and MIDP
    Replies: 3
    Last Post: 12-23-2009, 06:39 PM
  4. Replies: 0
    Last Post: 02-11-2009, 09:53 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •