本文目录:
- 一、File类(实例化、基本方法、创建与删除)
- 二、IO流
- 1.IO流体系,基本分类
- 2.FileReader基本使用
- 3.FileWriter基本使用
- 4.FileWriter与FileReader实现文件复制
- 5.FileInputStream与FileOutputStream实现视频复制
- 6.使用缓冲流FBufferedInputStream与BufferedOutputStream提高效率(生产常用)
- 7.使用缓冲流bufferedReader 与bufferedWriter 提高文本读写效率(生产常用)
- 8.使用转换流InputStreamReader与OutputStreamWriter转换文本编码
- 9.使用标准输入流System.in
- 10.打印流PrintStream的使用
- 11.数据流DataOutputStream的使用
- 12.对象流的序列化与反序列化
一、File类(实例化、基本方法、创建与删除)
package test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
class Test {
public static void main(String[] args) throws IOException {
System.out.println("********File实例化********");
// File(String path)
File file1 = new File("D:"+File.separator+"work"+File.separator+"test"); // 绝对路径
System.out.println(file1.getPath());
// File(String parentPath,String childPath)
File file2 = new File("DTest"+File.separator+"file","javaTest"); // 相对路径
System.out.println(file2.getPath());
// File(File file,String childPath)
File file3 = new File(file1,"javaTest.iml");
System.out.println(file2.getPath());
System.out.println("********File实例方法********");
// 获取路径
System.out.println("getAbsolutePath:\t\t"+file2.getAbsolutePath());
System.out.println("getPath:\t\t"+file2.getPath());
System.out.println("getName:\t\t"+file2.getName());
System.out.println("getParent:\t\t"+file2.getParent());
System.out.println("length:\t\t\t"+file2.length());
System.out.println("lastModified:\t"+new Date(file2.lastModified()));
// 下一层文件集合(String)
String[] list = file1.list();
System.out.println("list:\t\t"+Arrays.asList(list));
// 下一层文件集合(File)
File[] files = file1.listFiles();
for (File file : files) {
System.out.print("listFiles:\t\t\t"+file.getName()+" ");
}
System.out.println();
// 文件重命名,file4存在,file5不能存在,返回布尔
File file4 = new File(file1,"file1.txt");
// 判断
System.out.println("********判断********");
System.out.println(file4.isDirectory());
System.out.println(file4.isFile());
System.out.println(file4.exists());
System.out.println(file4.canRead());
System.out.println(file4.canWrite());
System.out.println(file4.isHidden());
System.out.println("********File类创建与删除文件或目录********");
File fileCreate = new File("createdFile.txt");
System.out.println(fileCreate.createNewFile());
System.out.println(fileCreate.delete());
File fileCreatePath = new File("directory");
System.out.println(fileCreatePath.mkdir()); // 单层目录
File fileCreatePath1 = new File("directory1"+File.separator+"createdFiles");
System.out.println(fileCreatePath1.mkdirs());// 多层目录
}
}
// 输出:
********File实例化********
D:\work\test
DTest\file\javaTest
DTest\file\javaTest
********File实例方法********
getAbsolutePath: D:\1file\JavaLearning\basicLearning\DTest\file\javaTest
getPath: DTest\file\javaTest
getName: javaTest
getParent: DTest\file
length: 0
lastModified: Thu Jan 01 08:00:00 CST 1970
list: [directory1, directory2, directory3, file1.txt, file2.txt, file3.txt]
listFiles: directory1 listFiles: directory2 listFiles: directory3 listFiles: file1.txt listFiles: file2.txt listFiles: file3.txt
********判断********
false
true
true
true
true
false
********File类创建与删除文件或目录********
true
true
false
false
二、IO流
1.IO流体系,基本分类
2.FileReader基本使用
package IOTest;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class IOTest {
@Test
public void FileReader() {
// 实例化流
FileReader fr = null;
// 为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
try {
// 提供File对象,确认文件存在且有内容
File file = new File("D:"+File.separator+"work"+File.separator+"test"+File.separator+"file1.txt");
System.out.println(file.getAbsolutePath()+" "+file.exists()+" "+file.length());
fr = new FileReader(file);
// 读入数据,方式1
/*int data = fr.read(); // read方法只读一个字符
while (data != -1){
System.out.print((char)data);
data = fr.read();
}*/
// 读入数据,方式2
/*int data;
while ((data = fr.read()) != -1){ // read方法只读一个字符
System.out.print((char)data);
}*/
// 读入数据,方式3,常用
char [] data = new char[6]; // 声明数组作为read的参数,用于存放每次读入的数据
int len ; // 每次读入数据的字符个数
while ((len= fr.read(data)) != -1){
// for (int i = 0; i < len; i++) {System.out.print(data[i]);}
// 或者
System.out.print(new String(data, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 防止fr在实例化之前报异常进入finally报错,加判断后关闭
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 输出
D:\work\test\file1.txt true 232
I don’t know what your dream is .
I don’t care how disappointing it might have been,
as you’ve been working toward that dream.
That dream that you are holding in your mind is possible.
鹅鹅鹅,曲项向天歌
如果中文出现乱码,请将文件调整为utf8编码:
3.FileWriter基本使用
package IOTest;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
@Test
public void FileWriter() {
// File实例化可以放在try外面,因为文件可以不存在
File file = new File("D:"+File.separator+"work"+File.separator+"test"+File.separator+"file4.txt");
FileWriter fw = null;
try {
// 文件不存在时,new时自动创建
fw = new FileWriter(file); // 默认覆盖写,实例化时清空文件内容,并改变文件的修改时间与访问时间
// fw = new FileWriter(file,true); // 追加写,,实例化不改变文件的修改时间与访问时间
fw.write("I have an apple\n");
fw.write("I have a pen\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.FileWriter与FileReader实现文件复制
package IOTest;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class IOTest {
@Test
public void FileCopy() {
FileReader fr = null;
FileWriter fw = null;
File toFile = new File("d:\\work\\test\\file5.txt");
try {
File fromFile = new File("d:\\work\\test\\file1.txt");
fr = new FileReader(fromFile);
fw = new FileWriter(toFile);
char[] data = new char[6];
int len ;
while ((len = fr.read(data)) != -1){
fw.write(data, 0, len); // 与fr.read()相同的写法
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.FileInputStream与FileOutputStream实现视频复制
package IOTest;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class IOTest {
@Test
public void FileInputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
File toFile = new File("d:\\work\\test\\testVideoCopy.avi");
try {
File fromFile = new File("d:\\work\\test\\testVideo.avi");
fis = new FileInputStream(fromFile);
fos = new FileOutputStream(toFile);
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1){
fos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.使用缓冲流FBufferedInputStream与BufferedOutputStream提高效率(生产常用)
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
@Test
public void FileInputStream() {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File toFile = new File("d:\\work\\test\\testVideoCopy1.avi");
try {
File fromFile = new File("d:\\work\\test\\testVideo.avi");
fis = new FileInputStream(fromFile);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(toFile);
bos = new BufferedOutputStream(fos);
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
7.使用缓冲流bufferedReader 与bufferedWriter 提高文本读写效率(生产常用)
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
@Test
public void FileInputStream() {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
File toFile = new File("d:\\work\\test\\file1-copy.txt");
try {
File fromFile = new File("d:\\work\\test\\file1.txt");
FileReader fileReader = new FileReader(fromFile);
FileWriter fileWriter = new FileWriter(toFile);
bufferedReader = new BufferedReader(fileReader);
bufferedWriter = new BufferedWriter(fileWriter);
// 原始方法
/*char[] chars = new char[10];
int len ;
while ((len= bufferedReader.read(chars)) != -1 ){
bufferedWriter.write(chars,0,len);
}*/
// 每次读一行
String str;
while((str = bufferedReader.readLine()) != null){
bufferedWriter.write(str);
bufferedWriter.newLine(); // 换行,相当于bufferedWriter.write("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null)
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedWriter!= null)
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
8.使用转换流InputStreamReader与OutputStreamWriter转换文本编码
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
@Test
public void FileInputStream1() {
// 转换流:属于字符流
// InputStreamReader:将一个字节的输入流转换为字符的输入流
// OutputStreamWriter:将一个字符的输出流转换为字节的输出流
FileInputStream fis = null;
FileOutputStream fos = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
File toFile = new File("file1-copy1.txt");
try {
File fromFile = new File("d:\\work\\test\\file1.txt");
fis = new FileInputStream(fromFile);
isr = new InputStreamReader(fis,"utf-8");
fos = new FileOutputStream(toFile);
osw = new OutputStreamWriter(fos,"gbk");
char[] chars = new char[10];
int len;
while ((len = isr.read(chars)) != -1){
System.out.println(len+"==="+new String(chars,0,len));
osw.write(chars, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (isr != null)
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (osw != null)
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
9.使用标准输入流System.in
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
public static void main(String[] args) {
BufferedReader br = null;
try {
//InputStream in = System.in;
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true){
System.out.println("please input:");
String s = br.readLine();
if (s.equals("exit")){
break;
}
String upperCase = s.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
10.打印流PrintStream的使用
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
@Test
public void test2() {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("D:\\work\\file1.txt"));
// 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
ps = new PrintStream(fos, true);
if (ps != null) {// 把标准输出流(控制台输出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 输出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50个数据一行
System.out.println(); // 换行
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}
}
11.数据流DataOutputStream的使用
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
@Test
public void test3() throws IOException {
//1.
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
//2.
dos.writeUTF("运维小菜");
dos.flush();//刷新操作,将内存中的数据写入文件
dos.writeInt(23);
dos.flush();
dos.writeBoolean(true);
dos.flush();
//3.
dos.close();
}
}
12.对象流的序列化与反序列化
package IOTest;
import org.junit.Test;
import java.io.*;
public class IOTest {
@Test
public void ObjectStreamTest() {
// 序列化:ObjectOutputStream
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("objects.obj"));
oos.writeObject(new GoodWorker("yunWeiXiaoCai",22));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 反序列化:ObjectInputStream
System.out.println("****反序列化过程****");
GoodWorker goodWorker = null;
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("objects.obj"));
Object obj = ois.readObject();
goodWorker = (GoodWorker)obj;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (ois != null)
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(goodWorker.getName());
}
}
/* 1.需要实现接口:Serializable
* 2.当前类提供一个全局常量:serialVersionUID
* 3.除了当前Person类需要实现Serializable接口之外,还必须保证其内部所有属性也必须是可序列化的。(默认情况下,基本数据类型可序列化)
* 4.ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量*/
class GoodWorker implements Serializable{
private String name;
private int age;
public GoodWorker(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
}