1.File文件的增删查
1.1文件的创建:
File 变量名= new File("路径");
creatNewFile() 创建新文件,成功返回true,失败返回false
exists() 判断文件是否存在,返回值为布尔类型
//File对文件进行操作
File file = new File("E:/kj10/file001.txt");
// createNewFile() 创建新文件,返回值为boolean
try { // 添加try块或者throws声明异常
// exists() 判断文件是否存在
if (file.exists()) {
System.out.println("文件已经存在,不能继续创建");
} else {
boolean flag = file.createNewFile();
if (flag) {
System.out.println("创建文件成功");
} else {
System.out.println("创建文件失败");
}
}
} catch (IOException e) {
e.printStackTrace();
}
1.2文件的查询
isFile() // 判断是文件吗 true
isDirectory() // 判断是文件夹吗 false
getPath() // 获取相对路径 E:\kj10\file001.txt
getAbsolutePath() // 获取绝对路径 E:\kj10\file001.txt
getName() // 获取文件名 file001.txt
length() // 获取文件长度,字节 0
1.3文件的删除
// 删除方法
delete() // 返回值为布尔类型
public void delFile(File file) {
if (file.exists()) {
boolean flag = file.delete();
if (flag) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} else {
System.out.println("文件不存在");
}
}
2.流
2.1流的分类
按照流向分类:
输入InputStream和Reader作为基类
输出OutputStream和Writer作为基类
按照处理数据单元分类:
字节流:输入:InputStream作为基类
输出:OutputStream作为基类
字符流:输入:Reader作为基类
输出:Writer作为基类
3.FileInputStream字节输入流实现数据的读取:
需要导入FileInputStream包,首先实例化对象:FileInputStream fileInputStream = fileInputStream = new FileInputStream("E:\\kj10\\file001.txt");
后面为文件的路径,然后调用FileInputStream的read()方法,当下面有值的时候,返回对应的ASCII码,最后的元素返回-1,添加循环,读出所有元素。
注意添加路径的时候需要抛出或者处理文件未找到异常,并且在最后finally种添加关闭流。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 利用FileInputStream实现数据的读取
*/
public class Demo4_Input {
public static void main(String[] args) {
// 1.首先定义一个FileInputStream的空变量(设置为全局变量是为了后续关闭字节输入流)
FileInputStream fileInputStream = null;
try {
// 2.实例化对象,添加文件路径(此时有可能产生异常,需要try-catch语句)
fileInputStream = new FileInputStream("E:\\kj10\\file001.txt");
// 3.定义一个变量,接收fis对象调用read()的返回值
int a = -1;
// 4.讲read()读取后的返回值(为元素的ASCII码)赋值给a,然后比较是否为-1,-1为文件读取结束的标识,并且此时会添加IOException
while ((a = fileInputStream.read()) != -1) {
// 5.将a代表的ASCII码转换成char类型,并输出
System.out.print((char) a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 6.在finally语句中添加关闭流的语句,此时需添加try-catch
try {
fileInputStream.close();
} catch (IOException e) {
}
}
}
}
4.FileInputStream字节输入流和FileOutputStream字节输出流实现复制
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 利用FileInputStream实现数据的读取
*/
public class Demo4_Input {
public static void main(String[] args) {
// 1.首先定义一个FileInputStream的空变量(设置为全局变量是为了后续关闭字节输入流)
FileInputStream fileInputStream = null;
try {
// 2.实例化对象,添加文件路径(此时有可能产生异常,需要try-catch语句)
fileInputStream = new FileInputStream("E:\\kj10\\file001.txt");
// 3.定义一个变量,接收fis对象调用read()的返回值
int a = -1;
// 4.讲read()读取后的返回值(为元素的ASCII码)赋值给a,然后比较是否为-1,-1为文件读取结束的标识,并且此时会添加IOException
while ((a = fileInputStream.read()) != -1) {
// 5.将a代表的ASCII码转换成char类型,并输出
System.out.print((char) a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 6.在finally语句中添加关闭流的语句,此时需添加try-catch
try {
fileInputStream.close();
} catch (IOException e) {
}
}
}
}
5.FileReader字符流实现读取
字节流是一个字节一个字节的读取,字符流是一个字符一个字符的读取(中文是两个字节)
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Demo4_Readerzijie2 {
public static void main(String[] args) {
// 1.定义Reader类型的reader变量
Reader reader = null;
try {
// 2.实例化FileReader对象,获取对应路径下的文件
reader = new FileReader("E:\\kj10\\demo1.txt");
// 3.循环遍历并输出(无数组形式)
int a = 0;
while ((a = reader.read()) != -1) {
System.out.print((char) a);
}
System.out.println();
// 3.循环遍历并输出(数组形式)
int b = 0; // 接收返回值的变量b
char[] c = new char[1024]; // 接收字符的数组
while ((b = reader.read(c)) != -1) {
for (int i = 0; i < b; i++) {
System.out.print(c[i]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭reader流对象
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
关于字符流中文乱码的问题:
将txt文档用记事本打开,把UTF-8的格式另存为ANSI并替换原文件,然后打开java工程或文件将文件的格式设置为GBK(不同环境的默认可能不同)
关于字节流中文输出乱码的问题:
将txt文档用记事本打开,把UTF-8的格式另存为ANSI并替换原文件,然后打开java工程或文件将文件的格式设置为GBK(不同环境的默认可能不同)
定义一个InputStreamReader类型的空变量isr,然后正常创建实例化字节流FileInputStream类型的fis对象,然后再实例化isr对象的时候引用fis对象并且改变为GBK格式
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo4_Readerzijie {
public static void main(String[] args) {
// 1.定义InputStreamReader类型的fr变量
InputStreamReader isr = null;
try {
// 2.实例化InputStreamReader对象,获取对应路径下的文件
// 字节流的转中文
FileInputStream fis = new FileInputStream("E:\\kj10\\demo1.txt");
isr = new InputStreamReader(fis, "GBK");
// 3.定义临时变量a,判断并读取
int a = -1;
while ((a = isr.read()) != -1) {
System.out.print((char) a);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭reader流对象
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.FileReader字符流和StringBuffer缓存区实现读取
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Demo4_Readerzijie2 {
public static void main(String[] args) {
// 1.定义Reader类型的reader变量
Reader reader = null;
try {
// 2.实例化FileReader对象,获取对应路径下的文件
reader = new FileReader("E:\\kj10\\demo1.txt");
// 3.循环遍历并输出
int b = 0; // 接收返回值的变量b
char[] c = new char[1024]; // 接收字符的数组
StringBuffer s = new StringBuffer(); // 接收字符数组的StringBuffer字符串
while ((b = reader.read(c)) != -1) {
s.append(c);// 添加元素
System.out.println(s.subSequence(0, b));// 输出的时候将后续的空格去掉
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭reader流对象
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
7.FileReader字符流加缓冲流BufferedReader实现读取
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
// 字符流读取+缓冲流(提高读取效率)
public class Demo1_BufferRead {
public static void main(String[] args) {
// 1.定义Reader字符读取流对象的变量
Reader fr = null;
// 2.定义BufferedReader字符缓冲流读取对象的变量
BufferedReader br = null;
try {
// 3.实例化字符读取对象
fr = new FileReader("E:\\kj10\\Demo1.txt");
// 4. 实例化BufferedReader字符缓冲读取流
br = new BufferedReader(fr);
// 5.定义字符串变量date
String date = null;
// 6.循环读取
// System.out.println(br.readLine());
while ((date = br.readLine()) != null) {
System.out.println(date);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
8.字符流FileWriter实现写出
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* 字符流输出
*
*/
public class Demo2_FileWriter {
public static void main(String[] args) {
// 1.定义一个Writer类型的fw变量
Writer fw = null;
try {
// 2.实例化FileWriter对象
fw = new FileWriter("E:\\kj10\\demo2.txt");
// 3.定义字符串变量,值为刘亦菲
String str = "刘亦菲";
// 4.调用fw对象的write()方法,将字符串输出到文本中
fw.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 5.close掉流对象(必须关闭,否则无法输出)
try {
fw.flush(); // 强制刷新,作用同close,但是一般还会加上close
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
9.字符流FileWriter加BufferedWriter实现写出
相当于在原有的FileWriter输出的管上添加更粗的管道BufferedWriter
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* 字符流输出+字符缓冲流(提高输出效率)
*
*/
public class Demo3_FileWriter_BufferWriter {
public static void main(String[] args) {
// 1.定义一个Writer类型的fw变量
Writer fw = null;
// 2.定义一个字符缓冲输出流变量
BufferedWriter bf = null;
try {
// 3.实例化FileWriter对象
fw = new FileWriter("E:\\kj10\\demo2.txt");
// 4.实例化BufferedWriter字符缓冲输出流,传入fw对象作为参数
bf = new BufferedWriter(fw);
// 5.定义字符串变量,值为杨紫
String str = "杨紫";
// 4.调用bf对象的write()方法,将字符串输出到文本中
bf.write(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 5.close掉流对象(必须关闭,否则无法输出)
try {
bf.close(); // 关闭的时候注意顺序,顺序错了无法输出
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
10.字符流实现复制
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 字符流实现复制功能
*
*/
public class Demo4_copy {
public static void main(String[] args) {
// 1.定义FileReader字符流读取对象
FileReader fr = null;
// 2.定义BufferedReader缓冲字符流读取对象
BufferedReader br = null;
// 3.定义FileWriter字符流写出对象
FileWriter fw = null;
// 4.定义BufferedWriter缓冲字符流写出对象
BufferedWriter bw = null;
try {
// 5.全部实例化,将fr,fw作为参数实例化br,bw
fr = new FileReader("E:\\kj10\\demo1.txt");
br = new BufferedReader(fr);
fw = new FileWriter("E:\\kj10\\demo3.txt");
bw = new BufferedWriter(fw);
// 6.定义字符串str变量
String str = null;
// 7.循环遍历读取流中的数据,返回值给str赋值,直接读取接收,返回值为null
while ((str = br.readLine()) != null) {
// 8.通过流的方式写出在字符串中
bw.write(str + "\r\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 9.关闭流(注意顺序)
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
11.复制二进制文件(图片视频)
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 利用DateInputStream实现图片或者视频的复制
*
*/
public class Demo5_copypicture {
public static void main(String[] args) {
FileInputStream fis = null;
DataInputStream dis = null;
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fis = new FileInputStream("E:\\kj10\\nn.jpg");
dis = new DataInputStream(fis);
fos = new FileOutputStream("E:\\kj10\\nn1.jpg");
dos = new DataOutputStream(fos);
// 第一种方式
int data = -1;
while ((data = dis.read()) != -1) {
dos.write(data);
}
// 第二种方式
int date = -1;
byte[] b = new byte[1024];
while ((date = dis.read(b)) != -1) {
dos.write(b, 0, date);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dos.close();
fos.close();
dis.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}