Java 1.8 API 在线
1. IO流
(1)IO流:
A:用于实现设备间的数据传输问题
上传文件,下载文件
B:java中数据以IO流的形式传输
C:针对流操作的对象都在io包中
(2)IO流的分类
A:数据流向
输入流 读数据
输出流 写数据
B:数据类型
字节流
字符流
IO流的4个基类
字节流
字节输入流 InputStream
字节输出流 OutputStream
字符流
字符输入流 Reader
字符输出流 Writer
a:默认情况下,IO流分类说的是按照数据类型分。
b:通过windows记事本打开并能够读懂的文件,就用字符流。
否则,用字节流。
如果你不知道,建议永远使用字节流。
(3)FileOutputStream写数据
FileOutputStream的构造方法:
FileOutputStream(File file)
FileOutputStream(String name)
步骤:
A:创建字节输出流对象
B:调用写数据功能
C:释放资源
练习:请把字节的名字写到一个文本文件
代码体现:
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write("helloworld".getBytes());
fos.close();
几个小问题:
A:创建字节输出流对象做了几件事情?
a.调用系统功能创建了一个文件
b.创建了一个对象
c.把对象指向了文件
B:写完数据后,为什么要close()呢?
a.关闭流对象(流对象不可以继续在写数据了)
b.释放与此流相关的资源(通知系统去释放与此流相关的资源)
C:如何实现数据的换行呢?
不同的系统,针对换行符号的识别是不一样的。
Mac: \r
linux: \n
windows: \r\n
系统自带的记事本软件,只能识别该系统能够识别的换行。
而Eclipse自带的记事本,以及Editplus却可以识别任意的换行符。
D:如何实现数据的追加写入呢?
public FileOutputStream(String name,boolean append)
如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
(4)FileInputStream读数据
两种方式:
一次读取一个字节:public int read();读取到-1时表示结束
一次读取一个字节数组:public int read(byte[] b);返回的是实际的读取长度,把数据读取到字节数组中
步骤:
A:创建字节输入流对象
B:调用读数据功能
C:释放资源
代码体现:
FileInputStream fis = new FileInputStream("fis.txt");
//方式1:一次读取一个字节
int by = 0;
while((by=fis.read())!=-1) {
System.out.print((char)by);
}
//方法2:一次读取一个字节数组
byte[] bys = new byte[1024];//这里的数据一般是1024或者其整数倍
int len = 0;
while((len=fis.read(bys))!=-1) {
System.out.print(new String(bys,0,len));
}
fis.close();
读写数据,代码实现如下:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Test{
public static void main(String[] args) throws IOException{
//写数据
FileOutputStream fos = new FileOutputStream("fos.txt");
fos.write("helloworld".getBytes());
fos.close();
//读数据
FileInputStream fis = new FileInputStream("fos.txt");
int by = 0;
while((by=fis.read())!=-1){
System.out.print((char)by);
}
System.out.println();
fis.close();
//读数据
fis = new FileInputStream("fos.txt");
byte[] bys = new byte[1024*1024];
int len = 0;
while((len=fis.read(bys))!=-1){
System.out.print(new String(bys,0,len));
}
System.out.println();
fis.close();
}
}
案例:复制文件
数据源:读数据
目的地:写数据
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("D:\\Desktop\\java\\code\\a.txt");
// 封装目的地
FileOutputStream fos1 = new FileOutputStream("a1.txt");
FileOutputStream fos2 = new FileOutputStream("a2.txt");
// 读写数据
int by = 0;
while ((by = fis.read()) != -1) {
fos1.write(by);
}
// 读写数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos2.write(bys, 0, len);
}
// 释放资源
fos1.close();
fos2.close();
fis.close();
}
}
1:字节缓冲流
(1)为了提高效率,java就提供了字节缓冲区流。
(2)字节缓冲区流
字节缓冲输入流 BufferedInputStream(InputStream is)
字节缓冲输出流 BufferedOutputStream(OutputStream out)
通过看构造方法,我们发现,缓冲流不能直接操作文件。
是建立在基本的操作流之上的。
所以,这种流也被称之为高级流。
(3)复制视频文件四种方式
A:基本字节流一次读写一个字节
B:基本字节流一次读写一个字节数组
C:高效字节流一次读写一个字节
D:高效字节流一次读写一个字节数组
(4)流的一些名字
A:节点流 基本流
B:处理流 高级流
2:转换流
(1)因为字节流操作文本文件不是特别的方便,所以java就提供了转换流。
可以把字节流转换为字符流
用来让我们操作文本文件数据更方便一些
(2)转换流本身是一个字符流
字符流 = 字节流 + 编码表
(3)编码表
字符和对应数据组成的一张表
常见编码表:
ASCII
ISO-8859-1
GB2312,GBK,GB18030
BIG5
UTF-8
编码表详解:
计算机只能识别二进制数据,早期由来是电信号。
为了方便应用计算机,让它可以识别各个国家的文字。
就将各个国家的文字用数字来表示,并一一对应,形成一张表。
ASCII:美国标准信息交换码。
用一个字节的7位可以表示。
ISO8859-1:拉丁码表。欧洲码表
用一个字节的8位表示。
GB2312:中国的中文编码表。
GBK:中国的中文编码表升级,融合了更多的中文文字符号。
GB18030:GBK的取代版本
BIG-5码 :通行于台湾、香港地区的一个繁体字编码方案,俗称“大五码”。
Unicode:国际标准码,融合了多种文字。
所有文字都用两个字节来表示,Java语言使用的就是unicode
UTF-8:最多用三个字节来表示一个字符。
UTF-8不同,它定义了一种“区间规则”,这种规则可以和ASCII编码保持最大程度的兼容:
它将Unicode编码为00000000-0000007F的字符,用单个字节来表示它将Unicode编码为00000080-000007FF的字符用两个字节表示 它将Unicode编码为00000800-0000FFFF的字符用3字节表示
(4)编码问题
A:String的编码问题
B:IO流中的编码问题
方案:
统一编码,就不会有任何问题。
3:字符流
(1)字符流体系
Reader
|--InputStreamReader
|--FileReader
|--BufferedReader
Writer
|--OutputStreamWriter
|--FileWriter
|--BufferedWriter
BufferedReader:
public String readLine():
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
BufferedWriter:
public void newLine():写一个换行符
(2)字符流操作案例
A:基本字符流一次读写一个字符
B:基本字符流一次读写一个字符数组
C:高效字符流一次读写一个字符
D:高效字符流一次读写一个字符数组
E:高效字符流一次读写一个字符串
(3)字符流 = 字节流 +编码表
OutputStreamWriter 字符输出流
public OutputStreamWriter(OutputStream out);默认字符集
public OutputStreamWriter(OutputStream out,String charsetName);指定字符集
InputStreamReader 字符输入流
public InputStreamReader(InputStream in);默认字符集
public InputStreamReader(InputStream in,String charsetName);指定字符集
OutputStreamWriter写数据方法
public void write(int c)
public void write(char[] cbuf)
public void write(char[] cbuf,int off,int len)
public void write(String str)
public void write(String str,int off,int len)
OutputStreamWriter读数据方法
public int read()
public int read(char[] cbuf)
转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,
所以为了简化我们的书写,转换流提供了对应的子类。
FileWriter写数据
FileReader读取数据
(4)flush()和close()的区别?
A:flush 刷新流,流对象还可以继续使用
B:close 关闭流,流对象不可以继续使用
4:IO流小结
IO流
|--字节流
|--输入流 InputStream
|--FileInputStream
|--BufferedInputStream
|--输出流 OutputStream
|--FileOutputStream
|--BufferedOutputStream
|--字符流
|--输入流 Reader
|--FileReader
|--BufferedReader
|--输出流 Writer
|--FileWriter
|--BufferedWriter
复制文本文件:9种方案。
字节流:4种
字符流:5种
建议使用字符流。并且用最后一种。
复制二进制流数据:
4种方案。
如果不知道,用字节流
字节缓冲流,实现代码如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Test {
public static void main(String[] args) throws IOException {
// 写数据
// BufferedOutputStream(OutputStream out)
// OutputStream out = new FileOutputStream("bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(out);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
bos.write("hello".getBytes());
bos.close();
// 读数据
// BufferedInputStream(InputStream is)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));
// 方式1
int by = 0;
while ((by = bis.read()) != -1) {
System.out.print((char) by);
}
System.out.println("\n");
// 方式2
bis = new BufferedInputStream(new FileInputStream("bos.txt"));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
}
bis.close();
}
}
字节流读写四种方式
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
字节流读写四种方式:
A:基本字节流一次读写一个字节 共耗时:7510毫秒
B:基本字节流一次读写一个字节数组 共耗时:16毫秒
C:高效字节流一次读写一个字节 共耗时:59毫秒
D:高效字节流一次读写一个字节数组 共耗时:6毫秒
*/
public class Test {
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
method1();
long end1 = System.currentTimeMillis();
method2();
long end2 = System.currentTimeMillis();
method3();
long end3 = System.currentTimeMillis();
method4();
long end4 = System.currentTimeMillis();
System.out.println("共耗时:" + (end1 - start) + "毫秒");
System.out.println("共耗时:" + (end2 - end1) + "毫秒");
System.out.println("共耗时:" + (end3 - end2) + "毫秒");
System.out.println("共耗时:" + (end4 - end3) + "毫秒");
}
// 基本字节流一次读写一个字节
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("D:\\Desktop\\java\\file\\qq.png");
FileOutputStream fos = new FileOutputStream("qq_copy.png");
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("D:\\Desktop\\java\\file\\qq.png");
FileOutputStream fos = new FileOutputStream("qq_copy.png");
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 高效字节流一次读写一个字节
public static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\Desktop\\java\\file\\qq.png"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("qq_copy.png"));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节数组
public static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\Desktop\\java\\file\\qq.png"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("qq_copy.png"));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
转换流,代码实现如下
import java.util.Arrays;
import java.io.UnsupportedEncodingException;
public class Test {
public static void main(String[] args) throws UnsupportedEncodingException{
// public byte[] getBytes()
// public byte[] getBytes(String charsetName)
// public String(byte[] bytes, String charsetName)
String str = "你好,世界!";
byte[] bys1 = str.getBytes();
byte[] bys2 = str.getBytes("GBK");
String str2 = new String(bys1, "GBK");
System.out.println(Arrays.toString(bys1));
System.out.println(Arrays.toString(bys2));
System.out.println(str2);
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*
* IO流中的编码解码问题:要想在IO流中对数据进行编码问题,必须使用转换流。
*/
public class Test {
public static void main(String[] args) throws IOException {
// 写数据
// 默认GBK
//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));
//指定GBK
//OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"), "GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"), "UTF-8");
osw.write("中国");
osw.close();
// 读数据
//InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));
//InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"), "GBK");
InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"), "UTF-8");
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
System.out.println(new String(chs, 0, len));
}
isr.close();
}
}
用OutputStreamWriter和InputStreamReader复制文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/*
* 用OutputStreamWriter和InputStreamReader复制文件
*/
public class Test {
public static void main(String[] args) throws IOException {
// 封装数据源
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
// 封装目的地
OutputStreamWriter osw1 = new OutputStreamWriter(new FileOutputStream("b.txt"));
OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("c.txt"));
// 方式1
int ch = 0;
while ((ch = isr.read()) != -1) {
osw1.write(ch);
}
// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
osw2.write(chs, 0, len);
}
// 释放资源
osw1.close();
osw2.close();
isr.close();
}
}
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt"));
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* Writer:
* OutputStreamWriter 把字节流转换为字符流,并可以指定编码
* FileWriter
* Reader:
* InputStreamReader 把字节流转换为字符流,并可以指定编码
* FileReader
*/
public class Test {
public static void main(String[] args) throws IOException {
// 封装数据源
FileReader fr = new FileReader("a.txt");
// 封装目的地
FileWriter fw1 = new FileWriter("b1.txt");
FileWriter fw2 = new FileWriter("b2.txt");
// 读写
// 方式1
int ch = 0;
while ((ch = fr.read()) != -1) {
fw1.write(ch);
}
// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw2.write(chs, 0, len);
}
// 释放资源
fw1.close();
fw2.close();
fr.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
// 写数据
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
for (int x = 0; x < 10; x++) {
bw.write("hello" + x);
bw.newLine();
bw.flush();
}
bw.close();
// 读数据
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}
}
2. IO流练习
IO流练习
(1)复制图片
(2)复制文本文件
(3)把ArrayList集合中的字符串数据存储到文本文件
(4)从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
(5)复制单极文件夹
(6)复制单极文件夹中指定文件并修改文件名称
(7)复制多极文件夹
(8)键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
(9)names.txt中存储的是所有同学的名称,每次运行程序,随机产生一个名称出来。
(1)复制图片(4种方式)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 复制图片(4种)
* A:基本字节流一次读写一个字节
* B:基本字节流一次读写一个字节数组
* C:高效字节流一次读写一个字节
* D:高效字节流一次读写一个字节数组
*/
public class Test {
public static void main(String[] args) throws IOException {
File srcFile = new File("D:\\Desktop\\java\\code\\a.txt");
File destFile = new File("D:\\Desktop\\java\\code\\a1.txt");
// method1(srcFile, destFile);
// method2(srcFile, destFile);
// method3(srcFile, destFile);
method4(srcFile, destFile);
}
// 高效字节流一次读写一个字节数组
public static void method4(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节
public static void method3(File srcFile, File destFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int by = 0;
while ((by = bis.read()) != -1) {
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void method2(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void method1(File srcFile, File destFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
(2)复制文本文件(9种,而我们选择字符流,就用5种方式实现)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 复制文本文件(9种,而我们选择字符流,就用5种方式实现)
* A:基本字符流一次一个字符
* B:基本字符流一次一个字符数组
* C:高效字符流一次一个字符
* D:高效字符流一次一个字符数组
* E:高效字符流一次一个字符串
*/
public class Test {
public static void main(String[] args) throws IOException {
String srcString = "D:\\Desktop\\java\\code\\a.txt";
String destString = "D:\\Desktop\\java\\code\\a1.txt";
method1(srcString, destString);
// method2(srcString, destString);
// method3(srcString, destString);
// method4(srcString, destString);
// method5(srcString, destString);
}
// 高效字符流一次一个字符串
private static void method5(String srcString, String destString)throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
String line = null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
// 高效字符流一次一个字符数组
private static void method4(String srcString, String destString)throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
}
bw.close();
br.close();
}
// 高效字符流一次一个字符
private static void method3(String srcString, String destString)throws IOException {
BufferedReader br = new BufferedReader(new FileReader(srcString));
BufferedWriter bw = new BufferedWriter(new FileWriter(destString));
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
}
bw.close();
br.close();
}
// 基本字符流一次一个字符数组
private static void method2(String srcString, String destString)throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
}
fw.close();
fr.close();
}
// 基本字符流一次一个字符
private static void method1(String srcString, String destString)throws IOException {
FileReader fr = new FileReader(srcString);
FileWriter fw = new FileWriter(destString);
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
}
fw.close();
fr.close();
}
}
(3)把ArrayList集合中的字符串数据存储到文本文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
/*
* 把ArrayList集合中的字符串数据存储到文本文件
*/
public class Test {
public static void main(String[] args) throws IOException {
// 创建集合对象
// 封装数据源
ArrayList<String> array = new ArrayList<String>();
array.add("hello");
array.add("world");
array.add("java");
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("array.txt"));
for (String line : array) {
bw.write(line);
bw.newLine();
bw.flush();
}
// 释放资源
bw.close();
}
}
(4)从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/*
* 从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
*/
public class Test {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("array.txt"));
// 封装目的地
ArrayList<String> array = new ArrayList<String>();
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
}
br.close();
// 遍历集合
for (String s : array) {
System.out.println(s);
}
}
}
(5)复制单极文件夹
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 复制单级文件夹
*
* 分析:
* A:在目的地创建文件夹
* B:获取数据源文件夹下的所有文件的File数组
* C:遍历File数组,得到每一个File对象
* D:复制该File
*/
public class Test {
public static void main(String[] args) throws IOException {
// 封装数据源文件夹
File srcFolder = new File("D:\\Desktop\\java\\file");
// 封装和数据源一样的文件夹,并判断是否存在,如果不存在,就创建
File destFolder = new File(srcFolder.getName());
if (!destFolder.exists()) {
destFolder.mkdir();
}
// 获取数据源文件夹下的所有文件的File数组
File[] fileArray = srcFolder.listFiles();
// 遍历File数组,得到每一个File对象
for (File file : fileArray) {
String name = file.getName();
File newFile = new File(destFolder, name);
copy(file, newFile);
}
}
private static void copy(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
(6)复制单极文件夹中指定文件并修改文件名称
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
* 复制单级文件夹中指定文件并修改文件名称
*/
public class Test {
public static void main(String[] args) throws IOException {
// 封装数据源目录
File srcFolder = new File("D:\\Desktop\\java\\file");
// 封装目的地目录
File destFolder = new File("D:\\Desktop\\java\\code\\file");
if (!destFolder.exists()) {
destFolder.mkdir();
}
// 获取数据源目录下的所有文件的File数组
File[] fileArray = srcFolder.listFiles();
// 遍历File数组,得到每一个File对象
for (File file : fileArray) {
// System.out.println(file);
String name = file.getName();
name = name.replace(".txt", ".class");
File newFile = new File(destFolder, name);
copy(file, newFile);
}
}
private static void copy(File file, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
file));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(newFile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
}
(7)复制多极文件夹
import java.io.*;
public class Test {
public static void main(String[] args)throws IOException {
//创建数据源File对象
File srcfile = new File("D:\\Desktop\\java\\file");
//创建目的地File对象
File destfile = new File("D:\\Desktop\\java\\code\\file");
//文件夹复制
copyFolder(srcfile, destfile);
}
private static void copyFolder(File srcfile, File destfile)throws IOException {
if (srcfile.isDirectory()) {
String srcfileName = srcfile.getName();
File newFolder = new File(destfile, srcfileName);
if (!newFolder.exists()) {
newFolder.mkdir();
}
File[] filearray = srcfile.listFiles();
for (File file : filearray) {
copyFolder(file, destfile);
}
} else {
copyFile(srcfile, new File(destfile, srcfile.getName()));
}
}
private static void copyFile(File srcfile, File destfile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destfile));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1) {
bos.write(bys,0,len);
}
bis.close();
bos.close();
}
}
(8)键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
/*
* 键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
*
* 分析:
* A:创建学生类
* B:使用TreeSet集合存储学生信息,并按照总分排序,用比较器实现
* C:键盘录入学生信息
* D:遍历Set集合,把学生信息存储到文本文件
*/
public class Test{
public static void main(String[] args) throws IOException {
// 创建Set集合
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getSum() - s1.getSum();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;
return num2;
}
});
System.out.println("学生录入开始----------------------");
// 键盘录入数据
for (int x = 1; x <= 5; x++) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第" + x + "个学生的姓名:");
String name = sc.nextLine();
System.out.println("请输入第" + x + "个学生的语文成绩:");
int chinese = sc.nextInt();
System.out.println("请输入第" + x + "个学生的数学成绩:");
int math = sc.nextInt();
System.out.println("请输入第" + x + "个学生的英语成绩:");
int english = sc.nextInt();
// 创建学生对象
Student s = new Student();
s.setName(name);
s.setChinese(chinese);
s.setMath(math);
s.setEnglish(english);
// 把学生添加到集合
ts.add(s);
}
System.out.println("学生录入结束----------------------");
// 把学生信息存储到文本文件
BufferedWriter bw = new BufferedWriter(new FileWriter("students.txt"));
bw.write("姓名,语文成绩,数学成绩,英语成绩");
bw.newLine();
bw.flush();
for (Student s : ts) {
StringBuilder sb = new StringBuilder();
sb.append(s.getName()).append(",").append(s.getChinese()).append(",")
.append(s.getMath()).append(",").append(s.getEnglish());
bw.write(sb.toString());
bw.newLine();
bw.flush();
}
bw.close();
System.out.println("数据成功写入文件");
}
}
class Student {
private String name;
private int chinese;
private int math;
private int english;
public Student(String name, int chinese, int math, int english) {
super();
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSum() {
return chinese + math + english;
}
}
(9)names.txt中存储的是所有同学的名称,每次运行程序,随机产生一个名称出来。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.FileInputStream;
/*
* 需求:names.txt中存储的是所有同学的名称,每次运行程序,随机产生一个名称出来。
*
* 分析:
* A:数据来自于文本文件,所以我们要读取数据
* B:把读取到的数据存储到集合中
* C:随机产生一个索引
* D:从集合中根据随机产生的索引获取一个值就可以了
*/
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("names.txt"));
ArrayList<String> array = new ArrayList<String>();
String line = null;
while ((line = br.readLine()) != null) {
array.add(line);
System.out.println(line);
}
br.close();
// 随机产生一个索引
// int index = (int) (Math.random() * array.size());
// [0,array.size()-1)
Random r = new Random();
int index = r.nextInt(array.size());
System.out.println("幸运者是:" + array.get(index));
System.out.println("===========================");
//如果出现乱码可通过以下代码修改编码方式
ArrayList<String> array1 = new ArrayList<String>();
InputStreamReader br1 = new InputStreamReader(new FileInputStream("names.txt"),"GBK");
BufferedReader bf = new BufferedReader(br1);
String line1 = null;
while ((line1 = bf.readLine()) != null) {
array1.add(line1);
System.out.println(line1);
}
bf.close();
br1.close();
index = r.nextInt(array.size());
System.out.println("幸运者是:" + array1.get(index));
}
}
3. IO流补充
操作基本数据类型的流
DataInputStream
DataOutputStream
可以把基本类型的流写到文本文件,也可以从文本文件中读取
内存操作流
操作字节数组
ByteArrayInputStream
ByteArrayOutputStream
操作字符数组
CharArrayReader
CharArrayWrite
操作字符串
StringReader
StringWriter
打印流
(1)分为字节打印流和字符打印流
PrintWriter 字节流打印流
PrintStream 字符打印流
(2)字符打印流
A:只操作目的地,不操作数据源
B:可以操作任意的数据类型
C:如果启动了自动刷新,在调用println()方法的时候,可以自动刷新和换行
D:可以直接操作文件
问题:哪些流对象可以直接操作文件呢?
构造方法可以同时接收File和String类型的都可以直接操作文件
(3)复制文本文件
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
PrintWriter pw = new PrintWriter(new FileWriter("b.txt"),true);
String line = null;
while((line=br.readLine())!=null) {
pw.println(line);
}
pw.close();
br.close();
标准输入输出流
System类中的字段:in,out。
它们各代表了系统标准的输入和输出设备。
默认输入设备是键盘,输出设备是显示器。
System.in的类型是InputStream.
System.out的类型是PrintStream是OutputStream的子类FilterOutputStream 的子类.
System.in:BufferedReader br = new BufferedReader(new InputStream(System.in));
System.out:BufferedWriter bw = new BufferedWriter(new OutputStream(System.out));
后面参数不具有具体文件路径无需释放资源
随机访问流
RandomAccessFile类不属于流,是Object类的子类。
融合了InputStream和OutputStream的功能,支持对随机访问文件的读取和写入。
也就是说可以在指定的位置写数据,也可以从指定的位置读取数据。
自己本身既可以写数据,还可以读数据
合并流:SequenceInputStream类
可以将多个输入流串流在一起,合并为一个输入流,因此也被称为合并流。
我们如果要把多个文件合并到一起进行读取,就要使用合并流。
SequenceInputStream的构造方法
SequenceInputStream(InputStream s1, InputStream s2);接收两个InputStream 参数
SequenceInputStream(Enumeration<? extends InputStream> e);接收多个InputStream参数
案例:把多个文件的内容写入到一个文本文件
序列化流
(1)序列化流:ObjectOutputStream
把对象按照流一样的方式存储到文件,或者在网络中传输;
或者说为了让对象数据永久储存或者实现在网络中传输而生成的二进制流
(2)反序列化流:ObjectInputStream
把流数据还原成对象,或者说将序列化对象还原
(3)如何实现序列化:实现Seriable接口
没有任何方法接口的意义:标记接口
让被序列化的流对象所属的类实现序列化接口
(4)如何解决问题?
A:只要有一点小小的变动,会重新产生一个序列化id值
B:黄色警告线
指定一个固定的SerialVersionUID
序列化操作问题
为什么要实现序列化?
如何实现序列化?
序列化数据后,再次修改类文件,读取数据会出问题,如何解决呢?
使用transient关键字声明不需要序列化的成员变量
Properties集合
(1)是Map的子集合,可以和IO流进行结合使用。键和值都是字符串
Properties的特殊功能
A:添加功能
public Object setProperty(String key,String value)
B:获取功能
public String getProperty(String key)
public Set<String> stringPropertyNames()
Properties和IO流的结合使用
public void load(Reader reader)
public void store(Writer writer,String comments)
NIO包下的IO流
NIO其实就是新IO的意思。
JDK4出现NIO。新IO和传统的IO有相同的目的,都是用于进行输入输出的,
但新IO使用了不同的方式来处理输入输出,采用内存映射文件的方式,
将文件或者文件的一段区域映射到内存中,就可以像访问内存一样的来访问文件了,
这种方式效率比旧IO要高很多,但是目前好多地方我们看到的还是旧IO的使用,
所以我们仍以旧IO为主,知道NIO即可。
4. IO流补充练习
(1)已知s.txt文件中有这样的一个字符串:“helloworld”,请编写程序读取数据内容,把数据排序后写入ss.txt中。
(2)用Reader模拟BufferedReader的readLine()功能
(3)自定义类模拟LineNumberReader的特有功能
(4)获取每次读取数据的行号
(5)登录注册IO版
5. 访问模式
"r" 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException。
"rw" 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。
"rws" 打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。
"rwd" 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。
一般使用rw模式。