java让文件隐藏文件_java将文件设置为(只读文件、存档文件、系统文件、隐藏文件)...

【实例简介】设置Windows系统的文件

【实例截图】

f3c382ee1f2d12209fe903ada47ef6a0.png

【核心代码】

package com.mingrisoft;

import java.awt.EventQueue;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.IOException;

import java.util.Scanner;

import javax.swing.JButton;

import javax.swing.JCheckBox;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.border.EmptyBorder;

public class FileAttributeModification extends JFrame {

/**

*

*/

private static final long serialVersionUID = -5028582953468410969L;

private JPanel contentPane;

private JTextField textField;

private File selectFile;

private JCheckBox readCheckBox;

private JCheckBox archiveCheckBox;

private JCheckBox systemCheckBox;

private JCheckBox hiddenCheckBox;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

FileAttributeModification frame = new FileAttributeModification();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

}

/**

* Create the frame.

*/

public FileAttributeModification() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setBounds(100, 100, 400, 160);

contentPane = new JPanel();

contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

setContentPane(contentPane);

contentPane.setLayout(new GridLayout(3, 1, 5, 5));

JPanel filePanel = new JPanel();

contentPane.add(filePanel);

JLabel label = new JLabel("文件路径:");

filePanel.add(label);

textField = new JTextField();

textField.setEditable(false);

filePanel.add(textField);

textField.setColumns(15);

JButton chooseButton = new JButton("选择文件");

chooseButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

do_chooseButton_actionPerformed(e);

}

});

filePanel.add(chooseButton);

JPanel attributePanel = new JPanel();

contentPane.add(attributePanel);

readCheckBox = new JCheckBox("只读");

attributePanel.add(readCheckBox);

archiveCheckBox = new JCheckBox("存档");

attributePanel.add(archiveCheckBox);

systemCheckBox = new JCheckBox("系统");

attributePanel.add(systemCheckBox);

hiddenCheckBox = new JCheckBox("隐藏");

attributePanel.add(hiddenCheckBox);

JPanel buttonPanel = new JPanel();

contentPane.add(buttonPanel);

JButton setButton = new JButton("设置");

setButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

do_setButton_actionPerformed(e);

}

});

buttonPanel.add(setButton);

JButton closeButton = new JButton("关闭");

closeButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

do_closeButton_actionPerformed(e);

}

});

buttonPanel.add(closeButton);

}

protected void do_chooseButton_actionPerformed(ActionEvent e) {

JFileChooser chooser = new JFileChooser();

chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);

chooser.setMultiSelectionEnabled(false);

int result = chooser.showOpenDialog(this);

if (result == JFileChooser.APPROVE_OPTION) {

selectFile = chooser.getSelectedFile();

String path = selectFile.getAbsolutePath();

textField.setText(path);

String command = "attrib " path;

try {

Process process = Runtime.getRuntime().exec(command);

Scanner scanner = new Scanner(process.getInputStream());

while (scanner.hasNextLine()) {

String line = scanner.nextLine();

int pathIndex = line.indexOf(path);

String attribute = line.substring(0, pathIndex).trim();

readCheckBox.setSelected(attribute.contains("R"));

archiveCheckBox.setSelected(attribute.contains("A"));

systemCheckBox.setSelected(attribute.contains("S"));

hiddenCheckBox.setSelected(attribute.contains("H"));

}

} catch (IOException e1) {

e1.printStackTrace();

}

}

}

protected void do_setButton_actionPerformed(ActionEvent e) {

StringBuilder command = new StringBuilder("attrib ");// 创建命令

if (readCheckBox.isSelected()) {// 如果需要增加只读属性

command.append(" R ");// 在命令行增加参数

}

if (archiveCheckBox.isSelected()) {// 如果需要增加存档属性

command.append(" A ");// 在命令行增加参数

}

if (systemCheckBox.isSelected()) {// 如果需要增加系统属性

command.append(" S ");// 在命令行增加参数

}

if (hiddenCheckBox.isSelected()) {// 如果需要增加隐藏属性

command.append(" H ");// 在命令行增加参数

}

command.append(selectFile.getAbsolutePath());// 增加文件路径

try {

Runtime.getRuntime().exec(command.toString());// 执行命令

} catch (IOException e1) {

e1.printStackTrace();

}

}

protected void do_closeButton_actionPerformed(ActionEvent e) {

System.exit(0);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值