第一天
分隔符
public static void fun1() {
String separator = File.separator;
System.out.println(separator);
String pathseparator = File.pathSeparator;
System.out.println(pathseparator);
}
File类的三个构造方法
public static void fun2() {
File file = new File("/Users/lanou/Desktop/Test");
System.out.println(file); // 结果: /Users/lanou/Desktop/Test
System.out.println(file.exists()); // 结果:true
File file2 = new File("src");
System.out.println(file2.getAbsolutePath()); // /Users/lanou/eclipse-workspace/sh-day-复习六/src
System.out.println(file2.exists()); // 结果: true
String parent = "/Users/lanou/Desktop/";
String child = "test";
File file3 = new File(parent,child);
System.out.println(file3); // 结果:/Users/lanou/Desktop/test
System.out.println(file3.exists()); // 结果:true
File file4 = new File(new File(parent), child);
System.out.println(file4); // 结果:/Users/lanou/Desktop/test
System.out.println(file4.exists()); // 结果: true
}
创建文件 文件夹
public static void fun3() throws IOException {
File file = new File("/Users/lanou/Desktop/test3/kkk.txt");
file.createNewFile();
File file2 = new File("src/po1.txt");
file2.createNewFile();
File file3 = new File("/Users/lanou/Desktop/test3/kkbn");
file3.mkdir();
File file4 = new File("/Users/lanou/Desktop/test3/k/c/d/f");
file4.mkdirs();
}
删除空文件夹
public static void fun4() {
File file = new File("/Users/lanou/Desktop/test3/kkbn");
boolean delete = file.delete();
System.out.println(delete);
File file2 = new File("/Users/lanou/Desktop/test3/k");
boolean delete2 = file2.delete();
System.out.println(delete2);
}
判断是否是文件夹 是否是文件
public static void fun5() {
File file = new File("/Users/lanou/Desktop/test3");
boolean b = file.isDirectory();
boolean c = file.isFile();
System.out.println(b);
System.out.println(c);
}
获取文件信息
public static void fun6() {
File file = new File("/Users/lanou/Desktop/test3/kk.txt");
System.out.println(file.exists());
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getParent());
System.out.println(file.getParentFile());
}
获取文件下下的名字 路径
public static void fun7() {
File file = new File("/Users/lanou/Desktop/test3");
String[] list = file.list();
for (String string : list) {
System.out.println(string);
}
File[] listFiles = file.listFiles();
for (File file2 : listFiles) {
System.out.println(file2);
}
}
结果
.DS_Store
gbk.java
haha.java
k
kkk
kkk.txt
person.txt
poo.txt
ppp.txt
ppp6.txt
utf8.java
wwww.java
xzb.properties
znb.java
图7.png
/Users/lanou/Desktop/test3/.DS_Store
/Users/lanou/Desktop/test3/gbk.java
/Users/lanou/Desktop/test3/haha.java
/Users/lanou/Desktop/test3/k
/Users/lanou/Desktop/test3/kkk
/Users/lanou/Desktop/test3/kkk.txt
/Users/lanou/Desktop/test3/person.txt
/Users/lanou/Desktop/test3/poo.txt
/Users/lanou/Desktop/test3/ppp.txt
/Users/lanou/Desktop/test3/ppp6.txt
/Users/lanou/Desktop/test3/utf8.java
/Users/lanou/Desktop/test3/wwww.java
/Users/lanou/Desktop/test3/xzb.properties
/Users/lanou/Desktop/test3/znb.java
/Users/lanou/Desktop/test3/图7.png
获取file下的文件路径
public class Demo01 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3");
fun8(file);
}
public static void fun8(File file) {
File[] files = file.listFiles();
for (File file2 : files) {
if(file2.isFile()) {
System.out.println(file2);
}else {
fun8(file2);
}
}
}
结果:
/Users/lanou/Desktop/test3/.DS_Store
/Users/lanou/Desktop/test3/gbk.java
/Users/lanou/Desktop/test3/haha.java
/Users/lanou/Desktop/test3/k/.DS_Store
/Users/lanou/Desktop/test3/k/c/.DS_Store
/Users/lanou/Desktop/test3/kkk
/Users/lanou/Desktop/test3/kkk.txt
/Users/lanou/Desktop/test3/person.txt
/Users/lanou/Desktop/test3/poo.txt
/Users/lanou/Desktop/test3/ppp.txt
/Users/lanou/Desktop/test3/ppp6.txt
/Users/lanou/Desktop/test3/utf8.java
/Users/lanou/Desktop/test3/wwww.java
/Users/lanou/Desktop/test3/xzb.properties
/Users/lanou/Desktop/test3/znb.java
/Users/lanou/Desktop/test3/图7.png
父类 子类异常关系
package com.lanou3g.bean1;
public class Demo02 {
}
abstract class father{
public abstract void fun();
}
class son extends father{
@Override
public void fun(){
try {
throw new Exception();
} catch (Exception e) {
e.printStackTrace();
}
}
}
小型程序
/*
* 获取数字的工具类
* 要求:从键盘输入 输入的是字符串要处理异常重新输入
*
* 封装一个有两个数的对象类
* 要求:类中有两个int数字 作为成员变量
*
* 封装自定义异常类
* 要求:类名表示除法除数不能为0
*
* 封装运算类
* 1.获取 两个数的对象
* 2.获取 要做的运算法则 加法或者除法
* 输入数字1是加法 2是除法(输入错误 要重新输入)
* 3.加法方法
* 4.除法方法(要求捕获异常 并抛出自定义异常)
* 5.根据运算法则 执行运算 并返回运算结果
* 除数如果是0 要重新输入数字
*
* 测试类:
* 对以上方法进行测试
*/
package com.lanou3g.bean1;
import java.util.Scanner;
public class IntTools {
public static int getNumber() {
System.out.println("请输入一个整数 注意 是Int类型的数");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
try {
int num = Integer.parseInt(string);
return num;
} catch (Exception e) {
System.out.println("大哥 你输入错了");
return getNumber();
}
}
}
package com.lanou3g.bean1;
public class MyNum {
private int num1;
private int num2;
public MyNum() {
super();
}
public MyNum(int num1, int num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
public int getNum1() {
return num1;
}
public void setNum1(int num1) {
this.num1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
this.num2 = num2;
}
@Override
public String toString() {
return "MyNum [num1=" + num1 + ", num2=" + num2 + "]";
}
}
package com.lanou3g.bean1;
import java.util.Scanner;
import java.util.function.DoubleToLongFunction;
public class Operate {
public static MyNum getMyNum() {
int num1 = IntTools.getNumber();
int num2 = IntTools.getNumber();
MyNum myNum = new MyNum(num1, num2);
return myNum;
}
public static int getOperator() {
int i = 0;
System.out.println("请输入运算符 1 代表+ 2 代表/:");
i = IntTools.getNumber();
while(true) {
if(i == 1 || i == 2) {
break;
}
i = IntTools.getNumber();
System.out.println("输入错误 请重新输入");
}
return i;
}
public static int getAdd(MyNum myNum) {
return myNum.getNum1() + myNum.getNum2();
}
public static double getDiv(MyNum myNum) throws DivNotZeroException {
try {
double num = myNum.getNum1() / myNum.getNum2();
return num;
} catch (Exception e) {
throw new DivNotZeroException("除数为0了");
}
}
public static double getTotal(MyNum myNum,int num) {
double d = 0;
if(num == 1) {
d = getAdd(myNum);
}else {
try {
d = getDiv(myNum);
} catch (DivNotZeroException e) {
MyNum myNum2 = getMyNum();
getTotal(myNum2, num);
}
}
return d;
}
}
package com.lanou3g.bean1;
public class DivNotZeroException extends Exception{
public DivNotZeroException() {
super();
}
public DivNotZeroException(String message) {
super(message);
}
}
public class Test {
public static void main(String[] args) {
MyNum myNum = Operate.getMyNum();
int operator = Operate.getOperator();
double total = Operate.getTotal(myNum, operator);
System.out.println(total);
}
}
测试异常
package com.lanou3g.bean1;
public class Demo03 {
public static void main(String[] args) {
Animal animal = new Animal();
try {
double d = animal.getArea(-1);
System.out.println(d);
} catch (Exception e) {
e.printStackTrace();
System.out.println("半径不合格");
}
}
public static void fun2() {
Animal animal = new Animal();
try {
animal.fun1();
} catch (Exception e) {
System.out.println("运行时异常");
}
}
public static void fun1() {
Animal animal = new Animal();
try {
animal.fun();
} catch (Exception e) {
e.printStackTrace();
System.out.println("我是编译时异常");
}
}
}
class Animal{
public void fun() throws Exception {
throw new Exception();
}
public void fun1() {
throw new RuntimeException();
}
public double getArea(double r) throws Exception {
if(r <= 0) {
throw new Exception("半径应该大于0");
}
return 3.14*r*r;
}
}
判断 删除
package com.lanou3g.bean1
import java.io.File
import java.util.Scanner
public class Demo04 {
public static void main(String[] args) {
System.out.println("请输入文件路径")
Scanner scanner = new Scanner(System.in)
String line = scanner.nextLine()
File file = new File(line)
if(file.exists()) {
System.out.println(file.length())
boolean b = file.delete()
System.out.println(b)
}else {
System.out.println("文件路径不正确")
}
}
}
第二天
判断输入的是否是文件夹
public static File getFile() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入文件夹路径");
String path = scanner.nextLine();
File file = new File(path);
if(file.isDirectory()) {
return file;
}else {
return getFile();
}
}
从控制台输入一个字符串 该字符串是文件夹路径 计算这个文件夹的大小
public static Long getLength(File file) {
File[] files = file.listFiles();
long sum = 0;
for (File subFile : files) {
if(subFile.isFile()) {
sum = sum + subFile.length();
}else {
sum = sum + getLength(subFile);
}
}
return sum;
}
从控制台输入一个字符串 该字符串是文件夹路径 删除这个文件夹
public static void deleteFile(File file) {
File[] files = file.listFiles();
for (File file2 : files) {
if(file2.isFile()) {
file2.delete();
}else {
deleteFile(file2);
}
file.delete();
}
}
输入一个文件夹路径 按层级关系 打印
public class Demo02 {
public static void main(String[] args) {
printFile(new File("/Users/lanou/Desktop/test3"), 0);
}
public static void printFile(File file,int den) {
String string = "";
for(int i = 0; i < den; i++) {
string = string + "-";
}
File[] files = file.listFiles();
for (File subFile : files) {
if(!subFile.isHidden()) {
System.out.println(string + subFile.getName());
if(subFile.isDirectory()) {
printFile(subFile, den + 2);
}
}
}
}
}
输入一个文件夹路径 用map记录类型出现的次数
package com.lanou3g.bean2;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Demo03 {
public static void main(String[] args) {
HashMap<String, Integer> hashMap = getHashMap(new File("/Users/lanou/Desktop/test3"));
Set<String> keySet = hashMap.keySet();
for (String string : keySet) {
System.out.println(string + " " + hashMap.get(string));
}
}
public static HashMap<String, Integer> getHashMap(File file){
HashMap<String, Integer> hashMap = new HashMap<>();
File[] files = file.listFiles();
for (File subFile : files) {
if(subFile.isFile()) {
String path = subFile.getAbsolutePath();
int lastIndexOf = path.lastIndexOf(".");
String string = path.substring(lastIndexOf + 1,path.length());
if(hashMap.containsKey(string)) {
Integer integer = hashMap.get(string);
integer++;
hashMap.put(string, integer);
}else {
hashMap.put(string, 1);
}
}else {
getHashMap(subFile);
}
}
return hashMap;
}
}
文件过滤器(是个接口) 需求:通过过滤器 遍历打印文件夹中的所有txt文件
package com.lanou3g.bean2;
import java.io.File;
import java.io.FileFilter;
public class Demo04 {
public static void main(String[] args) {
File file = new File("/Users/lanou/Desktop/test3");
fun1(file);
}
public static void fun1(File file) {
File[] listFiles = file.listFiles(new MyFileFilter());
for (File subFile : listFiles) {
if(subFile.isFile()) {
System.out.println(subFile.getName());
}else {
fun1(subFile);
}
}
}
}
class MyFileFilter implements FileFilter{
@Override
public boolean accept(File file) {
if(file.isDirectory()) {
return true;
}
return file.getName().endsWith("txt");
}
}
单字节读取文件
public class Demo05 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3/kkk.txt");
FileInputStream fis = new FileInputStream(file);
int len = 0;
while((len = fis.read()) != -1) {
System.out.print((char)len);
}
fis.close();
}
}
字符数组读取
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3/kkk.txt");
FileInputStream fis = new FileInputStream(file);
int len = 0;
byte[] bs = new byte[1024];
while((len = fis.read(bs)) != -1) {
System.out.println(new String(bs, 0, len));
}
}
简单写入
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3/liurong.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(53);
fos.write(100);
byte[] bs = new byte[]{5,4,88,66};
fos.write(bs);
fos.write(bs,1,2);
fos.write("哈哈哈".getBytes());
fos.close();
}
文件续写
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3/liurong.txt");
FileOutputStream fos = new FileOutputStream(file,true);
fos.write("大中华".getBytes());
fos.close();
}
文件写入异常处理
public static void main(String[] args) {
FileOutputStream fos = null;
try {
File file = new File("/Users/lanou/Desktop/test3/liurong.txt");
fos = new FileOutputStream(file,true);
fos.write("我爱中国".getBytes());
} catch (FileNotFoundException e) {
throw new RuntimeException("找不到文件");
} catch (IOException e) {
throw new RuntimeException("文件写入失败");
}finally{
if(fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("文件关闭失败");
}
}
}
}
第三天
复制文件夹的方法
package com.lanou3g.bean3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/test3");
File file2 = new File("/Users/lanou/Desktop/Test2");
copyFile(file1, file2);
}
public static void copyFile(File file1, File file2) throws IOException {
File file3 = new File(file2.getPath(),file1.getName());
file3.mkdir();
FileInputStream fis = null;
FileOutputStream fos = null;
File[] files = file1.listFiles();
for (File subFile : files) {
if(subFile.isFile()) {
if(!subFile.isHidden()) {
File newFile = new File(file3,subFile.getName());
newFile.createNewFile();
fis = new FileInputStream(subFile);
fos = new FileOutputStream(newFile);
Integer len = 0;
while((len = fis.read()) != -1) {
fos.write(len);
}
}
}else {
copyFile(subFile, file3);
}
}
try {
fis.close();
} catch (Exception e) {
}finally {
try {
} catch (Exception e2) {
fos.close();
}
}
}
}
将一个文件夹下的所有txt文件 复制到目标文件夹下
package com.lanou3g.bean3;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo02 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3");
File file2 = new File("/Users/lanou/Desktop/level/dfdf");
copyTxtFile(file, file2);
}
public static void copyTxtFile(File src, File dest) throws IOException {
File[] files = src.listFiles(new MyFileFilter());
FileInputStream fis = null;
FileOutputStream fos = null;
for (File subFile : files) {
if(subFile.isFile()) {
File newFile = new File(dest, subFile.getName());
fis = new FileInputStream(subFile);
fos = new FileOutputStream(newFile);
int len = 0;
while((len = fis.read()) != -1) {
fos.write(len);
}
try {
fis.close();
} catch (Exception e) {
throw new RuntimeException("fis关闭失败");
}finally {
try {
fos.close();
} catch (Exception e2) {
throw new RuntimeException("fos关闭失败");
}
}
}else {
copyTxtFile(subFile, dest);
}
}
}
}
class MyFileFilter implements FileFilter{
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory()) {
return true;
}
return pathname.getName().endsWith("txt");
}
}
字符流写入
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("/Users/lanou/Desktop/test3/qwert.txt")
fw.write(100)
fw.flush()
fw.write("d")
fw.flush()
fw.write('c')
fw.flush()
char[] c = new char[] {'2','7','1','3'}
fw.write(c, 0, 2)
fw.flush()
fw.write("床前明月光")
fw.flush()
fw.close()
}
结果:
ddc27床前明月光
循环读取(两种)
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test3/ppp6.txt");
int len = 0;
while((len = fr.read()) != -1) {
System.out.println((char)len);
}
fr.close();
}
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test3/ppp6.txt");
int len = 0;
char[] c = new char[1024];
while((len = fr.read(c)) != -1) {
System.out.println(new String(c, 0, len));
}
fr.close();
}
利用字符流 复制文件 带异常处理
public static void main(String[] args) {
File src = new File("/Users/lanou/Desktop/test3/ppp6.txt");
File dest = new File("/Users/lanou/Desktop/test3/ppp7.txt");
copyByChar(src, dest);
}
public static void copyByChar(File src, File dest) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(src);
fw = new FileWriter(dest);
int len = 0;
while((len = fr.read()) != -1) {
fw.write(len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件链接异常");
} catch (IOException e) {
throw new RuntimeException("文件IO异常");
}finally {
try {
if(fr != null) {
fr.close();
}
} catch (IOException e) {
throw new RuntimeException("fis关闭失败");
}finally {
try {
fw.close();
} catch (Exception e2) {
throw new RuntimeException("fos关闭失败");
}
}
}
}
public static void main(String[] args) {
File src = new File("/Users/lanou/Desktop/test3/ppp6.txt");
File dest = new File("/Users/lanou/Desktop/test3/ppp7.txt");
copyByChar(src, dest);
}
public static void copyByChar(File src, File dest) {
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(src);
fw = new FileWriter(dest);
int len = 0;
char[] c = new char[1024];
while((len = fr.read(c)) != -1) {
fw.write(c);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件链接异常");
} catch (IOException e) {
throw new RuntimeException("文件IO异常");
}finally {
try {
if(fr != null) {
fr.close();
}
} catch (IOException e) {
throw new RuntimeException("fis关闭失败");
}finally {
try {
fw.close();
} catch (Exception e2) {
throw new RuntimeException("fos关闭失败");
}
}
}
}
使用转换流使用GBK编码写入文件
public static void inputByGBK(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
osw.write("疑似银河落九天");
osw.flush();
osw.close();
}
利用转换流写文件OutputStreamWriter 默认UTF-8写的
public static void inputByUTF(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osr = new OutputStreamWriter(fos,"UTF-8");
osr.write("小荷才漏尖尖角");
osr.close();
}
利用转换流读文件 使用GBK
public static void outputByGBK(File file) throws IOException {
FileInputStream fos = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fos,"GBK");
int len = 0;
char[] cs = new char[1024];
while((len = isr.read(cs)) != -1) {
System.out.println(new String(cs, 0, len));
}
isr.close();
}
利用转换流读文件 默认UTF-8
public static void outputByUTF(File file) throws IOException {
FileInputStream fos = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fos,"UTF-8");
int len = 0;
while((len = isr.read()) != -1) {
System.out.print((char)len);
}
isr.close();
}
测试结果
public static void main(String[] args) throws IOException {
inputByUTF(new File("/Users/lanou/Desktop/test3/utfFile.txt"));
inputByGBK(new File("/Users/lanou/Desktop/test3/gbkFile.txt"));
outputByUTF(new File("/Users/lanou/Desktop/test3/utfFile.txt"));
outputByGBK(new File("/Users/lanou/Desktop/test3/gbkFile.txt"));
}
练习
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
public class Demo07 {
public static void main(String[] args) throws IOException {
String string = "Success is the constant experience of failure and always keeping the initial enthusiasm";
File file = new File("/Users/lanou/Desktop/test3/ppp.txt");
fun(string, file);
}
public static void fun(String string, File file) throws IOException {
String[] list = string.split(" ");
HashMap<String, Integer> hashMap = new HashMap<>();
for (String str : list) {
if(hashMap.containsKey(str)) {
Integer integer = hashMap.get(str);
integer = integer + 1;
hashMap.put(str, integer);
}else {
hashMap.put(str, 1);
}
}
FileOutputStream fis = new FileOutputStream(file);
for (String str1 : hashMap.keySet()) {
fis.write((str1 + "=" + hashMap.get(str1) + "\n").getBytes());
fis.flush();
}
fis.close();
}
}
将一个文件夹下的所有txt文件 复制 到另一个文件夹下 并且把txt改成java
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/test3");
File file2 = new File("/Users/lanou/Desktop/fff");
CopyAndChange(file, file2);
}
public static void CopyAndChange(File src,File dest) throws IOException {
File[] files = src.listFiles(new MyFileFilter1());
for (File file : files) {
if(file.isFile()) {
String[] split = file.getName().split("\\.");
String string = split[split.length - 2] + ".java";
File newFile = new File(dest, string);
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(newFile);
int len = 0;
while((len = fis.read()) != -1) {
fos.write(len);
fos.flush();
}
fis.close();
fos.close();
}else {
CopyAndChange(file, dest);
}
}
}
第四天
缓冲字节输出流
public static void fun1() throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test3/liurongsheng.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write("床前明月光".getBytes());
bos.flush();
bos.close();
}
缓冲字节输入流
public static void fun2() throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test3/liurongsheng.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] bs = new byte[1024];
int len = 0;
while((len = bis.read(bs)) != -1) {
System.out.println(new String(bs, 0, len));
}
bis.close();
}
测试高效流的复制文件快慢
package com.lanou3g.bean4;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.net.ssl.ExtendedSSLSession;
public class Demo02 {
public static void main(String[] args) throws IOException {
new test1().test();
}
}
abstract class TestTime {
File file = new File("/Users/lanou/Desktop/图7.png");
File file2 = new File("/Users/lanou/Desktop/test3/图1.png");
public void test() throws IOException {
long start = System.currentTimeMillis();
copyFile();
long end = System.currentTimeMillis();
System.out.println(end - start);
}
public abstract void copyFile() throws IOException;
}
class test1 extends TestTime{
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
int len = 0;
while((len = fis.read()) != -1) {
fos.write(len);
}
fis.close();
fos.close();
}
}
class test2 extends TestTime{
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len = 0;
while((len = bis.read()) != -1) {
bos.write(len);
}
bis.close();
bos.close();
}
}
class test3 extends TestTime{
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
int len = -1;
byte[] bs = new byte[1024];
while((len = fis.read(bs)) != -1) {
fos.write(new String(bs, 0, len).getBytes());
}
fis.close();
fos.close();
}
}
class test4 extends TestTime{
@Override
public void copyFile() throws IOException {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(file2);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len = 0;
byte[] bs = new byte[1024];
while((len = bis.read(bs)) != -1) {
bos.write(new String(bs, 0, len).getBytes());
}
}
}
缓冲输出字符流
public static void fun1() throws IOException {
FileWriter fw = new FileWriter("/Users/lanou/Desktop/test3/kkk.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("床前明月光");
bw.newLine();
bw.flush();
char[] c = new char[] {'a','b','c','d','e'};
bw.write(c, 1, 2);
bw.flush();
bw.write(60);
bw.flush();
bw.close();
}
缓冲输入字符流
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test3/kkk.txt");
BufferedReader br = new BufferedReader(fr);
String string = "";
while((string = br.readLine()) != null) {
System.out.println(string);
}
br.close();
}
文件复制
package com.lanou3g.bean4;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Demo04 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("/Users/lanou/Desktop/test3/ppp7.txt");
FileWriter fw = new FileWriter("/Users/lanou/Desktop/test3/ppp8.txt");
BufferedReader br = new BufferedReader(fr);
BufferedWriter bw = new BufferedWriter(fw);
String string = "";
while((string = br.readLine()) != null) {
bw.write(string);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
Properties
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("张飞", "男");
properties.setProperty("关羽", "男");
Set<String> set = properties.stringPropertyNames();
for (String string : set) {
String string2 = properties.getProperty(string);
System.out.println(string + "=" + string2);
}
}
Properties读取
public static void fun2() throws FileNotFoundException, IOException {
Properties properties = new Properties();
FileReader fis = new FileReader("/Users/lanou/Desktop/test3/ppp6.txt");
properties.load(fis);
System.out.println(properties);
}
Properties写入
public static void fun3() throws IOException {
Properties properties = new Properties();
properties.setProperty("张飞", "40");
properties.setProperty("关羽", "50");
properties.setProperty("刘备", "60");
FileWriter fos = new FileWriter("/Users/lanou/Desktop/test3/ppp6.txt");
properties.store(fos, "");
fos.close();
}
序列化
public static void fun1() throws FileNotFoundException, IOException {
FileOutputStream fis = new FileOutputStream("/Users/lanou/Desktop/test3/ppp6.txt");
ObjectOutputStream oos = new ObjectOutputStream(fis);
oos.writeObject(new Person("张飞", 13));
oos.flush();
oos.close();
}
反序列化
public static void main(String[] args) throws IOException, ClassNotFoundException {
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test3/ppp6.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Object readObject = ois.readObject();
System.out.println(readObject);
}
代码示例
/* 学生类
* 姓名 性别 年龄 空参有参构造 set和get方法 toString方法
键盘录入6个学员信息(格式为 张三,男,25),要求有两个相同的信息,将6个学员信息存入到ArrayList集合中
将存有6个学员信息的ArrayList集合对象写入到文件中
读取文件中的ArrayList对象
对集合中的6个学生对象进行去重并按照年龄从小到大的顺序排序
再将排序完成的集合写进重新写进文件中*/
package com.lanou3g
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.util.ArrayList
import java.util.Collections
import java.util.Scanner
import java.util.TreeSet
public class Demo08 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Scanner scanner = new Scanner(System.in)
ArrayList< Student> array = new ArrayList<>()
while(array.size() < 3) {
boolean b = true
System.out.println("请输入六个学员信息((格式为 张三,男,25)):")
String string = scanner.nextLine()
try {
String[] strings = string.split(",")
Student student = new Student(strings[0],strings[1],Integer.parseInt(strings[2]))
array.add(student)
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("角标越界了 请重新输入")
}catch (Exception e) {
System.out.println("年龄不对 请重新输入")
}
}
// 写出文件
FileOutputStream fos = new FileOutputStream("/Users/lanou/Desktop/test3/ppp9.txt")
ObjectOutputStream oss = new ObjectOutputStream(fos)
oss.writeObject(array)
// 读取文件
FileInputStream fis = new FileInputStream("/Users/lanou/Desktop/test3/ppp9.txt")
ObjectInputStream ois = new ObjectInputStream(fis)
Object readObject = ois.readObject()
array = (ArrayList)readObject
// 排序
TreeSet<Student> set = new TreeSet<>()
set.addAll(array)
// System.out.println(set)
array.clear()
array.addAll(set)
// 写出文件
oss.writeObject(array)
// 读取文件
Object readObject2 = ois.readObject()
array = (ArrayList)readObject2
System.out.println(array)
oss.close()
ois.close()
}
}
第五天
模仿装饰者模式
package com.lanou3g.p03;
public class p13 {
public static void main(String[] args) {
MyStu stu = new MyStu(new Student());
stu.code();
}
}
interface Coder{
public abstract void code();
}
class Student implements Coder{
@Override
public void code() {
System.out.println("我会c语言");
System.out.println("我会c#");
System.out.println("我会c++");
}
}
class MyStu implements Coder{
private Student stu;
public MyStu() {
}
public MyStu(Student student) {
this.stu = student;
}
@Override
public void code() {
stu.code();
System.out.println("我会java");
System.out.println("我会javaEE");
}
}
LineNumberReader简单应用
public class p14 {
public static void main(String[] args) throws IOException {
FileReader fis = new FileReader("/Users/lanou/Desktop/level/dfdf/ppp.txt");
LineNumberReader lnr = new LineNumberReader(fis);
String string = null;
while((string = lnr.readLine()) != null) {
System.out.println(string);
}
lnr.close();
}
}
底层实现LinumberReader
package com.lanou3g.p03;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class P15 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/dfdf/ppp.txt");
FileReader fr = new FileReader(file);
MyNumber myNumber = new MyNumber(fr);
String string = "";
while((string = myNumber.getString()) != null) {
System.out.println(string);
}
myNumber.myClose();
}
}
class MyNumber{
private Reader reader;
public MyNumber() {
super();
}
public MyNumber(Reader reader) {
super();
this.reader = reader;
}
public String getString() throws IOException {
StringBuilder sb = new StringBuilder();
int len = 0;
while((len = reader.read()) != -1) {
if(len == 10) {
return sb.toString();
}else {
sb.append((char)len);
}
}
if(sb.length() == 0) {
return null;
}
return sb.toString();
}
public void myClose() throws IOException {
reader.close();
}
}
打印流PrintWriter
public class p16 {
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/Test2/k.txt");
FileWriter fr = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fr);
PrintWriter pw = new PrintWriter(bw);
pw.write("sdfsz");
pw.write("我爱中国");
pw.write("true");
pw.write(10);
char[] cs = new char[] {'a','c','f','g','h'};
pw.write(cs,1,2);
ps.println("网卡了");
ps.println(true);
ps.print(100);
ps.write(100);
}
}
打印流PrintStream
File file = new File("/Users/lanou/Desktop/Test2/k.txt");
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintStream ps = new PrintStream(bos);
ps.println("sdf");
ps.print(true);
ps.write(10);
ps.print(10);
ps.close();
代码练习
public static void main(String[] args) throws IOException {
System.out.println("请输入:");
InputStream in = System.in;
StringBuilder sb = new StringBuilder();
while(true) {
int read2 = in.read();
if(sb.toString().endsWith("quit")) { System.out.println(sb);
break;
}
if(read2 == 10) { System.out.println(sb.toString());
sb.delete(0, sb.length());
}else {
sb.append((char)read2);
}
}
}