一:文件目录
File类
作用:
用于文件,目录的基本操作
语法:
File file=new File(String path);
方法:
方法名 | 说明 |
---|---|
exists() | 判断文件或目录是否存在 |
isFile() | 判断是否是文件 |
isDirectory() | 判断是否是目录 |
getName() | 得到文件或目录的名 |
getPath() | 得到文件的路径 |
getAbsolutePath() | 得到文件的绝对路径 |
length() | 返回文件的长度,若文件不存在,则返回0 |
delete() | 输出指定的文件或目录 |
creatNewFile() | 创建新的空文件 |
实例:
import java.io.File;
public static void main(String[] args) {
File file=new File("D:\\project\\Hello.txt");
try {
file.createNewFile(); //创建
System.out.println("文件已经创建");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件已经创建");
if(file.exists()) { //判断文件是否存在
if(file.isFile()) { //判断是否是文件
System.out.println("名称:"+file.getName());
}
if(file.isDirectory()){ //判断
System.out.println("此文件是目录");是否是目录
}
}else {
System.out.println("此文件不存在");
}
}
二:流
作用:
发送和接收数据的通道
分类:
根据读取方式:字节流,字符流
根据流向方式:输入流,输出流
读取步骤:
1.导入包,Java.IO
2.创建流对象
3.使用对象的方法进行读取
4.关闭流
三:读文件
基类(抽象类):InputStream,Reader
子类:FileInputStream,FileReader,BufferedReader
FileInputStream实例:
import java.io.*;
public static void main(String[] args) throws Exception {
FileInputStream file=null;
try {
file=new FileInputStream("D:\\project\\Hello.txt");
System.out.println("文件内容为 :");
int date;
while((date=file.read())!=-1) { //判断是否有值
System.out.println(date+"");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(file!=null) {
file.close();
}
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
FileReader实例:
import java.io.*;
public static void main(String[] args) {
Reader fr=null;
try {
fr=new FileReader("D:\\project\\hello.txt");
char ch[]=new char[1024];
int length=fr.read(ch); //将字符读入到数组中
while((length!=-1)) {
System.out.print(ch); //输出读到的字符
length=fr.read()
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally{
try {
if(fr!=null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader实例:
import java.io.*;
public static void main(String[] args) {
FileReader fr=null;
BufferedReader br=null;
try {
fr=new FileReader("D:\\project\\hello.txt");
br=new BufferedReader(fr);
String line=br.readLine(); //读取一行数据
while(line!=null) { //判断是否有值
System.out.println(line);
line=br.readLine();
}
} catch (IOException e) {
System.out.println("文件不存在");
}finally {
try {
if(br!=null) {
br.close();
}
if(fr!=null) {
fr.close();
}
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
四:取文件
基类(抽象类):OutputStream,Writer
子类:FileOutputStream,FileWriter,BufferedWriter
FileOutputStream实例:
import java.io.*;
public static void main(String[] args) throws Exception {
FileOutputStream fos=null;
try {
String str="好好学习";
byte[] words=str.getBytes(); //转化为字节数组
fos=new FileOutputStream("D:\\project\\Hello.txt");
fos.write(words,0,words.length);
//把数组写入txt,0是开始下标,长度是写入的个数
System.out.println("hello文件已更新");
} catch (IOException e) {
System.out.println("创建文件时报错");
}finally{
try {
if(fos!=null) {
fos.close();
}
} catch (IOException e2) {
e2.printStackTrace();
}
}
FileWriter实例:
import java.io.*;
public static void main(String[] args) {
Writer fw=null;
try {
fw=new FileWriter("D:\\project\\hello.txt");
fw.write("我爱我的团队");
fw.flush(); //刷新缓冲区
System.out.println("写入成功!");
} catch (IOException e) {
System.out.println("文件不存在");
}finally {
try {
if(fw!=null) {
fw.close();
}
} catch (IOException e2) {
e2.printStackTrace();
}
}
【注意:如果没有flush方法和关闭了流的操作,内容则不会写入到txt中,要想写入成功,则必须有flush方法或关闭了流操作】
BufferedWriter实例:
import java.io.*;
public static void main(String[] args) {
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter("D:\\project\\hello.txt");
bw=new BufferedWriter(fw);
bw.write("大家好");
bw.newLine(); //换行
bw.write("请多多指教");
bw.flush(); //刷新
}catch (IOException e) {
System.out.println("文件不存在");
}finally {
try {
if(fw!=null) {
br.close();
}
if(bw!=null) {
fr.close();
}
} catch (IOException e2) {
e2.printStackTrace();
}
}
}