File类:
File类中的java.io包是唯一代表磁盘文件本身对象。
创建File对象:
File file = new File("D:\aa\hello.txt");
获取文件的名字和路径:
public static void main(String[] args) {
File file = new File("\\aa\\hello.txt");
//获取绝对路径
String absolutePath = file.getAbsolutePath();
System.out.println("absolutePath = " + absolutePath);
//获取创建时传入的路径
String path = file.getPath();
System.out.println("path = " + path);
//获取file对象的文件名/目录(重要)
String name1 = file.getName();
System.out.println("name1 = " + name1);
//获取file大大小,当file大了之后,这里和电脑显示的会有误差
long length = file.length();
System.out.println("length = " + length);
}
判断文件:
public static void main(String[] args) {
File file = new File("\\aa\\hello.txt");
//1,判断该文件是否存在
boolean exists = file.exists();
System.out.println("exists = " + exists);
//2.判断file文件是不是文件夹
boolean directory = file.isDirectory();
System.out.println("directory = " + directory);
//3.判断该file文件是不是文件
boolean file1 = file.isFile();
System.out.println("file1 = " + file1);
}
创建文件和删除文件:
public static void main(String[] args) throws IOException {
File file = new File("\\aa\\bb\\cc");
//1.创建文件,如果不存在就创建出来,返回true,否则就不管,返回false
boolean newFile = file.createNewFile();
System.out.println("newFile = " + newFile);
//2.删除文件/文件夹,如果要删除文件夹只能删除空文件夹,删除文件夹先删除里面的文件,再删除文件夹。
boolean delete = file.delete();
System.out.println("delete = " + delete);
//3.创建单级文件夹
boolean mkdir = file.mkdir();
System.out.println("mkdir = " + mkdir);
//创建多级文件夹
boolean mkdirs = file.mkdirs();
System.out.println("mkdirs = " + mkdirs);
}
对文件夹进行遍历:
public static void main(String[] args) {
File file = new File("D:\\aa");
//得到文件夹中所有内容的名字
String[] list = file.list();
for (String s : list) {
System.out.println("s = " + s);
}
System.out.println("------------------");
//得到文件夹中所有内容的file对象
File[] files = file.listFiles();
for (File file1 : files) {
System.out.println("file1 = " + file1);
}
}
IO流顾名思义输入输出流,输入(I)就是往磁盘中写数据,输出(O)就是读取磁盘数据
字节流:
字节输出流:
字节输出流(往内存向磁盘写数据)
public static void main(String[] args) throws IOException {
//1.创建字节输出流
FileOutputStream out = new FileOutputStream("D:\\aa\\hello.txt");
//2.写入数据
//2.1写入单个数据
out.write(89);
//2.2写入整个字节数组
byte[] arr = {1,2,3};
//out.write(arr);
//2.3写入部分数组
out.write(arr,0,1);
//3.刷新流对象
out.flush();
//4.关流
out.close();
}
字节输入流:
读取数据
public static void main(String[] args) throws IOException {
//创建字节输入流
FileInputStream in = new FileInputStream("D:\\aa\\hello.txt");
//读取信息
//一次读一个字节
/*int read = in.read();
System.out.println("read = " + read);*/
//一次读取多个字节
byte[] arr = new byte[5];//创建一个数组,充当拖车
int read = in.read(arr);//读取数组
System.out.println(Arrays.toString(arr));
System.out.println("read = " + read);
//关流
in.close();
}
使用循环读取数据:
//使用循环读取数据
public class demo3 {
public static void main(String[] args) throws Exception{
FileInputStream in = new FileInputStream("D:\\aa\\hello.txt");
//读取信息
int read;//定义变量,代表一次读取几个字节
byte[] arr = new byte[5];
//使用while循环,完成读取完毕
while ((read = in.read(arr)) != -1){
String s = new String(arr, 0, read);
System.out.println(s);
}
//关流
in.close();
}
}
对文件进行复制:
//文件的复制
public class demo4 {
public static void main(String[] args) throws IOException {
//读取,输入流
FileInputStream in = new FileInputStream("D:\\aa\\b.txt");
//写入,输出流
FileOutputStream out = new FileOutputStream("D:\\aa\\a.txt");
//循环读取数据并写入
/*int xrc;
byte[] arr = new byte[5];
while ((xrc=in.read(arr))!=-1){
out.write(arr,0,xrc);
out.flush();
}
//关流,之前可以自动刷新流对象,可以省略out》flush();
in.close();
out.close();*/
//一个字节一个字节的复制,
int b;
while ((b = in.read()) != -1){
//b就是读取出来的值
out.write(b+1);
}
in.close();
out.close();
}
}
对文件进行加密:
//对文件进行加密
//+1是加密,解密就再复制一次-1就好了(还有文件)
public class demo5 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("D:\\aa\\aaa.jpg");
FileOutputStream out = new FileOutputStream("D:\\aa\\22356209-6.jpg");
int b;
while ((b = in.read()) != -1){
out.write(b-1);
}
in.close();
out.close();
}
}
字节输入缓冲流:
public class demo6 {
public static void main(String[] args) throws IOException {
//字节输入缓冲流
BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\aa\\x.txt"));
//字节输出缓冲流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\aa\\r.txt"));
//缓冲流主要是提高效率
int a;
while ((a = in.read())!=-1){
out.write(a+1);//加密是+1,解密是在循环一次-1
}
in.close();
out.close();
}
}
字符流:
字符流只能复制文本文档,其他都用字节流
字符输出流:
乱码的解决:
public class demo1 {
public static void main(String[] args) throws UnsupportedEncodingException {
String s = "中国";
//字符串-字节数组utf-8
byte[] arrUTF = s.getBytes("UTF-8");
System.out.println(Arrays.toString(arrUTF));
//GBK
byte[] arrGBK = s.getBytes("GBK");
System.out.println(Arrays.toString(arrGBK));
//乱码的出现,用不同的编码会出现乱码
//字节数组还原字符串
String s1 = new String(arrUTF, "UTF-8");
System.out.println("s1 = " + s1);
}
}
字符输出流;写文件
//字符输出流(写文件)
public class demo2 {
public static void main(String[] args) throws IOException {
//创建字符输出流,文件不存在就会自动创建,存在则会清空
//开启续写功能就在后面添加一个参数true
FileWriter in = new FileWriter("D:\\aa\\a.txt",true);
//写数据
//写入单个数据
in.write("99");
//写入一个数组
char[] arr = {'q','x','e'};//写入的含头不含尾
in.write(arr,0,1);//如果想全部写进去就不要在后面写参数,后面是根据索引写那些数据
//写入字符串
in.write("w中国人不偏中国人",0,4);//如果想全部写进去就不要在后面写参数,后面是根据索引写那些数据
//关流
in.close();
}
}
字符输入流:读文件
//字符输入流(读取)
public class demo3 {
public static void main(String[] args) throws IOException {
//1.创建字符输入流
FileReader out = new FileReader("D:\\aa\\a.txt");
//2.读取
//一次读取一个字符
/*int b;//装字符
while ((b=out.read())!=-1){
System.out.println((char)b);
}*/
//一次读取多个字符
char[] arr = new char[5];
int len;//代表读取的个数
while ((len=out.read(arr))!=-1){
String s = new String(arr,0,len);
System.out.println("s = " + s);
}
//3.关流
out.close();
}
}
字符流复制文本:
//使用字符流完成文件复制
//字符流只能复制文本文档,其他都用字节流
public class demo4 {
public static void main(String[] args) throws IOException {
//创建字符输出输入流
FileReader fileReader = new FileReader("D:\\aa\\a.txt");
FileWriter fileWriter = new FileWriter("D:\\aa\\c.txt",true);
//边读取边写入
int b;
while ((b = fileReader.read()) != -1){
fileWriter.write(b);
}
fileReader.close();
fileWriter.close();
}
}
字符缓冲流:
//重点
//字符缓冲流
public class demo5 {
public static void main(String[] args) throws IOException {
//字符缓冲输入流
// BufferedReader bufferedReader = new BufferedReader
// (new FileReader("D:\\aa\\a.txt"));
/*String s = bufferedReader.readLine();
System.out.println("s = " + s);
String s1 = bufferedReader.readLine();
System.out.println("s = " + s1);
String s2 = bufferedReader.readLine();
System.out.println("s = " + s2);
//没有对象读取时返回null
String s3 = bufferedReader.readLine();
System.out.println("s = " + s3);*/
/*String s;
while ((s=bufferedReader.readLine())!=null){
System.out.println(s);
}
//关流
bufferedReader.close();*/
//字符缓冲输出流
BufferedWriter bufferedWriter = new BufferedWriter
(new FileWriter("D:\\aa\\a.txt",true));//true是开启续写,是从最后一行开始续写
bufferedWriter.write("11");
//换行
bufferedWriter.newLine();
bufferedWriter.write("21");
bufferedWriter.close();
}
}