一、File类:与文件本身操作有关的类,File类即可描述文件,也可描述文件夹
文件本身操作:创建、删除、取得信息(文件大小等等文件本身的信息),文件本身操作与文件内容无关
1、文件基本操作步骤:
1)取得File类对象:File类构造方法:public File(String path);
2) 判断文件是否存在:file.exists();
3)判断当前file对象是否为文件:file.isFile();
4)删除文件:delete();
5)创建文件:file.createNewFile();
public class Test {
public static void main(String[] args) {
//一、文件操作
//1.取得File对象
File file = new File("D:"+File.separator+"My things"+File.separator+"test.txt");
//判断文件是否存在,判断是否为文件
if(file.exists() && file.isFile()) {
//删除
file.delete();
}else {
//2.创建文件
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
二、字节流与字符流
1、字节流:OutputStream、InputStream,是原生操作
OutputStream out = new FileOutputStream(file);
向上转型,FileOutputStream类直接继承OutputStream抽象类,
2、字符流:Reader、Writer,是经过处理后的操作
Writer out = new FileWriter(file);
向上转型,FileWriter先继承OutputStreamWriter类(也就是转换流:字节流->字符流),然后OutputStream类再继承Writer类
转换流OutputStreamWriter类提供了接收OutputStream类的方法,用来进行流转换。
所以,在字符流输入输出,会有转换这一过程。
3、流的操作模式:
1)根据文件路径取得File对象
2)取得File对象的输入输出流(即,拿到管道)
3)通过输入输出流传输数据
4)关闭流
4、使用字节流进行文件拷贝
public class Copy {
public static void main(String[] args) throws Exception {
String sPath = "图片.jpg";
String dPath = "图片1.jpg";
System.out.println(fileCopy(sPath, dPath)?"拷贝成功":"拷贝失败");
}
private static boolean fileCopy(String sPath, String dPath) throws Exception {
//通过文件路径取得File对象
File file = new File(sPath);
File file1 = new File(dPath);
//取得File对象的输入输出流(拿到管道)
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(file1);
return fileCopyHandle(in, out);
}
private static boolean fileCopyHandle(InputStream in, OutputStream out) throws Exception {
int len = 0;
byte[] res = new byte[1024*1024];
long start = System.currentTimeMillis();
//通过输入输出流传输数据
//一次读取1MB
while((len = in.read(res)) != -1) {
out.write(res);
}
long end = System.currentTimeMillis();
System.out.println("消耗时间: "+(end-start));
//关闭流
in.close();
out.close();
return true;
}
}
三、打印流与Scanner类的应用
练习:
要求:在桌面上新建一个Test.txt,使用打印流向文件中输出如下:
Hello 123
hello bit
然后使用Scanner类读取文件内容并输入到控制台。
public class Test3 {
public static void main(String[] args) throws Exception {
File file = new File("C:"+File.separator+"Users"+File.separator+
"zhangmeng"+File.separator+"Desktop"+File.separator+"Test.txt");
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.println("Hello 123");
pw.println("hello bit");
pw.close();
Scanner scanner = new Scanner(new FileInputStream(file));
//scanner.useDelimiter("\r\n");
while(scanner.hasNext()) {
System.out.println(scanner.nextLine());//默认以回车换行作为分隔符
}
scanner.close();
}
}
运行结果:在桌面创建了Test.txt文件,并在控制台显示了Sacnner的输入结果
四、序列化与反序列化
1、序列化:将内存中的对象变为二进制流的形式在网络传输或者保存在文件中
2、实现序列化:实现java.io.Serializable接口(实质为标记接口,Java会自动识别此类自动实例化)
3、操作:
将对象反序列化输入:ObjectInputStream:readObject();
将对象序列化输出:ObjectOutPutStream:writeObject(Object obj);
4、应用:要求自定义Person类,其中三个属性name,age,school。age属性不作为序列化保存而其他两个属性使用序列化保存在本地文件TestSer.txt中。
class Person implements Serializable{
private String name;
private transient int age;//保证age不被序列化
private String school;
public Person(String name, int age, String school) {
super();
this.name = name;
this.age = age;
this.school = school;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", school=" + school + "]";
}
}
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("TestSer.txt");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
oos.writeObject(new Person("张三", 20, "西工大"));
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
System.out.println(ois.readObject());
ois.close();
}
}
运行结果:创建的文件打开乱码。
五、算法题:
输入一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()) {
String str = scanner.nextLine();
if(judge(str)) {
System.out.println("是");
}else {
System.out.println("否");
}
}
}
private static boolean judge(String str) {
char number[] = str.toCharArray();
for(int i = 0; i < number.length/2; i++) {
if(number[i] != number[str.length() -1 - i]) {
return false;
}
}
return true;
}
}