基本信息
源码名称:java将文件设置为(只读文件、存档文件、系统文件、隐藏文件)
源码大小:3.26KB
文件格式:.zip
开发语言:Java
更新时间:2019-07-21
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
设置Windows系统的文件
设置Windows系统的文件
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); } }