标签:字符 java -- --- IO close new txt public
目录
IO流
就是对数据存储和取出做操作
I:input输入流
o:输出流
文件复制
一 一次读一个字节
文件复制: 数据源:从哪里读 a.txt -- 读取数据 -- FileInputStream 目的地:写到哪里去 b.txt -- 写出数据 -- FileOutputStreama.txt
参考代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FilecopyDemo1 {
public static void main(String[] args) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("src\\com\\hjl\\test\\day28\\a.txt");
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("src\\com\\hjl\\test\\day28\\b.txt");
//一次读一个字节
int b = 0;
//一次写一个字节
while ((b=fis.read())!=-1){
System.out.print(b);
fos.write(b);
}
//释放资源
fis.close();
fos.close();
}
}
输出结果:
1081059711010312297105979899104101108108111106971189710497100111111112
b.txt
二 一次读一个字节
将E:\下面的金克斯.jpg复制到G:\盘的根目录下
数据源: E:\金克斯.jpg -- 读取数据 -- FileInputStream 目的地: G:\金克斯.jpg -- 写出数据 -- FileOutputStream
参考代码:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileCopyDemo2 {
public static void main(String[] args) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("E:\\金克斯.jpg");
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("G:\\金克斯.jpg");
int b = 0;
while ((b=fis.read())!=-1){
fos.write(b);
}
//释放资源
fis.close();
fos.close();
}
}
运行结果:
三 一次读一个字符数组
把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 数据源: a.txt -- 读取数据 -- 字符输入流(字符转换流) -- InputStreamReader 目的地: b.txt -- 写出数据 -- 字符输出流(字符转换流) -- OutputStreamWriter
参考代码:
import java.io.*;
public class FileCopyDemo3 {
public static void main(String[] args) throws IOException {
//创建字符输入流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("src\\com\\hjl\\test\\day28\\a.txt"));
//创建字符输出流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src\\com\\hjl\\test\\day28\\b.txt"));
//一次读写一个字符数组
char[] chars = new char[1024];
int length = 0;
while ((length = isr.read(chars)) != -1) {
osw.write(chars, 0, length);
osw.flush();
}
//释放资源
osw.close();
isr.close();
}
}
运行结果:
字符转换流的简化写法
字符输出流:FileWriter
字符输入流:FileReader
字符流 = 字节流 + 编码表 OutputStreamWriter = FileOutputStream + 编码表(Unicode) InputStreamReader = FileInputStream + 编码表(Unicode)
参考代码1:
public class FileCopyDemo5 {
public static void main(String[] args) throws IOException {
//创建字符输入流对象
// InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
//创建简化后的字符输入流对象
FileReader fr = new FileReader("src\\com\\shujia\\hjl\\day28\\a.txt");
//创建简化后的字符输出流对象
FileWriter fw = new FileWriter("src\\com\\shujia\\hjl\\day28\\b.txt");
//第一种方式:一次读写一个字符
// int c = 0;
// while ((c=fr.read())!=-1){
// fw.write(c);
// fw.flush();
// }
//第二种方式:一次读写一个字符数组
char[] chars = new char[1024];
int length = 0;
while ((length=fr.read(chars))!=-1){
fw.write(chars, 0, length);
fw.flush();
}
//释放资源
fw.close();
fr.close();
}
}
运行结果:
复制土味情话.txt 3000遍
参考代码:
import java.io.*;
public class FileCopyDemo6 {
public static void main(String[] args) throws IOException {
//起始时间戳
long start = System.currentTimeMillis();
for (int i = 0;i < 3000;i++){
//创建字符输入流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("G:\\JavaTest\\src\\com\\hjl\\test\\day28\\土味情话.txt"));
//创建字符输出流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("G:\\JavaTest\\src\\com\\hjl\\test\\day28\\爱你三千遍.txt"),true);
char[] chars = new char[1024];
int length = 0;
while ((length = isr.read(chars)) !=-1){
osw.write(chars,0,length);
osw.flush();
}
//释放资源
osw.close();
isr.close();
}
//结束时间戳
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
输出结果:
1528
运行结果:
使用高效字符流复制文件
BufferedWriter
BufferedReader
import java.io.*;
/*
使用高效字符流复制文件
*/
public class FileCopyDemo7 {
public static void main(String[] args) throws IOException {
//起始时间戳
long start = System.currentTimeMillis();
for (int i = 0; i < 3000; i++) {
BufferedReader br = new BufferedReader(new FileReader("G:\\JavaTest\\src\\com\\hjl\\test\\day28\\土味情话.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("G:\\JavaTest\\src\\com\\hjl\\test\\day28\\爱你三千遍.txt",true));
//一次读取一个字符数组
char[] chars = new char[2048];
int length = 0;
while ((length = br.read(chars)) != -1) {
bw.write(chars, 0, length);
bw.flush();
}
//释放资源
bw.close();
br.close();
}
long end = System.currentTimeMillis();
System.out.println("总耗费时间:" + (end - start));
}
}
输出结果;
总耗费时间:1487
运行结果:
相比较上一个而言速度更快
字符缓冲流特殊功能复制文件
void newLine()
String readLine()
参考代码:
import java.io.*;
public class FileCopyDemo8 {
public static void main(String[] args) throws IOException {
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("G:\\JavaTest\\src\\com\\hjl\\test\\day28\\土味情话.txt"));
//创建字符缓冲输出流对象
BufferedWriter bw = new BufferedWriter(new FileWriter("G:\\JavaTest\\src\\com\\hjl\\test\\day28\\土味情话1.txt"));
String line = null;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
br.close();
}
}
运行结果:
❤到底啦!给靓仔一个关注吧!❤
标签:字符,java,--,---,IO,close,new,txt,public 来源: https://blog.csdn.net/hujieliang123/article/details/122178328
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。