java中怎样获得一个文件夹中的所有文件名

java中怎样获得一个文件夹中的所有文件名,第1张

java中获得一个文件夹中的所有文件名代码如下:

package comreadfile;

import javaioFile;

public class GetAllFiles {

public static void main(String[] args) {

//路径   这里写一个路径进去

String path="F:\\QQ文档";

//调用方法

getFiles(path);

}

 

/

递归获取某路径下的所有文件,文件夹,并输出

/

public static void getFiles(String path) {

File file = new File(path);

// 如果这个路径是文件夹

if (fileisDirectory()) {

// 获取路径下的所有文件

File[] files = filelistFiles();

for (int i = 0; i < fileslength; i++) {

// 如果还是文件夹 递归获取里面的文件 文件夹

if (files[i]isDirectory()) {

Systemoutprintln("目录:" + files[i]getPath());

getFiles(files[i]getPath());

} else {

Systemoutprintln("文件:" + files[i]getPath());

}

}

} else {

Systemoutprintln("文件:" + filegetPath());

}

}

}

扩展资料:

如果想要获得当前文件中的文件名只需要String [] fileName = filelist();就可以了。

如果要包括文件中的文件名就可以用递归的方式。下面是两个具体的实现。

其中public static String [] getFileName(String path)是只得到当前文件中的文件名。

public static void getAllFileName(String path,ArrayList<String> fileName)是包括当前文件及其子文件的文件名。

参考资料:

百度百科-Java

方法如下:

首先

创建一个Bean用来存贮要得到的信

public class MonitorInfoBean {

/ 可使用内存 /

private long totalMemory;

/ 剩余内存 /

private long freeMemory;

/ 最大可使用内存 /

private long maxMemory;

/ *** 作系统 /

private String osName;

/ 总的物理内存 /

private long totalMemorySize;

/ 剩余的物理内存 /

private long freePhysicalMemorySize;

/ 已使用的物理内存 /

private long usedMemory;

/ 线程总数 /

private int totalThread;

/ cpu使用率 /

private double cpuRatio;

public long getFreeMemory() {

return freeMemory;

}

public void setFreeMemory(long freeMemory) {

thisfreeMemory = freeMemory;

}

public long getFreePhysicalMemorySize() {

return freePhysicalMemorySize;

}

public void setFreePhysicalMemorySize(long freePhysicalMemorySize) {

thisfreePhysicalMemorySize = freePhysicalMemorySize;

}

public long getMaxMemory() {

return maxMemory;

}

public void setMaxMemory(long maxMemory) {

thismaxMemory = maxMemory;

}

public String getOsName() {

return osName;

}

public void setOsName(String osName) {

thisosName = osName;

}

public long getTotalMemory() {

return totalMemory;

}

public void setTotalMemory(long totalMemory) {

thistotalMemory = totalMemory;

}

public long getTotalMemorySize() {

return totalMemorySize;

}

public void setTotalMemorySize(long totalMemorySize) {

thistotalMemorySize = totalMemorySize;

}

public int getTotalThread() {

return totalThread;

}

public void setTotalThread(int totalThread) {

thistotalThread = totalThread;

}

public long getUsedMemory() {

return usedMemory;

}

public void setUsedMemory(long usedMemory) {

thisusedMemory = usedMemory;

}

public double getCpuRatio() {

return cpuRatio;

}

public void setCpuRatio(double cpuRatio) {

thiscpuRatio = cpuRatio;

}

}

之后,建立bean的接口

public interface IMonitorService {

public MonitorInfoBean getMonitorInfoBean() throws Exception;

}

然后,就是最关键的,得到cpu的利用率,已用内存,可用内存,最大内存等信息。

import javaioInputStreamReader;

import javaioLineNumberReader;

import sunmanagementManagementFactory;

import comsunmanagementOperatingSystemMXBean;

import javaio;

import javautilStringTokenizer;

/

获取系统信息的业务逻辑实现类

@author GuoHuang

/

