File类
File类简介
1.File类的一个对象,代表一个文件或者一个文件夹
2.File类声明在java.io包下
File类的使用
预备知识
相对路径:相较于某个路径下,指明的路径。
绝对路径:包含盘符在内的文件或文件目录的路径。
不同系统中路径分隔符表示不同:
win 使用 "\" 表示
unix和url 使用 "/" 表示
为了方便统一,java提供了一个常量 public static final String separator
例:File file = new File("d:"+File.separator+"hello.txt");
File类中只涉及了到关于文件或者文件目录的创建、删除、重命名等方法,并未涉及到对文件内容的操作。对文件内容的具体操作,必须使用IO流来完成。
File类的实例化
//常用构造器1
File(String filePath)
//例:
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
//常用构造器2
File(String parentPath,String childPath)
//例:
File file = new File("E:\\程序代码\\JavaIO","hello.txt");
//常用构造器3
File(File parentPath,String childPath)
//例:
File file1 = new File("E:\\程序代码\\JavaIO");
File file2 = new File(file1,"hello.txt");
获取文件信息
获取绝对路径
public String getAbsolutePath()
//案例
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
System.out.println(file1.getAbsolutePath());
System.out.println(file2.getAbsolutePath());
获取路径
public String getPath()
//案例
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
System.out.println(file1.getPath());
System.out.println(file2.getPath());
获取文件名称
public String getName()
//案例
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
System.out.println(file1.getName());
System.out.println(file2.getName());
获取上层目录路径
public String getParent()
//案例
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
System.out.println(file1.getParent());
System.out.println(file2.getParent());
获取文件大小
public String length()
//案例 以字节为单位
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
System.out.println(file1.length());
System.out.println(file2.length());
获取最后一次文件修改时间
public String lastModified()
//案例 以毫秒为单位
File file1 = new File("hello.txt");
File file2 = new File("E:\\程序代码\\JavaIO\\hello.txt");
System.out.println(new Date(file1.lastModified());
System.out.println(new Date(file2.lastModified()));
获取指定目录下所有文件或文件目录的名称(String 数组)
public String[] list()
//案例
File file = new File("E:\\程序代码\\JavaIO");
System.out.println(Arrays.toString(file3.list()));
获取指定目录下所有文件或文件目录的名称(File 数组)
public String[] listFiles()
//案例
File file = new File("E:\\程序代码\\JavaIO");
System.out.println(Arrays.toString(file3.list()));
判断文件信息
是否是文件目录
public boolean isDirectory()
//案例
File file = new File("hello.txt");
System.out.println(file2.isDrectory());
是否是文件
public boolean isFile()
//案例
File file = new File("hello.txt");
System.out.println(file2.isFile());
文件是否存在
public boolean exists()
//案例
File file = new File("hello.txt");
System.out.println(file2.exists());
文件是否可读
public boolean canRead()
//案例
File file = new File("hello.txt");
System.out.println(file2.canRead());
文件是否可写
public boolean canWrite()
//案例
File file = new File("hello.txt");
System.out.println(file2.canWrite());
文件是否隐藏
public boolean isHidden()
//案例
File file = new File("hello.txt");
System.out.println(file2.isHidden());
创建修改文件
创建文件
public boolean createNewFile() //若文件存在,则不创建,返回false
//案例
File file = new File("hello.txt");
if(!file.exists()){
file.createNewFile();
System.out.println("文件创建成功");
}else{
System.out.println("文件已经存在");
}
创建文件目录
public boolean mkdir() //若上层目录不存在则不创建
public boolean mkdirs() //若上层目录不存在则一并创建
//案例
File file = new File("\\123\\456");
File file2 = new File("\\123\\789");
file.mkdirs();
file.mkdir();
删除文件
public boolean delete()
//案例
File file = new File("\\123");
file.delete();
移动文件
public boolean renameTo(File dest) //可以移动重命名文件 linux中的mv
//案例
File file = new File("hello.txt"); //原路径
File path = new File("hellocp.txt"); //目标路径
file.renameTo(path);
综合案例
//遍历指定目录下所有的文件信息
public class FilePractice {
public static void main(String[] args) {
System.out.print("请输入文件路径:");
Scanner input = new Scanner(System.in);
String path = input.next();
// System.out.println(path);
File file = new File(path);
// System.out.println(file.getName());
if (file.exists()){
ergodicPath(file);
}else {
System.out.println("您输入的路径不存在。");
}
}
private static void ergodicPath(File file) {
if(file.isDirectory()){
System.out.println(file.getAbsolutePath());
for (File listFile : file.listFiles()) {
ergodicPath(listFile);
}
}else {
System.out.println(file.getAbsolutePath());
}
}
IO流
预备知识
I/O是input/output的缩写,I/O技术是非常实用的技术,用于处理设备之间的数据传输。
Java程序中,对应数据输入/输出操作以"流(stream)"流。
java.io包下提供了各种"流"类和接口,用以获取不同种类的数据,并通过标准end方法输入或输出数据。
##流的分类
按数据单位不同:字节流(8 bit),字符流(16 bit)
按数据流向不同:输入流,输出流
按流的角色不同:节点流,处理流
##抽象基类
输入流:InputStream(字节流),Reader(字符流)
输出流:OutputStream(字节流),Writer(字符流)
字符流
注:字符流只能用来处理文本文件(.txt,.java,.c …),,非文本文件使用字节流处理。
FileReader
@Test
public void testFileReader(){
FileReader fr = null;
try{
File file = new File("hello.txt");
//方式一 一次读入单个
fr = new FileReader(file);
int data;
//read() : 返回读入的一个字符,达到文件末尾,返回1
while((data = fr.read()) != -1)
{
System.out.printf("%c",data);
}
System.out.printf("\n");
//方式二 一次读入多个
fr = new FileReader(file);
int len;
char[] cf = new char[5];
//read(char[] cbuf) : 每次返回读入cbuf数组中字符的个数,达到文件末尾,返回1
while((len= fr.read(cf)) != -1)
{
for(int i=0;i<len;i++){
System.out.printf("%c",cf[i]);
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
//为了保证流一定可以被关闭,需要使用 trt-catch-finally 处理
try{
if(fr != null)
fr.close();
}catch(IOExcrption e){
e.printStackTrace();
}
}
}
//注:如果报FileNotFoundException 异常说明指定路径文件不存在
FileWriter
@Test
public void FileWriterTest(){
FileWriter fw = null;
try{
File file = new File("hello.txt");
//设置写入模式 默认为false,若文件存在则覆盖原文件 true 在原有的文件基础上追加数据
fw = new FileWriter(file);
//若目标文件不存在则创建新的文件
fw.write("hello ll");
//在原有文件上追加数据
fw.append("ssss");
}catch(IOException e){
e.printStackTrace();
}finally{
if (fw != null){
try{
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
综合案例-文本文件的复制
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class PracticeTest1 {
public static void main(String[] args) {
FileReader fr = null;
FileWriter fw = null;
try {
Scanner input = new Scanner(System.in);
System.out.print("输入原文件的路径:");
String OriginalPath = input.next();
File file = new File(OriginalPath);
if(file.exists()){
fr = new FileReader(file);
}else {
System.out.println("文件不存在");
throw new RuntimeException("文件不存在");
}
System.out.print("输入目标的路径:");
String TargetPath = input.next();
fw = new FileWriter(new File(TargetPath));
char[] t = new char[10];
String data = "";
int len=0;
while ((len = fr.read(t)) != -1){
for (int i=0;i<len;i++){
data += t[i] + "";
}
}
System.out.println(data);
fw.write(data);
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if(fr != null)
fr.close();
if(fw != null)
fr.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
字节流
InputStream
OutputStream
综合案例-复制文件
package IOTest;
import org.junit.jupiter.api.Test;
import java.io.*;
/**
* @Author bujiasisuo
* @Date 2020/9/22 10:34
* @Version 1.0
*/
public class PracticeTest1 {
public static void main(String[] args) {
FileInputStream ins = null;
FileOutputStream inw = null;
try{
File file = new File("123.docx");
ins = new FileInputStream(file);
inw = new FileOutputStream(new File("d:\\123.docx"),true);
byte[] data = new byte[100];
int len;
while ((len = ins.read(data)) != -1){
inw.write(data,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (ins != null)
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (inw != null)
inw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
处理流-缓冲流
注:缓冲流是作用在流上的流 ,用于提高读写速度,原因是内部提供了一个缓冲区域。
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
综合案例-复制文件
package IOTest;
import java.io.*;
/**
* @Author bujiasisuo
* @Date 2020/9/22 19:47
* @Version 1.0
* 缓冲流的使用 一种做用在流上的流
*/
public class BufferedTest {
public static void main(String[] args) {
FileInputStream fs = null;
FileOutputStream fw = null;
BufferedInputStream bis =null;
BufferedOutputStream bos = null;
File file1 = new File("123.docx");
File file2 = new File("d:\\123.docx");
try {
fs = new FileInputStream(file1);
fw = new FileOutputStream(file2);
bis = new BufferedInputStream(fs);
bos = new BufferedOutputStream(fw);
byte[] data = new byte[100];
int len;
while ((len = bis.read(data)) != -1)
{
bos.write(data,0,len);
bos.flush(); //刷新缓冲区
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//注:关闭外层流的同时会自动关闭对应内层流
try {
if(bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
处理流-转换流
注:转换流提供了在字节流和字符流之间的转换
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter: 将一个字符的输出流转换为字节的输出流
解码:字节、字节数组 —> 字符数组、字符串
编码:字符数组、字符串 —> 字节、字节数组
package IOTest;
import java.io.*;
/**
* @Author bujiasisuo
* @Date 2020/9/22 20:29
* @Version 1.0
* 转换流
* 转换流提供了在字节流和字符流之间的转换
*/
public class
StreamReaderTest {
public static void main(String[] args) {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try{
File file1 = new File("hello.txt");
File file2 = new File("d:\\hello.txt");
FileInputStream fs = new FileInputStream(file1);
FileOutputStream fw = new FileOutputStream(file2);
isr = new InputStreamReader(fs,"utf-8");
osw = new OutputStreamWriter(fw,"utf-8");
char[] data = new char[5];
int len;
while ((len = isr.read(data)) != -1){
osw.write(data,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (osw != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (osw != null)
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
其他流
标准输入、输出流
System.in:标准的输入流,默认从键盘输入
System.out:标准的输出流,默认从控制台输出
注:可以通过System类的setIn(InputStream is)/setOut(PrintStream ps )的方式重新指定输入输出流
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
* @Author bujiasiuo
* @Date 2020/9/23 9:18
* @Version 1.0
* 仿Scanner 功能
*/
public class SystemInTest {
public static void main(String[] args) {
MyInput input = new MyInput(System.in);
String ts = input.readString();
int t = input.readInt();
System.out.println(t);
}
}
class MyInput{
private InputStreamReader isr = null;
public MyInput(InputStream isr){
this.isr = new InputStreamReader(isr);
}
//读取字符串
public String readString(){
String data = "";
BufferedReader bdr = new BufferedReader(isr);
try {
data = bdr.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
//读取整数
public int readInt(){
int data;
String t = "";
BufferedReader bdr = new BufferedReader(isr);
try {
t = bdr.readLine();
// System.out.println(t);
} catch (IOException e) {
e.printStackTrace();
}
data = Integer.valueOf(t);
return data;
}
}
字符集
##常见的编码表
ASCII:美国标准信息交换表。用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表,用一个字节8位表示。
GB2312:中国的中文编码表。最多支持两个字节编码所有字符。
GBK:中国的中文编码表升级版,加入了更多的中文字符,最多两个字节编码。
Unicode:国际标准码,融入了目前人类对的所有字符。每一个字符对应唯一的字符码。两个字节表示。
UTF-8:变长的编码方式,可用1-4个字节表示所有的字符。
##注:
1.GB2312 和 GBK 通过首位判断当前字符是占用1个还是2个字符。0表示1个,1表示2个。
2.Unicode并不特别完美,存在一定的问题如:
英文符号只占用一个字节,如何区分Unicode和ascii?
如果使用最高位区分占用1个字节还是2个字节,就会少很多值无法显示。
3.UTF-8就是每次8个位传输数据,UTF-16就是每次16个位。
4.Unicode只是定义了一个字符集,规定了每个字符对应的编号,具体存储长成什么样子取决于编码方案。(UTF-8,UTF-16)