java 获取、修改文件属性

public class AttributeDemo {
    // jdk6读取文件属性
    public static void getAttribute(String path) {
	File file = new File(path);
	System.out.println("最后一次更改日期:"
		+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(
			file.lastModified())));
	System.out.printf("文件大小:%.3fKB", file.length() / 1024.0);// 字节
	// System.out.printf("\n文件大小:%d字节", file.length());//字节
    }

    // jdk7读取文件属性方式1:
    public static void geFileInfo(String srcFile) throws IOException {
	Path path = Paths.get(srcFile);
	System.out.println(Files.isDirectory(path));
	System.out.println(Files.isExecutable(path));// 是否是可执行文件 true
	System.out.println(Files.isHidden(path));
	System.out.println(Files.isReadable(path));
	System.out.println(Files.isWritable(path));
	System.out.println(Files.isRegularFile(path));// 是否是常规文件
	System.out.println(Files.isSymbolicLink(path));// 是否是符号链接
	System.out.println(Files.size(path));// 
	System.out.println("默认获取的是中心时区(0)的时间:"+ Files.getLastModifiedTime(path));// 2016-01-12T05:20:44.466277Z
	//格式化时间
	Date date = new Date(Files.getLastModifiedTime(path).toMillis());
	String time = DateUtil.dateToString(date, "yyyy-MM-dd HH:mm:ss");
	System.out.println("使用默认的东八区格式化:" + time);
	System.out.println(Files.getOwner(path));
    }

    // jdk7读取文件属性方式2:
    public static void getAttribute1(String path) {
	Path filePath = Paths.get(path);
	try {
	    BasicFileAttributes ra = Files.readAttributes(filePath, BasicFileAttributes.class);
	    System.out.println("CREATION TIME:" + ra.creationTime());
	    System.out.println("LAST ACCESS TIME:" + ra.lastAccessTime());
	    System.out.println("FILE SIZE:" + ra.size());// 字节
	    System.out.println("LAST MODIFIED:" + ra.lastModifiedTime());
	    System.out.println("IS SYSBOLIC LINK:" + ra.isSymbolicLink());
	    System.out.println("IS FOLDER:" + ra.isDirectory());
	    System.out.println("IS FILE:" + ra.isRegularFile());
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    // jdk7读取文件属性方式3:
    public static void getAttribute2(String path) {
	Path fp = Paths.get(path);
	try {
	    Files.getAttribute(fp, "basic:size");
	    System.out.println("CREATION TIME:"+ Files.getAttribute(fp, "basic:creationTime"));//创建时间
	    System.out.println("LAST ACCESS TIME:"+ Files.getAttribute(fp, "basic:lastAccessTime"));//访问时间
	    System.out.println("FILE SIZE:"+ Files.getAttribute(fp, "basic:size").toString());
	    System.out.println("LAST MODIFIED:"+ Files.getAttribute(fp, "basic:lastModifiedTime"));//修改时间
	    System.out.println("IS SYSBOLIC LINK:"+ Files.getAttribute(fp, "basic:isSymbolicLink"));
	    System.out.println("IS FOLDER:"+ Files.getAttribute(fp, "basic:isDirectory"));
	    System.out.println("IS FILE:"+ Files.getAttribute(fp, "basic:isRegularFile"));
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    
    //修改文件属性,比如修改文件的创建时间
    //同理可以修改文件的修改时间
//    Files.setAttribute(fp, "basic:lastAccessTime", fileTime);  
    //修改方式1:
    public static void  update1(String path){
	Path fp = Paths.get(path);  
        try{  
            BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);  
            long currentTimeMillis = System.currentTimeMillis();  
            FileTime fileTime = FileTime.fromMillis(currentTimeMillis);  
            Files.setAttribute(fp, "basic:creationTime", fileTime);  
            FileTime creationTime = (FileTime)Files.getAttribute(fp, "basic:creationTime");  
            System.out.println("NEW CREATION TIME:" + creationTime.toString());  
            System.out.println("OLD CREATION TIME:" + ra.creationTime());  
        }catch(IOException e){  
           e.printStackTrace();
        }  
    }  
    
    //修改方式2:setLastModifiedTime直接修改文件的修改时间
    public static void  update2(String path){
	Path fp = Paths.get("D:\\test.txt");  
        try{  
            BasicFileAttributes ra = Files.readAttributes(fp, BasicFileAttributes.class);  
            long currentTimeMillis = System.currentTimeMillis();  
            FileTime fileTime = FileTime.fromMillis(currentTimeMillis);  
            //直接修改文件的 修改时间
            Files.setLastModifiedTime(fp, fileTime);  
//          Files.setAttribute(path, attribute, value, options)
            FileTime time = (FileTime)Files.getAttribute(fp, "basic:lastModifiedTime");  
            System.out.println("NEW LAST-MODIFIED-TIME:" + time.toString());  
            System.out.println("OLD LAST-MODIFIED-TIME:" + ra.lastModifiedTime());  
        }catch(IOException e){  
            e.printStackTrace();
        }  
    }  
   
    public static void main(String[] args) throws IOException {
	// getAttribute("C:\\Users\\Administrator\\Desktop\\模板.txt");
	// geFileInfo("C:\\Users\\Administrator\\Desktop\\模板.txt");
	// getAttribute1("C:\\Users\\Administrator\\Desktop\\模板.txt");
//	getAttribute2("C:\\Users\\Administrator\\Desktop\\模板.txt");
//	update1("C:\\Users\\Administrator\\Desktop\\模板.txt");
    }
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用Java的DOM解析器来修改XML文件的某个属性。具体步骤如下: 1. 加载XML文件并创建DOM文档对象。 ```java DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("example.xml")); ``` 2. 获取需要修改的元素节点。 ```java Element root = doc.getDocumentElement(); NodeList nodeList = root.getElementsByTagName("book"); Element bookElement = null; for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); String id = element.getAttribute("id"); if (id.equals("001")) { bookElement = element; break; } } ``` 3. 修改属性。 ```java bookElement.setAttribute("price", "19.99"); ``` 4. 将修改后的DOM文档写入XML文件。 ```java TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("example.xml")); transformer.transform(source, result); ``` 完整代码示例: ```java import java.io.File; import javax.xml.parsers.*; import org.w3c.dom.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; public class ModifyXML { public static void main(String[] args) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("example.xml")); Element root = doc.getDocumentElement(); NodeList nodeList = root.getElementsByTagName("book"); Element bookElement = null; for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); String id = element.getAttribute("id"); if (id.equals("001")) { bookElement = element; break; } } bookElement.setAttribute("price", "19.99"); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("example.xml")); transformer.transform(source, result); System.out.println("XML file updated successfully"); } catch (Exception e) { e.printStackTrace(); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值