Results 1 to 9 of 9
  1. #1
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Question Creating and checking directories with java code

    Hello everyone.

    Can someone please give me an example of code so that I can:
    1. check if an existing directory exists
    2. create a new directory

    Any help would be great.

    Thank you. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    		File file = new File("directory path");
    		if(file.exists()){
    			System.out.println("File Exists");
    		}else{
    			boolean wasDirecotyMade = file.mkdirs();
    			if(wasDirecotyMade)System.out.println("Direcoty Created");
    			else System.out.println("Sorry could not create directory");
    		}
    One more thing .. check java documentation for mkdir() and mkdirs() .. they are different and better if you could find that yourself...
    hope it helps ..
    dont worry newbie, we got you covered.

  3. #3
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Thank you roots. This was what I wanted. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  4. #4
    shish is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi there
    I'm totally new at any Forum. Would you hep me to post a question to the forum?

    With thanx
    Shish

  5. #5
    shish is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi,
    I want to build a java code that can retrieve the Author name of a .doc/.xls file and then it make a folder with the same name of the Author and copies the file into the folder.

    Can anybody give me a suggestion?


    Shish

  6. #6
    shish is offline Member
    Join Date
    Jan 2008
    Posts
    3
    Rep Power
    0

    Default

    Hi there

    Is possible to retreive the Author name from any file (.doc/.ppt/.xls etc.)?
    Is there any java code that can help me to get author of a file?

    Please help me?

  7. #7
    maruthi_s is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    Here you go, I have more file operations but you can do it some more simpler... Just check it out.


    public class TestMaruti implements FilenameFilter {



    int iCount = 0;

    public String str1 = null;

    public void process(String Filter,String FilePath) throws Exception

    {


    str1=Filter;

    File file = new File(FilePath);

    String [] str = file.list(this);

    File file1 = new File(FilePath+"/"+Filter);

    if(!file1.isDirectory())

    {

    System.out.println(iCount++);

    file1.mkdir();

    for(int i=0;i<str.length;i++)

    {

    System.out.println("Writing the file "+str[i]+"....");

    File file2 = new File(FilePath+"/"+str[i]);

    File file3 = new File(FilePath+"/"+Filter+"/"+str[i]);

    moveFiles(file2,file3);

    System.out.println("Completed");

    }

    }


    //file.mkdir();

    }


    public boolean accept(File dir,String s)

    {

    if(s.startsWith(str1))

    {

    return true;

    }

    else

    {

    return false;

    }

    }


    public static void main (String args[]) throws Exception

    {

    String path="D:/Maruti/Test";

    TestMaruti tm = new TestMaruti();

    File allFiles = new File(path);

    String [] strFiles = allFiles.list();

    for(int j=0;j<strFiles.length;j++)

    {

    String flName = strFiles[j];

    flName=flName.substring(0,flName.indexOf("."));

    tm.process(flName,path);

    }

    }


    public void moveFiles(File source,File destination) throws Exception

    {

    InputStream in = new FileInputStream(source);

    OutputStream out = new FileOutputStream(destination);

    byte [] buf = new byte[4096];

    int len;

    while((len=in.read(buf)) > 0 )

    {

    out.write(buf,0,len);

    }

    in.close();

    out.close();

    source.delete();

    }

    }



    Please let me know incase of questions.

  8. #8
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Quote Originally Posted by shish View Post
    Hi there

    Is possible to retreive the Author name from any file (.doc/.ppt/.xls etc.)?
    Is there any java code that can help me to get author of a file?

    Please help me?
    Java Code:
                POIDocument document = new HWPFDocument(new FileInputStream("a.doc"));
                System.out.println(document.getSummaryInformation().getAuthor());
    
                POIDocument workbook = new HSSFWorkbook(new FileInputStream("a.xls"));
                System.out.println(workbook.getSummaryInformation().getAuthor());
    
                POIDocument show = new HSLFSlideShow(new FileInputStream("a.ppt"));
                System.out.println(show.getSummaryInformation().getAuthor());
    HPSF HOW-TO

    It works - please Download Apache POI and put in class path before executing above code.
    dont worry newbie, we got you covered.

  9. #9
    maruthi_s is offline Member
    Join Date
    Jan 2008
    Posts
    20
    Rep Power
    0

    Default

    Hey I thought author is name is part of the file name :)

    POI needs licesnse i guess ... not sure

Similar Threads

  1. JSP getting list of directories
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-31-2008, 01:51 PM
  2. getting paths and directories
    By marco in forum Java Applets
    Replies: 3
    Last Post: 11-25-2007, 08:28 AM
  3. checking if there are equal numbers
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 07:21 AM
  4. java code for checking if a software is installed or not
    By heggemony in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 02:19 PM

Posting Permissions

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