通过java代码更改文件权限

通过java代码更改文件权限。

第一种如下,通过ant进行

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
	public static void write(File path, File zipFile) throws IOException {
		ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFile));
		zip.setEncoding("GBK");
		Util.write(path, path, zip);
		zip.close();
	}

	private static void write(File base, File path, ZipOutputStream zip) throws IOException {
		URI rel = base.toURI().relativize(path.toURI());
		if (path.isDirectory()) {
			ZipEntry entry = new ZipEntry(rel.getPath());
			entry.setUnixMode(755);
			zip.putNextEntry(entry);
			zip.closeEntry();
			File[] files = path.listFiles();
			for (File file : files) {
				write(base, file, zip);
			}
		} else {
			ZipEntry entry = new ZipEntry(rel.getPath());
			entry.setUnixMode(644);
			zip.putNextEntry(entry);
			FileInputStream is = new FileInputStream(path);
			zip.write(IOUtils.toByteArray(is));
			is.close();
			zip.closeEntry();
		}
	}

第二种 通过执行linux命令操作

chmod 755 yourfile

private void execShell(String scriptPath, String... para) {
        try {

            String[] cmd = new String[]{scriptPath};

            //为了解决参数中包含空格
            cmd = ArrayUtils.addAll(cmd, para);

            //解决脚本没有执行权限
            ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "755", scriptPath);
            Process process = builder.start();
            process.waitFor();
            Process ps = Runtime.getRuntime().exec(cmd);
            ps.waitFor();
            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line).append("\n");
            }
            //执行结果
            String result = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

第三种 通过 PosixFilePermission 进行操作

public static void chmod755(File file ) throws IllegalStateException {

        //设置权限
        Set perms = new HashSet();
        perms.add(PosixFilePermission.OWNER_READ); //设置所有者的读取权限
        perms.add(PosixFilePermission.OWNER_WRITE); //设置所有者的写权限
        perms.add(PosixFilePermission.OWNER_EXECUTE); //设置所有者的执行权限
        perms.add(PosixFilePermission.GROUP_READ); //设置组的读取权限
        perms.add(PosixFilePermission.GROUP_EXECUTE); //设置组的读取权限
        perms.add(PosixFilePermission.OTHERS_READ); //设置其他的读取权限
        perms.add(PosixFilePermission.OTHERS_EXECUTE); //设置其他的读取权限
        try {
            //设置文件和文件夹的权限
            Path pathParent = Paths.get(file.getParentFile().getAbsolutePath());
            Path pathDest = Paths.get(file.getAbsolutePath());
            Files.setPosixFilePermissions(pathParent, perms);   //修改文件夹路径的权限
            Files.setPosixFilePermissions(pathDest, perms);  //修改图片文件的权限
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

第四种 直接通过file进行操作

File file = new File(path)

file.setExecutable(true);
file.setReadable(true);
file.setWritable(true);

 

你可能感兴趣的:(java,java,linux,文件,权限)