修改文件属性
File类的常用方法
方法名 功能 getName() 获取文件名称,不包括路径信息
length() 获取文件长度,以字节为单位
getPaht() 获取文件的路径信息,包括文件名
toURI() 文件的URI路径,以file为前缀
lastModified() 获取文件最后修改日期,以long值
示
canRead() 文件是否可读 canWrite() 文件是否可写 isHidden() 文件是否隐藏
package com.file.file01;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Date;
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.SwingUtilities;
public class ModifyFileAttribute extends JFrame {
private static final long serialVersionUID = 1L;
//面板容器
private JPanel jContentPane = null;
//北面面板
private JPanel jPanel = null;
//中间面板
private JPanel jPanel1 = null;
//选择文件按钮
private JButton jButton = null;
//文件标签
private JLabel fileLabel = null;
//文件大小标签
private JLabel jLabel = null;
//文件路径标签
private JLabel jLabel1 = null;
//uri标签
private JLabel jLabel2 = null;
//最后修改日期标签
private JLabel jLabel3 = null;
//文件大小文本框
private JTextField sizeField = null;
//文件路径文本框
private JTextField pathField = null;
//uri文本框
private JTextField uriField = null;
//最后修改日期
private JTextField modifyDateField = null;
//可读
private JCheckBox readCheckBox = null;
//可写
private JCheckBox writeCheckBox = null;
//隐藏
private JCheckBox hideCheckBox = null;
//构造方法
public ModifyFileAttribute() {
// super();
initialize();
}
//南面的面板
private JPanel getJPanel() { // //面板不为家的情况下创建
if (jPanel == null) {
//流式布局
FlowLayout flowLayout = new FlowLayout();
//间距
flowLayout.setVgap(0);
//创建面板
jPanel = new JPanel();
//设置面板的布局
jPanel.setLayout(flowLayout);
//添加复选框
//可读
jPanel.add(getReadCheckBox(), null);
//可写
jPanel.add(getWriteCheckBox(), null);
//隐藏
jPanel.add(getHideCheckBox(), null);
}
return jPanel;
}
//中间面板
private JPanel getJPanel1() {
if (jPanel1 == null) {
//GridBagConstraints 类指定使用 GridBagLayout 类布置的组件的约束。
//GridBagLayout 类是一个灵活的布局管理器,它不要求组件的大小相同即可将组件垂直和水平对齐
GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
//当组件的显示区域大于它所请求的显示区域的大小时使用此字段
//HORIZONTAL在水平方向而不是垂直方向上调整组件大小
gridBagConstraints8.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints8.gridy = 4;
gridBagConstraints8.weightx = 1.0;
gridBagConstraints8.insets = new Insets(0, 5, 5, 0);
gridBagConstraints8.gridx = 1;
GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
gridBagConstraints7.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints7.gridy = 3;
gridBagConstraints7.weightx = 1.0;
gridBagConstraints7.insets = new Insets(0, 5, 5, 0);
gridBagConstraints7.gridx = 1;
GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints6.gridy = 2;
gridBagConstraints6.weightx = 1.0;
gridBagConstraints6.insets = new Insets(0, 5, 5, 0);
gridBagConstraints6.gridx = 1;
GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
gridBagConstraints5.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints5.gridy = 1;
gridBagConstraints5.weightx = 1.0;
gridBagConstraints5.insets = new Insets(0, 5, 5, 0);
gridBagConstraints5.gridx = 1;
GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
gridBagConstraints4.gridx = 0;
gridBagConstraints4.gridy = 3;
jLabel3 = new JLabel();
jLabel3.setText("URI路径");
GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
gridBagConstraints3.gridx = 0;
gridBagConstraints3.gridy = 1;
jLabel2 = new JLabel();
jLabel2.setText("文件大小");
GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
gridBagConstraints2.gridx = 0;
gridBagConstraints2.gridy = 4;
jLabel1 = new JLabel();
jLabel1.setText("最后修改日期");
GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
gridBagConstraints11.gridx = 0;
gridBagConstraints11.gridy = 2;
jLabel = new JLabel();
jLabel.setText("文件路径");
GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
gridBagConstraints1.gridx = 1;
gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.insets = new Insets(0, 5, 0, 0);
gridBagConstraints1.gridy = 0;
fileLabel = new JLabel();
fileLabel.setText("文件名");
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.insets = new Insets(0, 5, 0, 0);
gridBagConstraints.gridy = 0;
jPanel1 = new JPanel();
jPanel1.setLayout(new GridBagLayout());
jPanel1.add(getJButton(), gridBagConstraints);
jPanel1.add(fileLabel, gridBagConstraints1);
jPanel1.add(jLabel, gridBagConstraints11);
jPanel1.add(jLabel1, gridBagConstraints2);
jPanel1.add(jLabel2, gridBagConstraints3);
jPanel1.add(jLabel3, gridBagConstraints4);
jPanel1.add(getSizeField(), gridBagConstraints5);
jPanel1.add(getPathField(), gridBagConstraints6);
jPanel1.add(getUriField(), gridBagConstraints7);
jPanel1.add(getModifyDateField(), gridBagConstraints8);
}
return jPanel1;
}
/**
* 选择文件按钮
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("选择文件");
// 添加按钮事件监听器
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 创建文件选择器
JFileChooser chooser = new JFileChooser();
// 显示文件打开对话框
chooser.showOpenDialog(ModifyFileAttribute.this);
File file = chooser.getSelectedFile();// 获取选择文件
fileLabel.setText(file.getName());// 显示文件名称
sizeField.setText(file.length() + "");// 显示文件大小
pathField.setText(file.getPath());// 显示文件路径
pathField.select(0, 0);
uriField.setText(file.toURI() + "");// 显示文件的URI路径
uriField.select(0, 0);
// 显示文件最后修改时间
modifyDateField.setText(new Date(file.lastModified()) + "");
// 显示可读属性
readCheckBox.setSelected(file.canRead());
// 显示可写属性
writeCheckBox.setSelected(file.canWrite());
// 显示隐藏属性
hideCheckBox.setSelected(file.isHidden());
}
});
}
return jButton;
}
private JTextField getSizeField() {
if (sizeField == null) {
sizeField = new JTextField();
}
return sizeField;
}
private JTextField getPathField() {
if (pathField == null) {
pathField = new JTextField();
}
return pathField;
}
private JTextField getUriField() {
if (uriField == null) {
uriField = new JTextField();
}
return uriField;
}
private JTextField getModifyDateField() {
if (modifyDateField == null) {
modifyDateField = new JTextField();
}
return modifyDateField;
}
//这里是定义可读的复选框
private JCheckBox getReadCheckBox() {
//如果为空的情况下我们来创建
if (readCheckBox == null) {
//实例化复选框
readCheckBox = new JCheckBox();
//设置内容为可读
readCheckBox.setText("可读");
}
//返回复选框
return readCheckBox;
}
//可以复选框
private JCheckBox getWriteCheckBox() {
//在不为空的情况下创建
if (writeCheckBox == null) {
//实例化复选框
writeCheckBox = new JCheckBox();
//设置内容为可写
writeCheckBox.setText("可写");
}
//返回复选框
return writeCheckBox;
}
//隐藏复选框
private JCheckBox getHideCheckBox() {
//为空的情情况下我们来创建
if (hideCheckBox == null) {
//实例化复选框
hideCheckBox = new JCheckBox();
//设置复选框的内容
hideCheckBox.setText("隐藏");
}
// /返回复选框
return hideCheckBox;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ModifyFileAttribute thisClass = new ModifyFileAttribute();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);=new 当前的类
}
});
}
private void initialize() {
//文件大小
this.setSize(385, 226);
//不可改变大小
this.setResizable(false);
//添加容器面板
this.setContentPane(getJContentPane());
//标
this.setTitle("读取文件属性");
}
//面板容器
private JPanel getJContentPane() {
//先来判断面板容器是否为空,如果为空那么我们就业定义面板容器
if (jContentPane == null) {
//边界布局
BorderLayout borderLayout = new BorderLayout();
//间距为0
borderLayout.setVgap(0);
borderLayout.setHgap(0);
//创建面板容器
jContentPane = new JPanel();
//面板容器设置布局格式
jContentPane.setLayout(borderLayout);
//容器添加面板放到南
jContentPane.add(getJPanel(), BorderLayout.SOUTH);
//容器添加板放到中间
jContentPane.add(getJPanel1(), BorderLayout.CENTER);
}
//返回容器
return jContentPane;
}
}
这个是我们今天上课时的课件,其实挺简单的,我做的就跟上面不同,但是结果呢,肯定是一样的 这个呢,是我做的,布局吗,有点丑,但是呢,结果还是一样的, 其实首先先做成这样一个面板 做成一个静态的
做成这个样子 首先要要知道需要上面组件 一个按钮 五个标签 四个文本框 还有一个复选框 首先就进行这样的排版 等下 这个出来了 然后在写一个 按钮事件
jb1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//创建文件显示器
JFileChooser chooser=new JFileChooser();
//显示文件打开对话框
chooser.showOpenDialog(null);
//获取文件
File file=chooser.getSelectedFile();
jtf1.setText(file.length()+"");//获取文件大小
jtf2.setText(file.getPath());//获取文件路径
jtf3.setText(file.toURI()+"");//获取URL路径
jtf3.select(0,0);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
String str=sdf.format(file.lastModified());
jtf4.setText(str);
// 显示文件最后修改时间
// 显示可读属性
readCheckBox.setSelected(file.canRead());
// 显示可写属性
writeCheckBox.setSelected(file.canWrite());
// 显示隐藏属性
hideCheckBox.setSelected(file.isHidden());
}
});
其实真的很简单 只是时间不过 我的布局也就那样 但是功能是出来了