I have a problem opening a file in linux, however, in windows XP doesn´t ocurrs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isaacrc82
    New Member
    • Nov 2008
    • 6

    I have a problem opening a file in linux, however, in windows XP doesn´t ocurrs

    Hi every body:

    An exception is thrown when I open some files in linux, this doesn't occurrs when I do the same in windows XP.
    This is the pierce of code that throws the exception:
    Code:
        42   public static byte[] readFileBytes(File file) throws IOException {
        43   java.io.InputStream istr = new java.io.FileInputStream(file);
        44   byte bytes[] = new byte[istr.available()];
        45   istr.read(bytes);
        46   istr.close();
        47   return bytes;
        48   }
    I received this exception:
    Exception in thread "main" java.io.IOExcep tion: No such file or directory
    at java.io.FileInp utStream.readBy tes(Native Method)
    at java.io.FileInp utStream.read(F ileInputStream. java:177)
    at ca.co.coding.Ma in.readFileByte s(Main.java:45)

    In windows I used the same file and the code is executed succesfully.
    Could somebody tell me why is this happening ???
    Thanks in advance
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Is the file path in each case correct? Remember, Windows uses \ as its separator (don't forget to escape it!) and Linux/Unix use /.

    Comment

    • isaacrc82
      New Member
      • Nov 2008
      • 6

      #3
      Yes the file path is correct, for instance this file path /state/Artificial Intelligence/Articles/mydocument.pdf
      I think this is not the problem, I don't understand why in windows works fine but in linux fail with the exception I posted above.

      I hope you can help me.
      Greetings
      Ariel

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by isaacrc82
        Yes the file path is correct, for instance this file path /state/Artificial Intelligence/Articles/mydocument.pdf. ..
        You might have to escape the space in Linux, so it would be
        /state/Artificial\\ Intelligence/Articles/mydocument.pdf
        But to test this, I'd recommend to use a [code=java]System.out.prin tln(file.getAbs olutePath() + (file.exists ? " exists" : " doesn't exist"));[/code] and check, what it says.

        Greetings,
        Nepomuk

        Comment

        • isaacrc82
          New Member
          • Nov 2008
          • 6

          #5
          I realized the problem is the name of the file, The files that have some not alpha-numerics characters like: ´ ' " are the files that throws the exception for example \state\Document s\the production of ´the ship´.pdf
          Then my question is How could I configure the java virtual machine in order to make it unicode supportable ???
          I have tried setting the system property to unicode
          Code:
          System.setProperty("file.encoding", "UTF-16");
          But the error still remains.

          Could you help me please ?
          Greetings
          Ariel

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Here, this code works fine with a file called testfile "one".txt" under Linux:
            [code=java]
            import java.io.File;

            public class FilenameTester {
            public static void main(String[] args) {
            File testfile = new File("testfile \"one\".txt" );
            System.out.prin tln(testfile.ge tAbsolutePath() +
            (testfile.exist s() ? " exists" : " doesn't exist"));
            }
            }
            [/code]

            Comment

            • isaacrc82
              New Member
              • Nov 2008
              • 6

              #7
              Yes but I am doing this for a directory

              This is my pierce of code:
              Code:
              	
                              File file = new File(directory);
              		File[] files = file.listFiles();
              		for (int i = 0; i < files.length; i++) {
              			System.out.println(file.getAbsolutePath() + (file.exists() ? " exists" : " doesn't exist"));
              		        java.io.InputStream istr = new java.io.FileInputStream(file);
              	                byte bytes[] = new byte[istr.available()];
              	                System.out.println(bytes.length);	        
              	                istr.read(bytes);
              	                istr.close();		
              		}
              Do I need then escape the characters of the name and reopen de file ???
              How can I do this, how do I escape the characters of the filename programatically ???

              Thanks in advances.
              Greetings
              Ariel

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                What surprises me a bit is that the FileInputStream succeeds while the actual
                reading of the file fails (according to the Exception stack trace). What happens
                when you manually rename that file?

                kind regards,

                Jos

                Comment

                • isaacrc82
                  New Member
                  • Nov 2008
                  • 6

                  #9
                  When I rename the file manually it works fine and the bytes are read sucessfully. For example I rename the file "It doesn't matters.pdf" to "it doesn t matter.pdf" and it works perfectly.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by isaacrc82
                    When I rename the file manually it works fine and the bytes are read sucessfully. For example I rename the file "It doesn't matters.pdf" to "it doesn t matter.pdf" and it works perfectly.
                    I'm really surprised because that means that a FileInputStream constructor would
                    succeed on a file that can not be read because of its name ... strange ...

                    kind regards,

                    Jos

                    Comment

                    • isaacrc82
                      New Member
                      • Nov 2008
                      • 6

                      #11
                      How can I solve that problem ???
                      Some body knows ???

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by isaacrc82
                        How can I solve that problem ???
                        Some body knows ???
                        Start by renaming that file again and putting in the offending character(s) in the
                        file name; see what happens. If it really doesn't make sense file a bug at Sun's
                        bugbase after checking that no such bug has been registered already.

                        A FileInputStream shouldn't successfully be constructed while you can't read
                        from that InputStream for some vague reason ...

                        kind regards,

                        Jos

                        Comment

                        Working...