1.File 类
1.1 File类概述和构造方法
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f1 = new File("E:\\test0807\\a.txt");
System.out.println(f1);
File f2 = new File("E:\\test0807","a.txt");
System.out.println(f2);
File f3 = new File("E:\\test0807");
File f4 = new File(f3,"a.txt");
System.out.println(f4);
}
}
输出结果:
E:\test0807\a.txt
E:\test0807\a.txt
E:\test0807\a.txt
1.2 File类的创建功能
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f1 = new File("E:\\test0807\\a.txt");
System.out.println(f1.createNewFile());//文件不存在,创建文件返回true;文件存在返回false
System.out.println(f1);
File f2 = new File("E:\\test0807\\javaSE");
System.out.println(f2.mkdir());//目录不存在,创建目录返回true;目录存在返回false
System.out.println(f2);
File f3 = new File("E:\\test0807\\javaEE\\test");
System.out.println(f3.mkdirs());//多级目录不存在,创建多级目录返回true;多级目录存在返回false
System.out.println(f3);
}
}
1.3 File类删除功能
1.4 File类判断和获取功能
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) throws IOException {
File f1 = new File("E:\\test0807\\a.txt");
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
System.out.println(f1.exists());
System.out.println("-----------");
System.out.println(f1.getAbsolutePath());
System.out.println(f1.getPath());
System.out.println(f1.getName());
System.out.println("------------");
File f2 = new File("E:\\test0807");
String[] fileArr = f2.list();
for(String s : fileArr){
System.out.println(s);
}
System.out.println("------------");
File[] files = f2.listFiles();
for(File file : files){
if(file.isDirectory()){
System.out.println(file);
System.out.println(file.getName());
}
}
}
}
1.5 递归
public class DiDuiDemo {
public static void main(String[] args) {
//每个月兔子的个数1,1,2,3,5,8.....
System.out.println(f(3));
}
public static int f(int n){
if(n ==1 || n == 2){
return 1;
}else {
return f(n-1) +f(n-2);
}
}
}
public class DiDuiDemo2 {
public static void main(String[] args) {
//求5!
System.out.println(f(5));
}
public static int f(int n){
if(n==1){
return 1;
}else {
return n*f(n-1);
}
}
}
// A code block
var foo = 'bar';import java.io.File;
public class DiDuiDemo {
public static void main(String[] args) {
File file = new File("E:\\test0807");
fMkdir(file);
}
public static void fMkdir(File f) {
File[] files = f.listFiles();
if (files != null) {
for (File ff : files) {
if (ff.isDirectory()) {
fMkdir(ff);
} else {
System.out.println(ff.getAbsolutePath());
}
}
}
}
}
2.字节流
2.1 IO流概述和分类
2.2 字节流写数据
2.3 字节流写数据的3种方式
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 构造方法:
* FileOutputStream(String name):创建文件输出流已指定的名称写入文件
* FileOutputStream(File file):创建文件输出流写入已指定的File对象表示的文件
*
* */
public class FileOutputStreamDemo {
public static void main(String[] args) throws FileNotFoundException,IOException {
FileOutputStream fos = new FileOutputStream("E:\\test0807\\a.txt");
/* File file= new File("E:\\test0807\\a.txt");
FileOutputStream fos1 = new FileOutputStream(file);*/
/*fos.write(97);
fos.write(98);*/
byte[] bs = {99,100,101,102};
/* fos.write(bs);
byte[] bs1 = "abcde".getBytes();
fos.write(bs1);*/
fos.write(bs,0,2);
fos.close();
}
}
2.4 字节流写数据的两个小问题
public class FileOutputStreamDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("E:\\test0807\\a.txt",true);
for(int i =0;i<10;i++){
fos.write("hello".getBytes());
fos.write("\r\n".getBytes());
}
fos.close(); //释放资源
}
}
2.5 字节流写数据加异常处理
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) {
//加入finally来实现释放资源
FileOutputStream fos = null;
try {
fos = new FileOutputStream("E:\\test0807\\a.txt");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace( );
}
}
}
}
}
2.6 字节流读数据
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args)throws IOException {
FileInputStream fis = new FileInputStream("E:\\test0807\\a.txt");
int by;
//到达文件的末尾等于-1
while ((by=fis.read()) !=-1){
System.out.print((char)(by));
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args)throws IOException {
FileInputStream fis = new FileInputStream("E:\\test0807\\a.txt");
FileOutputStream fos = new FileOutputStream("E:\\test0807\\javaSE\\a.txt");
int by;
//到达文件的末尾等于-1
while ((by=fis.read()) !=-1){
fos.write(by);
}
fis.close();
fos.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args)throws IOException {
FileInputStream fis = new FileInputStream("E:\\test0807\\a.txt");
int by;
byte[] bs = new byte[1024];//1024及其整数倍
//到达文件的末尾等于-1
while ((by=fis.read(bs)) !=-1){
System.out.println(new String(bs,0,by));
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String[] args)throws IOException {
FileInputStream fis = new FileInputStream("E:\\test0807\\1.png");
FileOutputStream fos = new FileOutputStream("E:\\test0807\\javaSE\\1.png");
int by;
byte[] bs = new byte[1024];//1024及其整数倍
//到达文件的末尾等于-1
while ((by=fis.read(bs)) !=-1){
fos.write(bs,0,by);
}
fis.close();
fos.close();
}
}
2.7 字节缓冲流
package learn;
import java.io.*;
public class BufferStreamDemo {
public static void main(String[] args) throws FileNotFoundException,IOException{
/*BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\test0807\\bb.txt"));
//写数据
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
bos.close();*/
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\test0807\\bb.txt"));
//一次读取一个字节数组
/*int len=0;
while ((len = bis.read()) != -1){
System.out.print((char)len);
}*/
byte[] bytes = new byte[1024];
int len = 0;
while ( (len =bis.read(bytes)) != -1){
System.out.print(new String(bytes,0,len));
}
}
}
package learn;
import java.io.*;
public class BufferStreamTest {
public static void main(String[] args)throws FileNotFoundException,IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\test0807\\javaEE\\001.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\test0807\\javaSE\\001.mp4"));
byte[] bytes = new byte[1024];
int len = 0;
while ( (len =bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bis.close();
bos.close();
}
}
3.字符类
3.1 为什么会出现字符流
3.2 编码表
3.3 字符串中的编码解码问题
package learn;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class StringDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
//编码
String s = "中国";
// 默认的是UTF-8
//byte[] bys = s.getBytes();//[-28, -72, -83, -27, -101, -67]
//byte[] bys = s.getBytes("UTF-8");//[-28, -72, -83, -27, -101, -67]
byte[] bys = s.getBytes("GBK");//[-42, -48, -71, -6]
System.out.println(Arrays.toString(bys));
//解码
System.out.println("------------");
/*String ss = new String(bys);//编码与解码的字符集编码规则要相同
System.out.println(ss);//乱码*/
String ss = new String(bys,"GBK");
System.out.println(ss);
}
}
3.4 字符流中的编码解码问题
package learn;
import java.io.*;
public class StreamDemo {
public static void main(String[] args) throws FileNotFoundException, IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\test0807\\javaEE\\aa.txt"));
osw.write("中国");
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\test0807\\javaEE\\aa.txt"));
osw.close();
//一次读取一个字符数据
int len ;
while ((len = isr.read()) != -1){
System.out.print((char)len);
}
isr.close();
}
}
输出结果:
中国
3.5 字符流写数据的五种方式
3.6 字符流读数据的2种方式
package learn;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderTest {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\test0807\\javaEE\\aa.txt"));
char[] chars = new char[1024];
int len;
while ( (len =isr.read(chars)) != -1){
System.out.println(new String(chars,0,len));
}
isr.close();
}
}
package learn;
import java.io.*;
public class InputStreamReaderTest {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\test0807\\javaEE\\Test.java"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\test0807\\Test.java"));
char[] chars = new char[1024];
int len;
while ( (len =isr.read(chars)) != -1){
osw.write(chars,0,len);
}
isr.close();
osw.close();
}
}
package learn;
import java.io.*;
public class InputStreamReaderTest {
public static void main(String[] args) throws IOException {
// 根据数据源创建字符输入流对象
FileReader fr = new FileReader("E:\\test0807\\javaEE\\Test.java");
// 根据目的地创建字符输出流对象
FileWriter fw = new FileWriter("E:\\test0807\\Test.java");
char[] chars = new char[1024];
int len;
while ( (len =fr.read(chars)) != -1){
fw.write(chars,0,len);
}
fr.close();
fw.close();
}
}
3.7 字符缓冲流
package learn;
import java.io.*;
public class BufferDemo {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new FileReader("E:\\test0807\\javaEE\\Test.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test0807\\javaSE\\Test.java"));
// 一次读取一个字符
/* int len;
while ((len =br.read()) != -1){
bw.write(len);
}*/
//一次读取一行字符
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1){
bw.write(chars,0,len);
}
br.close();
bw.close();
}
}
3.8 字符缓冲流特有功能
package learn;
import java.io.*;
public class BufferedDemo2 {
public static void main(String[] args) throws IOException{
/* BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test0807\\javaSE\\test2.txt"));
bw.write("hello");
bw.newLine();
bw.write("world");
bw.newLine();
bw.close();*/
BufferedReader br = new BufferedReader(new FileReader("E:\\test0807\\javaSE\\test2.txt"));
/*System.out.println(br.readLine());//hello
System.out.println(br.readLine());//world
System.out.println(br.readLine());//null*/
String line;
while ((line= br.readLine()) != null){
System.out.println(line);
}
br.close();
}
}
package learn;
import java.io.*;
public class BufferDemo {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new FileReader("E:\\test0807\\javaEE\\Test.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\test0807\\javaSE\\Test.java"));
String line;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
3.9 IO流小结
package learn;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class ArrayListTxtDemo {
public static void main(String[] args) throws IOException{
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("alisa1");
arrayList.add("alisa2");
arrayList.add("alisa3");
BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt"));
for(String s : arrayList){
bw.write(s);
bw.newLine();
}
bw.close();
}
}
package learn;
import java.io.*;
import java.util.ArrayList;
public class TxtToArrayListDemo {
public static void main(String[] args) throws IOException{
BufferedReader bw = new BufferedReader(new FileReader("array.txt"));
ArrayList<String> arrayList = new ArrayList<>();
String line;
while((line = bw.readLine()) != null){
arrayList.add(line);
}
for(String s :arrayList){
System.out.println(s);
}
}
}
package learn;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class CallNameDemo {
public static void main(String[] args) throws IOException {
BufferedReader bw = new BufferedReader(new FileReader("array.txt"));
ArrayList<String> arrayList = new ArrayList<>();
String line;
while((line = bw.readLine()) != null){
arrayList.add(line);
}
Random r = new Random();
int index = r.nextInt(arrayList.size());//nextInt(a)随机生成的随机数范围就变成[0,a)。
System.out.println("幸运者是" + arrayList.get(index));
}
}
package learn;
import java.io.*;
public class CopyFolderDemo {
public static void main(String[] args) throws IOException {
File srcFolder = new File("E:\\test0807\\javaSE");
String srcName = srcFolder.getName();
File destFolder = new File("E:\\test0807\\copy",srcName);
if(!destFolder.exists()){
destFolder.mkdir();
}
for(File srcFile : srcFolder.listFiles()) {
String srcFileName = srcFile.getName();
File destFile = new File(destFolder,srcFileName);
copyFile(srcFile,destFile);
}
}
public static void copyFile(File srcFile,File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0 ,len);
}
bis.close();
bos.close();
}
}
package learn;
import java.io.*;
public class CopyFolderTest {
public static void main(String[] args) throws IOException {
File srcFolder = new File("E:\\test0807");
File destFolder = new File("F:\\");
copyFolder(srcFolder,destFolder);
}
//复制文件内容
public static void copyFile(File srcFile,File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0 ,len);
}
bis.close();
bos.close();
}
//复制文件
public static void copyFolder(File srcFile,File destFile) throws IOException {
if(srcFile.isDirectory()){
String srcFileName = srcFile.getName();
File destFolder = new File(destFile, srcFileName);
if(!destFolder.exists()){
destFolder.mkdir();
}
File[] fileArray = srcFile.listFiles();
for(File file : fileArray){
copyFolder(file,destFolder);
}
}else {
File newFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
}
4.特殊操作流
4.1 标准输入输出流
4.2 打印流
4.3 对象序列化流
package learn;
import java.io.Serializable;
public class Student implements Serializable {
public int age;
public String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package learn;
import java.io.*;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException{
//对象序列化流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\test0807\\oos.txt"));
Student s = new Student(30,"林清");
oos.writeObject(s);
oos.close();
}
}
package learn;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ObjectInputStreamDemo{
public static void main(String[] args) throws IOException,ClassNotFoundException {
//对象反序列化流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\test0807\\oos.txt"));
Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s.getName()+","+s.getAge());
ois.close();
}
}
package learn;
import java.io.Serializable;
public class Student implements Serializable {
public static final long serialVersionUID = 42L;
public transient int age;
public String name;
public Student(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package learn;
import java.io.*;
public class ObjectStreamTest {
public static void main(String[] args) throws IOException,ClassNotFoundException{
//write();
read();
}
public static void read() throws IOException,ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\test0807\\ooos.txt"));
Object obj = ois.readObject();
Student s = (Student) obj;
System.out.println(s.getName()+","+s.getAge());
ois.close();
}
public static void write()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\test0807\\ooos.txt"));
Student s = new Student(30,"林清");
oos.writeObject(s);
oos.close();
}
}
输出结果:
林清,0
4.4 Properties
package learn;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args)throws IOException {
//myStore();
myLoad();
}
public static void myStore() throws IOException{
Properties prop = new Properties();
prop.setProperty("001","alisa");
prop.setProperty("002","susuan");
prop.setProperty("003","leon");
FileWriter fileWriter = new FileWriter("sss.txt");
prop.store(fileWriter,null);
fileWriter.close();
}
public static void myLoad() throws IOException{
Properties prop = new Properties();
FileReader fileReader = new FileReader("sss.txt");
prop.load(fileReader);
fileReader.close();
System.out.println(prop);
}
}