导航:首页 > 编程语言 > java读取路径下文件

java读取路径下文件

发布时间:2022-07-08 01:58:32

java怎样获得某个目录下所有文件的文件名

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

packagecom.readfile;

importjava.io.File;

publicclassGetAllFiles {

publicstaticvoidmain(String[] args) {

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

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

//调用方法

getFiles(path);

}

/**

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

*/

publicstaticvoidgetFiles(String path) {

File file =newFile(path);

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

if(file.isDirectory()) {

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

File[] files = file.listFiles();

for(inti =0; i < files.length; i++) {

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

if(files[i].isDirectory()) {

System.out.println("目录:"+ files[i].getPath());

getFiles(files[i].getPath());

}else{

System.out.println("文件:"+ files[i].getPath());

}

}

}else{

System.out.println("文件:"+ file.getPath());

}

}

}

(1)java读取路径下文件扩展阅读:

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

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

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

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

Ⅱ Java 如何读取目录下的文件内容

Java读取目录下的文件内容,使用的是java的文件类,示例如下:

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importjava.io.RandomAccessFile;
importjava.io.Reader;

publicclassReadFromFile{
/**
*以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*
*@paramfileName
*文件的名
*/
(StringfileName){
Filefile=newFile(fileName);
InputStreamin=null;
try{
System.out.println("以字节为单位读取文件内容,一次读一个字节:");
//一次读一个字节
in=newFileInputStream(file);
inttempbyte;
while((tempbyte=in.read())!=-1){
System.out.write(tempbyte);
}
in.close();
}catch(IOExceptione){
e.printStackTrace();
return;
}
try{
System.out.println("以字节为单位读取文件内容,一次读多个字节:");
//一次读多个字节
byte[]tempbytes=newbyte[100];
intbyteread=0;
in=newFileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
//读入多个字节到字节数组中,byteread为一次读入的字节数
while((byteread=in.read(tempbytes))!=-1){
System.out.write(tempbytes,0,byteread);
}
}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(in!=null){
try{
in.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以字符为单位读取文件,常用于读文本,数字等类型的文件
*
*@paramfileName
*文件名
*/
(StringfileName){
Filefile=newFile(fileName);
Readerreader=null;
try{
System.out.println("以字符为单位读取文件内容,一次读一个字节:");
//一次读一个字符
reader=newInputStreamReader(newFileInputStream(file));
inttempchar;
while((tempchar=reader.read())!=-1){
//对于windows下, 这两个字符在一起时,表示一个换行。
//但如果这两个字符分开显示时,会换两次行。
//因此,屏蔽掉 ,或者屏蔽 。否则,将会多出很多空行。
if(((char)tempchar)!=' '){
System.out.print((char)tempchar);
}
}
reader.close();
}catch(Exceptione){
e.printStackTrace();
}
try{
System.out.println("以字符为单位读取文件内容,一次读多个字节:");
//一次读多个字符
char[]tempchars=newchar[30];
intcharread=0;
reader=newInputStreamReader(newFileInputStream(fileName));
//读入多个字符到字符数组中,charread为一次读取字符数
while((charread=reader.read(tempchars))!=-1){
//同样屏蔽掉 不显示
if((charread==tempchars.length)
&&(tempchars[tempchars.length-1]!=' ')){
System.out.print(tempchars);
}else{
for(inti=0;i<charread;i++){
if(tempchars[i]==' '){
continue;
}else{
System.out.print(tempchars[i]);
}
}
}
}

}catch(Exceptione1){
e1.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*以行为单位读取文件,常用于读面向行的格式化文件
*
*@paramfileName
*文件名
*/
(StringfileName){
Filefile=newFile(fileName);
BufferedReaderreader=null;
try{
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader=newBufferedReader(newFileReader(file));
StringtempString=null;
intline=1;
//一次读入一行,直到读入null为文件结束
while((tempString=reader.readLine())!=null){
//显示行号
System.out.println("line"+line+":"+tempString);
line++;
}
reader.close();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(reader!=null){
try{
reader.close();
}catch(IOExceptione1){
}
}
}
}

/**
*随机读取文件内容
*
*@paramfileName
*文件名
*/
(StringfileName){
RandomAccessFilerandomFile=null;
try{
System.out.println("随机读取一段文件内容:");
//打开一个随机访问文件流,按只读方式
randomFile=newRandomAccessFile(fileName,"r");
//文件长度,字节数
longfileLength=randomFile.length();
//读文件的起始位置
intbeginIndex=(fileLength>4)?4:0;
//将读文件的开始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[]bytes=newbyte[10];
intbyteread=0;
//一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
//将一次读取的字节数赋给byteread
while((byteread=randomFile.read(bytes))!=-1){
System.out.write(bytes,0,byteread);
}
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(randomFile!=null){
try{
randomFile.close();
}catch(IOExceptione1){
}
}
}
}

/**
*显示输入流中还剩的字节数
*
*@paramin
*/
(InputStreamin){
try{
System.out.println("当前字节输入流中的字节数为:"+in.available());
}catch(IOExceptione){
e.printStackTrace();
}
}

publicstaticvoidmain(String[]args){
StringfileName="C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}

Ⅲ java怎么取当前目录下的文件

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;

public class ReadFile {
public ReadFile() {
}
/**
* 读取某个文件夹下的所有文件
*/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {

File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());

} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath="
+ readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());

} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}

}

} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}

/**
* 删除某个文件夹下的所有文件夹和文件
*/

/*public static boolean deletefile(String delpath)
throws FileNotFoundException, IOException {
try {

File file = new File(delpath);
if (!file.isDirectory()) {
System.out.println("1");
file.delete();
} else if (file.isDirectory()) {
System.out.println("2");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
System.out.println("path=" + delfile.getPath());
System.out.println("absolutepath="
+ delfile.getAbsolutePath());
System.out.println("name=" + delfile.getName());
delfile.delete();
System.out.println("删除文件成功");
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
file.delete();

}

} catch (FileNotFoundException e) {
System.out.println("deletefile() Exception:" + e.getMessage());
}
return true;
}*/

public static void main(String[] args) {
try {
readfile("e:/videos");
// deletefile("D:/file");
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
System.out.println("ok");
}

Ⅳ java根据路径读取文件

其读取方法为:

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.util.ArrayList;


publicclassreadFile{
privatestaticArrayList<String>listname=newArrayList<String>();
publicstaticvoidmain(String[]args)throwsException{
readAllFile("C:/Users/HP/Desktop");
System.out.println(listname.size());
}
publicstaticvoidreadAllFile(Stringfilepath){
Filefile=newFile(filepath);
if(!file.isDirectory()){
listname.add(file.getName());
}elseif(file.isDirectory()){
System.out.println("文件");
String[]filelist=file.list();
for(inti=0;i<filelist.length;i++){
Filereadfile=newFile(filepath);
if(!readfile.isDirectory()){
listname.add(readfile.getName());
}elseif(readfile.isDirectory()){
readAllFile(filepath+"\"+filelist[i]);//递归
}
}
}
for(inti=0;i<listname.size();i++){
System.out.println(listname.get(i));
}
}
}

Ⅳ java怎么通过文件的路径读取文件

packagefile.system.demo.exception;

importjava.io.File;
importjava.io.FileNotFoundException;
importjava.util.Scanner;

publicclassReadFile{
publicstaticStringgetFile(Stringrealpath){
Scannerscanner=null;
Stringtext="";
try{
Filefile=newFile(realpath);
scanner=newScanner(file);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
if(scanner!=null){
while(scanner.hasNextLine()){
text+=scanner.nextLine();
}
scanner.close();
}
//System.out.println(text);

returntext;
}


staticclassInnerTest{
publicstaticvoidmain(String[]args){
Stringrealpath="D:\test.txt";
Stringtext=getFile(realpath);
System.out.println(text);
}
}

}

实现方式有很多,还可以用字节流FileInputStream,字符流FileReader等方式读取

Ⅵ java怎么获取一个目录下的所有文件名

获取一个目录下的所有文件和目录方法:

importjava.io.File;

publicclassFileTest{

publicstaticvoidmain(String[]args){
/**
*将目标目录封装成File对象。
*/
Filedir=newFile("/Users/zym/Desktop/Mac应用程序");

/**
*获取目录下的所有文件和文件夹
*/
String[]names=dir.list();

for(Stringname:names){
System.out.println(name);
}
}

}

通过文件过滤器获取某个目录下的文件,例如所有的 .txt、.doc文件。

过滤器:

importjava.io.File;
importjava.io.FilenameFilter;

{

@Override
publicbooleanaccept(Filedir,Stringname){
returnname.endsWith(".doc");
}

}

获取某个目录下的按照过滤器规则的所有文件:

importjava.io.File;

publicclassFileTest{

publicstaticvoidmain(String[]args){
/**
*将目标目录封装成File对象。
*/
Filedir=newFile("/Users/zym/Desktop/Mac应用程序");

/**
*通过过滤器获取目录下的所有的.doc文件
*/
String[]names=dir.list(newFilterByDoc());

for(Stringname:names){
System.out.println(name);
}
}

}

希望能对您有所帮助!

Ⅶ java 怎么获取指定路径下的文件

//根据你的要求修改了一下代码,现在已经能将某文件夹下的所有指定类型文件复制到
//指定文件夹下了
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ReadFiles {
public static final String FILTER = "xml";
public static final String SRC_DIR = "E:\\StudyData";// 待扫描的文件夹
public static final String DES_DIR = "E:\\testdata";// 复制后的目标文件夹

public static void main(String[] args) {
long a = System.currentTimeMillis();
scanDir(SRC_DIR, DES_DIR);
System.out.println("共花费时间:"+(System.currentTimeMillis() - a)/1000+"秒");
}

public static void scanDir(String srcPath, String desPath) {
File dir = new File(srcPath);
File[] files = dir.listFiles();

if (files == null)
return;
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
scanDir(files[i].getAbsolutePath(), desPath);
} else {
String strFileName = files[i].getAbsolutePath().toLowerCase();
File(strFileName, desPath + files[i].getName());
}
}
}

public static void File(String srcName, String destName) {
if (srcName.endsWith(FILTER)) {
System.out.println("正在复制文件 "+srcName+" 至 "+destName);
try {
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(srcName));
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(destName));
int i = 0;
byte[] buffer = new byte[2048];
while ((i = in.read(buffer)) != -1) {
out.write(buffer, 0, i);
}
out.close();
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}

Ⅷ java读取文件路径问题

在java中获得文件的路径在我们做上传文件操作时是不可避免的。

web 上运行
1:
this.getClass().getClassLoader().getResource("/").getPath();
this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
System.getProperty("user.dir");
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

2:
this.getClass().getResource("/").getPath();
this.getClass().getResource("").getPath(); 得到的是当前类 文件的URI目录。不包括自己!
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/
this.getClass().getResource(".").getPath(); X 不 能运行

3:
Thread.currentThread().getContextClassLoader().getResource("/").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/
Thread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 项目的绝对路径。
如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

在本地运行中
1:
this.getClass().getClassLoader().getResource("").getPath();
this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
this.getClass().getClassLoader().getResource(".").getPath(); X 不 能运行
2:
this.getClass().getResource("").getPath();
this.getClass().getResource(".").getPath(); 得到的是当前类 文件的URI目录。不包括自己!
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/
/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

3:
Thread.currentThread().getContextClassLoader().getResource(".").getPath()
Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classes
Thread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能运行

最后
在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。
还有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

Ⅸ java如何通过文件路径读取该路径下的所有文件并将其放入list中

java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中。

假设指定路径为path,目标集合为fileList,遍历指定路径下的所有文件,如果是目录文件则递归调用,如果是普通文件则放入fileList中。
根据这个思路,得到java源代码如下所示:
//方法getFiles根据指定路径获取所有的文件
public void getFiles(String path){
//目标集合fileList
ArrayList<File> fileList = new ArrayList();
File file = new File(path);
if(file.isDirectory()){
File []files = file.listFiles();
for(File fileIndex:files){
//如果这个文件是目录,则进行递归搜索
if(fileIndex.isDirectory()){
getFiles(fileIndex.getPath());
}else {
//如果文件是普通文件,则将文件句柄放入集合中
fileList.add(fileIndex);
}
}
}
}

Ⅹ java如何在某一路径下读文件

其实你可以将这些配置文件写进xml文件,然后再读取时按照节点来读取就可以了。这样的知识点在网上有很多的。

阅读全文

与java读取路径下文件相关的资料

热点内容
自己购买云主服务器推荐 浏览:419
个人所得税java 浏览:760
多余的服务器滑道还有什么用 浏览:189
pdf劈开合并 浏览:26
不能修改的pdf 浏览:750
同城公众源码 浏览:488
一个服务器2个端口怎么映射 浏览:297
java字符串ascii码 浏览:78
台湾云服务器怎么租服务器 浏览:475
旅游手机网站源码 浏览:332
android关联表 浏览:945
安卓导航无声音怎么维修 浏览:332
app怎么装视频 浏览:430
安卓系统下的软件怎么移到桌面 浏览:96
windows拷贝到linux 浏览:772
mdr软件解压和别人不一样 浏览:904
单片机串行通信有什么好处 浏览:340
游戏开发程序员书籍 浏览:860
pdf中图片修改 浏览:288
汇编编译后 浏览:491