
语法:umask [选项] [权限掩码]
参数说明:-S 以字符的方式来表示权限掩码
示例:设置文件的权限掩码,使新建文件自动生成默认权限为rw-rw----
说明:rw-rw----对应的数字就是660 ,在Linux系统中,读权限(read,r)的值是4,写权限(write,w)的值是2,执行权限(execute,x)的值是1,没有授权的值是0 ,所以rw-rw----的数字权限值就是660。因为是建立新的文件,Linux默认不允许用户建立的文件具备可执行权限,所以文件的最大值权限值是666,umask值实际上是计算的最大权限值的补码,所以此时umask值是666-660=006
所以,要依次执行如下命令:
linux@server:~$ umask 006
linux@server:~$ touch abc
linux@server:~$ ll abc
-rw-rw---- 1 linux linux 0 2010-12-11 21:44 abc
扩展:两个比较特殊的权限掩码
umask 777 #创建文件会没有任何权限,他和chmod是相反的。
umask 000 #具有所有权限。但文件没有x权限。
复制粘贴,原网址:http://zhidao.baidu.com/link?url=NaYFAsL_M2L3kGdk6egtg-GT8dTuGM3PesqGktcrybvLUYWr7MXhvuOcdA5P-_77i73u-OsfDaScCI2zTqGe9_
方式一:用弊坦RandomAccessFile类 *** 作租基桐文件RandomAccessFile的open方法,锋芦提供了参数,实现以独占的方式打开文件:
new RandomAccessFile(file, "rws")
其中的“rws”参数中,rw代表读写方式,s代表同步方式,也就是锁。这种方式打开的文件,就是独占方式。
方式二:用文件通道(FileChannel)的锁功能
如:
RandomAccessFile raf = new RandomAccessFile(new File("c:\\test.txt"), "rw")
FileChannel fc = raf.getChannel()
FileLock fl = fc.tryLock()
if (fl.isValid()) {
System.out.println("get the lock!")
java文件追加内容的携灶三种方法:方法一:
public static void writeToTxtByRandomAccessFile(File file, String str){
RandomAccessFile randomAccessFile = null
try {
randomAccessFile = new RandomAccessFile(file,"rw")
long len = randomAccessFile.length()
randomAccessFile.seek(len)
randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n")
} catch (FileNotFoundException e) {
e.printStackTrace()
}catch (IOException e) {
e.printStackTrace()
}finally{
try {
randomAccessFile.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
方法二誉此:辩虚扮
public static void writeToTxtByFileWriter(File file, String content){
BufferedWriter bw = null
try {
FileWriter fw = new FileWriter(file, true)
bw = new BufferedWriter(fw)
bw.write(content)
} catch (IOException e) {
e.printStackTrace()
}finally{
try {
bw.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
方法三:
public static void writeToTxtByOutputStream(File file, String content){
BufferedOutputStream bufferedOutputStream = null
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true))
bufferedOutputStream.write(content.getBytes())
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch(IOException e ){
e.printStackTrace()
}finally{
try {
bufferedOutputStream.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)