java中的文件操作总结(干货)
File类简介
packagecom.file;
importjava.io.File;
importjava.io.IOException;
/**
*Createdbyelijahliuon2017/2/10.
*/
publicclassfiletest{
publicstaticvoidmain(String[]args){
Filefile=newFile("hello.txt");
//是否存在
if(file.exists()){
//文件
System.out.println(file.isFile());
//路径(文件夹)
System.out.println(file.isDirectory());
Filenameto=newFile("newHello.txt");
file.renameTo(nameto);//这里就是重命名文件的操作,直接新建一个file对象然后使用renameTo方法可以重命名文件
}else{
System.out.println("文件不存在");
try{
file.createNewFile();
System.out.println("文件已被创建");
}catch(IOExceptione){
System.out.println("文件无法创建");
}
}
if(file.exists()){
//删除文件
file.delete();
System.out.println("删除文件");
}else{
}
}
}
文件夹操作
packagecom.file;
importjava.io.File;
/**
*Createdbyelijahliuon2017/2/11.
*/
publicclassHelloFolder{
publicstaticvoidmain(String[]args){
Filefolder=newFile("mynewfolder");
if(folder.mkdir()){//创建文件夹判断是否成功
System.out.println("文件夹创建完成");
Filenewfolder=newFile("mynnewfoleder-new");
folder.renameTo(newfolder);//这里重命名了文件夹文件夹的重命名是可以单独更改一级的文件夹名的而这一级下面的文件夹不变保存目录结构
if(folder.delete()){
System.out.print("done");//这里的删除只能删除空文件夹,如果文件夹中有东西,那么则不能删除,不问三七二十一直接删除一个非空文件夹是非常不负责任的
}else{
System.out.println("fail");
}
}else{
if(folder.exists()){
System.out.println("文件夹已经存在不用创建");
}else{
System.out.println("文件夹创建失败");
}
}
Filefolders=newFile("mynewfolder/one/two/three/main");
folders.mkdirs();//在java中用mkdir只能创建一个,mkdirs可以创建多级目录
}
}
文件属性设置
packagecom.file;
importjava.io.File;
/**
*Createdbyelijahliuon2017/2/11.
*/
publicclassSetFileProperty{
publicstaticvoidmain(String[]args){
Filefile=newFile("test.file");
if(file.exists()){
file.setWritable(true);//可写
file.setReadable(true);//可读
file.setReadOnly();//只读
}
}
}
遍历文件夹
publicvoidprintFiles(Filedir,inttab){//tab为不同目录结构的缩进量
if(dir.isDirectory()){
Filenext[]=dir.listFiles();//判断如果是目录则返回目录所有的文件名数组用于遍历文件夹
for(inti=0;i<next.length;i++){//层次缩进输出
System.out.print("---");
}
for(inti=0;i<next.length;i++){//这里用了递归获取目录结构
System.out.println(next[i].getName());
if(next[i].isFile()){
printFiles(next[i],++tab);
}
}
}
}
文件简单读写
packagecom.file;
importjava.io.*;
/**
*Createdbyelijahliuon2017/2/11.
*/
publicclassReadFile{
publicstaticvoidmain(String[]args){
Filefile=newFile("newHello.txt");
if(file.exists()){
System.err.print("exsit");
try(FileInputStreamfis=newFileInputStream(file)){//文件输入流这是字节流
InputStreamReaderisr=newInputStreamReader(fis,"UTF-8");//inputstreamReader是一个字节流,将字节流和字符流转化的时候,就需要制定一个编码方式,不然就会乱码
BufferedReaderbr=newBufferedReader(isr);//字符缓冲区
Stringline;
while((line=br.readLine())!=null){//这里将缓冲区里的内容如果非空就读出来打印
System.out.println(line);
}
br.close();//最后将各个线程关闭
isr.close();
fis.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
Filenewfile=newFile("newtext.txt");
try{
FileOutputStreamfos=newFileOutputStream(newfile);//这里如果文件不存在会自动创建文件
OutputStreamWriterosw=newOutputStreamWriter(fos,"UTF-8");//和读取一样这里是转化的是字节和字符流
BufferedWriterbw=newBufferedWriter(osw);//这里是写入缓冲区
bw.write("厉害了我的哥");//写入字符串
bw.close();//和上面一样这里后打开的先关闭先打开的后关闭
osw.close();
fos.close();
System.out.println("done");
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。