文件访问时间识别
代码1
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
/**
* 读取文件创建时间和最后修改时间
*/
public class hello {
public static void main(String[] args) {
getCreateTime();
getModifiedTime_2();
}
/**
* 读取文件创建时间
*/
public static void getCreateTime(){
String filePath = "/home/dear/code/sh/1.sh";
String strTime = null;
try {
Process p = Runtime.getRuntime().exec("cmd /C dir "
+ filePath
+ "/tc" );
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while((line = br.readLine()) != null){
if(line.endsWith(".sh")){
strTime = line.substring(0,17);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("创建时间 " + strTime);
}
/**
* 读取文件修改时间的方法1
@SuppressWarnings("deprecation")
public static void getModifiedTime_1(){
File f = new File("C:\\test.txt");
Calendar cal = Calendar.getInstance();
long time = f.lastModified();
cal.setTimeInMillis(time);
//此处toLocalString()方法是不推荐的,但是仍可输出
System.out.println("修改时间[1] " + cal.getTime().toLocaleString());
//输出:修改时间[1] 2009-8-17 10:32:38
}
*/
/**
* 读取修改时间的方法2
*/
public static void getModifiedTime_2(){
File f = new File("/home/dear/code/sh/1.sh");
Calendar cal = Calendar.getInstance();
long time = f.lastModified();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cal.setTimeInMillis(time);
System.out.println("修改时间[2] " + formatter.format(cal.getTime()));
}
}
代码2
NIO.2详解
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.Paths
public class ObtainPath
{
public static void main(String[] args)
{
Path path = Paths.get("a", "b")
System.out.println(path)
path = Paths.get(FileSystems.getDefault().getSeparator() + "a", "b", "c")
System.out.println(path)
path = Paths.get("a", ":", "b")
System.out.println(path)
}
}
import java.nio.file.FileSystem
import java.nio.file.FileSystems
import java.nio.file.Path
import java.nio.file.Paths
public class hello
{
public static void main(String[] args)
{
dumpPathInfo(Paths.get("a", "b", "c"))
FileSystem fs = FileSystems.getDefault()
Iterable<Path> roots = fs.getRootDirectories()
for(Path root: roots)
{
dumpPathInfo(Paths.get(root.toString(), "a", "b", "c"))
break
}
}
static void dumpPathInfo(Path path)
{
System.out.printf("Path: %s%n", path)
System.out.printf("Filename: %s%n", path.getFileName())
System.out.println("Components")
for (int i = 0
System.out.printf(" %s%n", path.getName(i))
System.out.printf("Parent: %s%n", path.getParent())
System.out.printf("Root: %s%n", path.getRoot())
System.out.printf("Absolute: %b%n", path.isAbsolute())
System.out.printf("Subpath [0, 2): %s%n%n", path.subpath(0, 2))
}
}
import java.nio.file.Path
import java.nio.file.Paths
public class hello
{
public static void main(String[] args)
{
Path path1 = Paths.get("reports", ".", "2015", "jan")
System.out.println(path1)
System.out.println(path1.normalize())
path1 = Paths.get("reports", "2015", "..", "jan")
System.out.println(path1.normalize())
System.out.println()
path1 = Paths.get("reports", "2015", "jan")
System.out.println(path1)
System.out.println(path1.relativize(Paths.get("reports", "2016", "mar")))
try
{
System.out.println(path1.relativize(Paths.get("/reports", "2016",
"mar")))
}
catch (IllegalArgumentException iae)
{
iae.printStackTrace()
}
System.out.println()
path1 = Paths.get("reports", "2015")
System.out.println(path1)
System.out.println(path1.resolve("apr"))
System.out.println(path1.resolve("/apr"))
}
}
import java.io.IOException
import java.nio.file.attribute.FileTime
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
public class PathAttrib
{
public static void main(String[] args) throws IOException
{
if (args.length != 1)
{
System.err.println("usage: java PathAttrib path-object")
return
}
Path path = Paths.get(args[0])
System.out.printf("Last modified time: %s%n", Files.getLastModifiedTime(path))
System.out.printf("Last access time: %s%n", (FileTime)Files.getAttribute(path, "lastAccessTime"))
System.out.printf("Owner: %s%n", Files.getOwner(path))
System.out.printf("Size: %d%n", Files.size(path))
}
}
文件访问时间修改
import java.io.File;
import java.util.Date;
public class FileUtil {
public void changeFiletime(String filename) {
File fileToChange = new File(filename);
Date filetime = new Date(fileToChange.lastModified());
System.out.println(filetime.toString());
if (fileToChange.setLastModified(System.currentTimeMillis()))
System.out.println("Success!");
else
System.out.println("Failed!");
filetime = new Date(fileToChange.lastModified());
System.out.println(filetime.toString());
}
public static void main(String[] args) {
FileUtil fileutil = new FileUtil();
fileutil.changeFiletime("C:\\\\temp\\\\myfile.txt");
}
}
手机文件遍历
代码1
public static Vector<string> GetVideoFileName(String fileAbsolutePath) {
Vector<string> vecFile = new Vector<string>();
File file = new File(fileAbsolutePath);
File[] subFile = file.listFiles();
for (int iFileLength = 0; iFileLength < subFile.length; iFileLength++) {
if (!subFile[iFileLength].isDirectory()) {
String filename = subFile[iFileLength].getName();
if (filename.trim().toLowerCase().endsWith(".mp4")) {
vecFile.add(filename);
}
}
}
return vecFile;
}
代码2
import java.io.IOException;
import java.io.*;
public class hello
{
public static void main(String[] args) throws IOException
{
String path="/home/dear/code";
int count =countFile(path);
System.out.println("文件有"+count+"个");
}
static int result= 0;
public static int countFile(String path){
File file = new File(path);
if(!file.exists()){
System.out.println("提供的路径不存在!");
return 0;
}
File[] fs =file.listFiles();
if(fs==null){
System.out.println("提供的路径不是一个标准文件");
return 0;
}
for(int i =0;i<fs.length;i++){
File f = fs[i];
String str = f.getAbsolutePath();
if(f.isFile()){
result++;
System.out.println("找到文件"+str);
}else if(f.isDirectory()){
result +=countFile(str);
}
}
return result;
}
}
文件遍历+访问时间+距离时间
import java.io.*;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class hello
{
public static void main(String[] args) throws IOException
{
String path="/home/dear/Desktop";
int count =countFile(path);
System.out.println("文件有"+count+"个");
}
static int result= 0;
static Date now=new Date();
static long nowtime=now.getTime();
public static int countFile(String path){
File file = new File(path);
if(!file.exists()){
System.out.println("提供的路径不存在!");
return 0;
}
File[] fs =file.listFiles();
if(fs==null){
System.out.println("提供的路径不是一个标准文件");
return 0;
}
for(int i =0;i<fs.length;i++){
File f = fs[i];
String str = f.getAbsolutePath();
if(f.isFile()){
result++;
System.out.println("找到文件"+str);
String strTime = null;
try {
Process p = Runtime.getRuntime().exec("stat " + str);
InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while((line = br.readLine()) != null){
if(line.endsWith("00")&&line.startsWith("Access")){
strTime = line.substring(8,27);
break;
}
}
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date access_time=formatter.parse(strTime);
long time=access_time.getTime();
System.out.println((nowtime-time)/3600/1000/24);
}
catch(ParseException pppp){
pppp.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(strTime);
}
else if(f.isDirectory()){
result +=countFile(str);
}
}
return result;
}
}