public class MonitorServiceImpl implements IMonitorService {

private static final int CPUTIME = 30;

private static final int PERCENT = 100;

private static final int FAULTLENGTH = 10;

private static final File versionFile = new File("/proc/version");

private static String linuxVersion = null;

/

获得当前的监控对象

@return 返回构造好的监控对象

@throws Exception

@author GuoHuang

/

public MonitorInfoBean getMonitorInfoBean() throws Exception {

int kb = 1024;

// 可使用内存

long totalMemory = RuntimegetRuntime()totalMemory() / kb;

// 剩余内存

long freeMemory = RuntimegetRuntime()freeMemory() / kb;

// 最大可使用内存

long maxMemory = RuntimegetRuntime()maxMemory() / kb;

OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory

getOperatingSystemMXBean();

// *** 作系统

String osName = SystemgetProperty("osname");

// 总的物理内存

long totalMemorySize = osmxbgetTotalPhysicalMemorySize() / kb;

// 剩余的物理内存

long freePhysicalMemorySize = osmxbgetFreePhysicalMemorySize() / kb;

// 已使用的物理内存

long usedMemory = (osmxbgetTotalPhysicalMemorySize() - osmxb

getFreePhysicalMemorySize())

/ kb;

// 获得线程总数

ThreadGroup parentThread;

for (parentThread = ThreadcurrentThread()getThreadGroup(); parentThread

getParent() != null; parentThread = parentThreadgetParent())

;

int totalThread = parentThreadactiveCount();

double cpuRatio = 0;

if (osNametoLowerCase()startsWith("windows")) {

cpuRatio = thisgetCpuRatioForWindows();

}

else {

cpuRatio = thisgetCpuRateForLinux();

}

// 构造返回对象

MonitorInfoBean infoBean = new MonitorInfoBean();

infoBeansetFreeMemory(freeMemory);

infoBeansetFreePhysicalMemorySize(freePhysicalMemorySize);

infoBeansetMaxMemory(maxMemory);

infoBeansetOsName(osName);

infoBeansetTotalMemory(totalMemory);

infoBeansetTotalMemorySize(totalMemorySize);

infoBeansetTotalThread(totalThread);

infoBeansetUsedMemory(usedMemory);

infoBeansetCpuRatio(cpuRatio);

return infoBean;

}

private static double getCpuRateForLinux(){

InputStream is = null;

InputStreamReader isr = null;

BufferedReader brStat = null;

StringTokenizer tokenStat = null;

try{

Systemoutprintln("Get usage rate of CUP , linux version: "+linuxVersion);

Process process = RuntimegetRuntime()exec("top -b -n 1");

is = processgetInputStream();

isr = new InputStreamReader(is);

brStat = new BufferedReader(isr);

if(linuxVersionequals("24")){

brStatreadLine();

brStatreadLine();

brStatreadLine();

brStatreadLine();

tokenStat = new StringTokenizer(brStatreadLine());

tokenStatnextToken();

tokenStatnextToken();

String user = tokenStatnextToken();

tokenStatnextToken();

String system = tokenStatnextToken();

tokenStatnextToken();

String nice = tokenStatnextToken();

Systemoutprintln(user+" , "+system+" , "+nice);

user = usersubstring(0,userindexOf("%"));

system = systemsubstring(0,systemindexOf("%"));

nice = nicesubstring(0,niceindexOf("%"));

float userUsage = new Float(user)floatValue();

float systemUsage = new Float(system)floatValue();

float niceUsage = new Float(nice)floatValue();

return (userUsage+systemUsage+niceUsage)/100;

}else{

brStatreadLine();

brStatreadLine();

tokenStat = new StringTokenizer(brStatreadLine());

tokenStatnextToken();

tokenStatnextToken();

tokenStatnextToken();

tokenStatnextToken();

tokenStatnextToken();

tokenStatnextToken();

tokenStatnextToken();

String cpuUsage = tokenStatnextToken();

Systemoutprintln("CPU idle : "+cpuUsage);

Float usage = new Float(cpuUsagesubstring(0,cpuUsageindexOf("%")));

return (1-usagefloatValue()/100);

}

} catch(IOException ioe){

Systemoutprintln(ioegetMessage());

freeResource(is, isr, brStat);

return 1;

} finally{

freeResource(is, isr, brStat);

}

}

一 获取当前系统时间和日期并格式化输出:

import javautilDate;

import javatextSimpleDateFormat;

public class NowString {

public static void main(String[] args) {

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

Systemoutprintln(dfformat(new Date()));// new Date()为获取当前系统时间

}

}

二 在数据库里的日期只以年-月-日的方式输出,可以用下面两种方法:

1、用convert()转化函数:

String sqlst = "select convert(varchar(10),bookDate,126) as convertBookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

Systemoutprintln(rsgetString("convertBookDate"));

2、利用SimpleDateFormat类:

先要输入两个java包:

import javautilDate;

import javatextSimpleDateFormat;

然后:

定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

输出:

Systemoutprintln(dfformat(rsgetDate("bookDate")));

以上就是关于java中怎样获得一个文件夹中的所有文件名全部的内容,包括:java中怎样获得一个文件夹中的所有文件名、怎样用Java获取内存中的数据、请问JAVA中获取系统当前时间该怎么写等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10084297.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存