栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

Java中对文件进行操作的类都有哪些?

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java中对文件进行操作的类都有哪些?

File类:

对硬盘上的文件和目录进行操作的类。

构造函数:

1) File(String pathname)
Creates a new File instance by converting the given pathname string into an abstract pathname.

2)File(File parent, String child)
Creates a new File instance from a parent abstract pathname and a child pathname string.

3)File(String parent, String child)
Creates a new File instance from a parent pathname string and a child pathname string.

4)File(URI uri)
Creates a new File instance by converting the given file: URI into an abstract pathname.

方法:

1) list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

2)  list(FilenameFilter filter)
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

3) listFiles()
Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

4) listFiles(FileFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

5) listFiles(FilenameFilter filter)
Returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.

6) Boolean  createNewFile()
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

7)  mkdir()
Creates the directory named by this abstract pathname.

8) static File createTempFile(String prefix, String suffix)
Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

 

演示:

File path = new File(“icons”);

try {

//创建文件和目录

path.mkdir();

File.createTempFile(“temp”,”.java”,path);

} catch (IOException e) {

System.out.println(“创建临时文件异常:= ” +e.getMessage());

}

File f = new File(“.”);

File[] fileList = f.listFiles();

for(int i = 0; i < fileList.length;i++){

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

System.out.println(“目:= ” + fileList[i].getAbsolutePath());

}else{

System.out.println(“文:= ” + fileList[i].getAbsolutePath());

}

}

//筛选文件

String[] fileNames = f.list(new FilterNames());

for(int i = 0; i < fileNames.length;i++){

System.out.println(“名称为:= ” + fileNames[i]);

}

 

IO流:

 

流的分类:

1)节点流:应用程序直接操作目标设备所对应的流,只能操作字节字符型数据。

字节流

InputStream

OutputStream

字符流

 

2)包装流:提供了应用程序与外部设备之间,各种类型数据的读写功能。

对文件操作的流:

FileInputStream

FileOutputStream

FileReader

FileWriter

Reader

Writer

RandomAccessFile

RandomAccess

 

演示:

File f = new File(“configuration.txt”);

InputStream in = null;

OutputStream out = null;

String data = “世界和平是任何力量阻挡不了的” + System.getProperty(“line.separator”);

//调用系统的换行方式,在windows中使用’rn’

写入

try {

out = new FileOutputStream(f,true);   //追加在文件的末尾的方法

out.write(data.getBytes());

out.close();

} catch (IOException e) {

System.out.println(“写文件数据异常:= ” + e.getMessage());

}

 

 

try{

in = new FileInputStream(f);

byte[] buff = new byte[102];

int len;

while((len = in.read(buff)) != -1){

System.out.println(“len:= ” + len);

System.out.println(new String(buff,0,len));

}

in.close();

}catch(IOException e){

System.out.println(“读取文件数据异常:= ” + e.getMessage());

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/359804.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号