Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: chmod
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: chmod



Thank you for your reply.

> It worked for me (with the escaped characters).  Are you sure
> it's not because of the '/' at the beginning of your filename?
> Is your file really in the '/' directory?
Yes, the file really exists on the path.

> Does chmod work with this file when invoked from a Terminal window? Runtime.exec is a bit different, in that the input is not run through a shell (Terminal uses the bash shell by default). If it works from Terminal, try exec-ing a shell command, e.g.
>
>     new String[] {
>         "/bin/csh", "-c", "chmod g+w '/xxxxx.txt'"
>     }
I tried the code but it didn't work too.

I finally found the code which works well:

public static boolean chmod (File path,String auth,boolean recursive)
{
	boolean check = false;

	Process process = null;
	BufferedOutputStream bos = null;
	try
	{
		process = Runtime.getRuntime ().exec (new String [] {"/bin/sh","-s"});

		bos = new BufferedOutputStream (process.getOutputStream ());
		bos.write (("chmod " + (recursive ? "-R " : "") + auth + " \"" + path.getCanonicalPath () + "\"\n").getBytes ("UTF-8"));
		bos.flush ();
		bos.close ();

		process.waitFor ();
		check = process.exitValue () == 0;
		process.destroy ();
	}
	catch (InterruptedException ie)
	{
		check = false;
		ie.printStackTrace ();
	}
	catch (IOException ioe)
	{
		check = false;
		ioe.printStackTrace ();
	}
	finally
	{
		if (process != null) process.destroy ();
		try {if (bos != null) bos.close ();} catch (IOException ioe) {ioe.printStackTrace ();}
	}

	return check;
}

Thank you.
>

=======================================================================
株式会社ベビーユニバース 開発部 中山 太雅

■PDF小説「5年後のベビーユニバース」下記よりダウンロード!
http://www.baby-universe.co.jp/company/dl.php?file=img/after5yearbu.pdf

・本社
〒251-0025 神奈川県藤沢市鵠沼石上1-13-13-304
Tel : 0466-55-3000  Fax : 0466-55-3001

・IV : イノベーションビレッジ
〒252-0816 神奈川県藤沢市遠藤4489番 212号室
Cel : 090-2422-1981(212号室内設置携帯)
=======================================================================

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Re: chmod (From: Kevin Ryan <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.