Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. What is the problem accessing "C" drive using a code (using import.io.*; )?

What is the problem accessing "C" drive using a code (using import.io.*; )?

Scheduled Pinned Locked Moved Java
questionhelpjavaalgorithms
17 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C CoderForEver

    It throws a NullPointerException I think it is because , the application may not have an access on hard drives, but it works if I search in folders which are located in those drives. Can you try it please?

    J Offline
    J Offline
    jschell
    wrote on last edited by
    #8

    CoderForEver wrote:

    It throws a NullPointerException

    The stack trace tells you where. So which line is it?

    CoderForEver wrote:

    the application may not have an access on hard

    Application? You are running exactly the code you posted from a console window correct? Thus the only way it wouldn't have access is if you you, the user, doesn't have permission to access the C drive root. And if that was the case then you should see an exception that indicates exactly that - a permission problem.

    1 Reply Last reply
    0
    • T TorstenH

      ...you know, you need to mask the double-slash ? it's not interpreted right otherwise. the Backslash is in Java a sign for masking and special String-values like the tab "\t". To set a "\" you always need to use 2 "\\". It's a good solution to set up a String and use this as a argument for the following commands:

      FileSearchRecursive x=new FileSearchRecursive();
      String strSearchFolder = "C:\\\\someFolder"; // add extra slashes to make it a double slash
      String strSearchFile = "try.txt"´;
      System.out.println("Searching in folder:\t" + strSearchFolder + " for file:\t" + strSearchFile);
      if(false == (x.searchForFile(strSearchFolder,strSearchFile ))){
      System.out.println("File not found");
      }

      regards Torsten

      I never finish anyth...

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #9

      TorstenH. wrote:

      ...you know, you need to mask the double-slash ?

      And do you know that the OP code doesn't need to do that correct? The code has "C:\\" which is exactly what it should have to access the C drive root.

      TorstenH. wrote:

      String strSearchFolder = "C:\\\\someFolder"; // add extra slashes to make it a double slash

      Which resolves to "C:\\someFolder" which although it will work on windows is not any better or more correct than "C:\someFolder".

      1 Reply Last reply
      0
      • C CoderForEver

        Hi guys, The aim of this code is to find a file by giving starting folder. The problem is, when I use "C:\\" directory it doesn't work, even I tried "C:" and "C" if it was the problem. I think the problem is related to some Access restriction. If I changed the starting folder, lets say, to "C:\\someFolder" it will work perfectly. How can I make the code work so that it will start searching from C drive .. Help me Thank you

        package ex4;

        import java.io.*;

        public class FileSearchRecursive {

        /\*\*
         \* @param args
         \*/
        public static void main(String\[\] args) {
        	FileSearchRecursive x=new FileSearchRecursive();
                    if(!(x.searchForFile("C:\\\\","try.txt")))//works find if I changed the statement to 
                                                            if(!(x.searchForFile("C:\\\\someFolder","try.txt")))
        		System.out.println("File not found");
        
        }	
        	
        boolean searchForFile(String startDir,String fileName)
        {		
        			
        	File file1=new File(startDir);
        	
        	if(file1.exists())
        	{
        	File\[\] list=file1.listFiles();
        		for(File x: list)
        		{
        				
        			 if(x.isFile())
        			{					
        				if(x.getName().equals(fileName))	
        				{
        					System.out.println("I got the file "+x.getName()+" in "+x.getAbsolutePath());
        					return true;
        				}
        			}
        			 else if(x.isDirectory())
        				{
        					if(searchForFile(x.getAbsolutePath(),fileName))
        						return true;
        				}		
        			
        		}
        	}			
        	
        	
        	return false;
        }
        

        }

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #10

        What is the result of running the following? If it is exception then post the entire stack trace.

        File file1=new File("C:\\\\");
        System.out.println("exists=" + file1.exists());
        
        1 Reply Last reply
        0
        • C CoderForEver

          Come on guys. My question is how can I find a file in "C", not in "C:\someFolder"? ... on the latter case it works easily. If you have time try to run my code, it really works. But try to make the searching folder "C:\" not "C:\someFolder". Then it will through an exception. Though thank you for your help

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #11

          Consider these lines:

          	File\[\] list=file1.listFiles();
          		for(File x: list)
          

          if file1.listfiles() does not return any items then list will be null and the runtime will throw an exception. Took me less than 5 minutes to find it with the debugger. Lesson: Check return values and statuses, and catch exceptions: there is no alternative.

          The best things in life are not things.

          1 Reply Last reply
          0
          • C CoderForEver

            Hi guys, The aim of this code is to find a file by giving starting folder. The problem is, when I use "C:\\" directory it doesn't work, even I tried "C:" and "C" if it was the problem. I think the problem is related to some Access restriction. If I changed the starting folder, lets say, to "C:\\someFolder" it will work perfectly. How can I make the code work so that it will start searching from C drive .. Help me Thank you

            package ex4;

            import java.io.*;

            public class FileSearchRecursive {

            /\*\*
             \* @param args
             \*/
            public static void main(String\[\] args) {
            	FileSearchRecursive x=new FileSearchRecursive();
                        if(!(x.searchForFile("C:\\\\","try.txt")))//works find if I changed the statement to 
                                                                if(!(x.searchForFile("C:\\\\someFolder","try.txt")))
            		System.out.println("File not found");
            
            }	
            	
            boolean searchForFile(String startDir,String fileName)
            {		
            			
            	File file1=new File(startDir);
            	
            	if(file1.exists())
            	{
            	File\[\] list=file1.listFiles();
            		for(File x: list)
            		{
            				
            			 if(x.isFile())
            			{					
            				if(x.getName().equals(fileName))	
            				{
            					System.out.println("I got the file "+x.getName()+" in "+x.getAbsolutePath());
            					return true;
            				}
            			}
            			 else if(x.isDirectory())
            				{
            					if(searchForFile(x.getAbsolutePath(),fileName))
            						return true;
            				}		
            			
            		}
            	}			
            	
            	
            	return false;
            }
            

            }

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #12

            This works for me. As someone else has pointed out, file1.listFiles() may return null so you need to allow for that.

            L 1 Reply Last reply
            0
            • D David Skelly

              This works for me. As someone else has pointed out, file1.listFiles() may return null so you need to allow for that.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #13

              I am reduced to "someone else". :((

              The best things in life are not things.

              1 Reply Last reply
              0
              • L Lost User

                Judging by your comments and code extract I would suggest the following line be changed as indicated:

                if(!(x.searchForFile("C:\\someFolder","try.txt")))
                // change to

                if(!(x.searchForFile("C:\\someFolder\\","try.txt")))

                The best things in life are not things.

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #14

                This would show the original problem if C:\\someFolder was empty.


                Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

                L 1 Reply Last reply
                0
                • N Nagy Vilmos

                  This would show the original problem if C:\\someFolder was empty.


                  Panic, Chaos, Destruction. My work here is done. or "Drink. Get drunk. Fall over." - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre Have a bit more patience with newbies. Of course some of them act dumb -- they're often *students*, for heaven's sake. -- (Terry Pratchett, alt.fan.pratchett)

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #15

                  Exactly how I found the problem.

                  The best things in life are not things.

                  D 1 Reply Last reply
                  0
                  • L Lost User

                    Exactly how I found the problem.

                    The best things in life are not things.

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #16

                    Interesting. When I run it on an empty directory, I get a zero-length array. Also, it doesn't make any difference if I put "C:\\someFolder" or "C:\\someFolder\\". It behaves the same in both cases. I only get null back if there is some kind of problem (e.g. Access denied).

                    L 1 Reply Last reply
                    0
                    • D David Skelly

                      Interesting. When I run it on an empty directory, I get a zero-length array. Also, it doesn't make any difference if I put "C:\\someFolder" or "C:\\someFolder\\". It behaves the same in both cases. I only get null back if there is some kind of problem (e.g. Access denied).

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #17

                      I'll try it again sometime and check the results more closely.

                      The best things in life are not things.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups