À©µµ¿ìÁî¿¡¼­ µå¶óÀ̺ê À̸§ ´Ù·ç±â.


[ Follow Ups ] [ Post Followup ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]

Posted by ±è´öÅ on August 18, 1997 at 16:36:29:

Joao Mauricio wrote:
> The problem is exactly this. I want to write something like a File
> Manager (just to study). I can't choose the drive letters.
> I think i should choose other kind of program to study. Maybe, I must
> choose another operating system ;-) ).
>
> Thanks all,
> Joao Mauricio


You can finish your work.
The difficulty result from the combination of poor DOS and JDK bug.
Use "C:\." instead of "C:" or "C:\" to workaround JDK bug.
DOS has a current directory for each drives, but that is not supported by JDK.
Therefore, you can adjust the user-supplied file path names to proper forms.
Following example program will help you.



------------------------------- dir.java ----------------------------
import java.io.*;


class dir
{
public static void main (String args[])
throws IOException
{
if ( args.length == 0 )
list( "." );
else if ( args[0].equals("/d")
&& System.getProperty("os.name").startsWith("Windows") )
listDrives();
else
list( args[0] );
}


static void listDrives()
{
for( char drv = 'A'; drv <= 'Z'; ++drv )
// workaround for JDK bug
if ( new File(drv + ":\\.").exists() )
System.out.print( drv + ": " );
System.out.println();
}


static void list( String path ) throws IOException
{
// path adjustment (workaround for poor DOS and JDK bug)
if ( path.length() >= 2 && path.charAt(1) == ':' &&
System.getProperty("os.name").startsWith("Windows") )
{
if ( path.length() == 2 )
path += "\\.";
else if ( path.length() == 3 && path.charAt(2) == '\\' )
path += ".";
else if ( path.length() > 3 && path.charAt(2) != '\\' )
path = path.substring(0, 2) + "\\" + path.substring(2);
}

File f = new File( path );


if ( ! f.exists() )
{ System.out.println( "File not found" );
}
else if ( f.isDirectory() )
{ System.out.println( " Directory of " + f.getCanonicalPath() );
System.out.println();
String[] listing = f.list();
for( int i = 0; i < listing.length; i++ )
System.out.println( listing[i] );
}
else
{ System.out.println( " Directory of " +
new File(f.getParent()).getCanonicalPath() );
System.out.println();
}
}
}


<< Result >>
C:\example\io> java dir /d
C: D: E: F: G:
C:\example\io> java dir
Directory of C:\example\io
...
C:\example\io> java dir c:
Directory of c:\
...
C:\example\io> java dir c:\
Directory of c:\
...
C:\example\io> java dir c:windows
Directory of c:\Windows
...
C:\example\io> java dir c:\windows
Directory of c:\Windows
...


--
Deogtae Kim
CA Lab. CS Dept. KAIST
http://camool.kaist.ac.kr/~dtkim



Follow Ups:



À̾ ±Û¿Ã¸®±â(´äÇϱâ)

À̸§:
E-Mail:
Á¦¸ñ:
³»¿ë:
°ü·Ã URL(¼±ÅÃ):
URL Á¦¸ñ(¼±ÅÃ):
°ü·Ã À̹ÌÁö URL:


[ Follow Ups ] [ Post Followup ] [ ÀÚ¹Ù ¹¯°í ´äÇϱâ ]