P587 IO流-FileReader读入数据的基本操作
package com.io;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderWriterTest {
public static void main(String[] args){
File file = new File("hello.txt");
System.out.println(file.getAbsolutePath());
}
@Test
public void testFileReader(){
FileReader fr = null;
try {
File file = new File("hello.txt");
fr = new FileReader(file);
int data;
while((data = fr.read()) != -1){
System.out.println((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
P588 IO流-FileReader中使用Read的重载方法读入数据
@Test
public void testFileReader1() throws IOException {
FileReader fr = null;
try {
File file = new File("hello.txt");
fr = new FileReader(file);
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
String str = new String(cbuf, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
P589 IO流-FileWriter写出数据的操作
@Test
public void testFileWriter() throws IOException {
File file = new File("hello1.txt");
FileWriter fw = new FileWriter(file, false);
fw.write("I have a dream\n");
fw.write("U have a dream");
fw.close();
}
P590 IO流-使用FileReader和FileWriter实现文本文件的复制
@Test
public void testFileWriter() throws IOException {
FileWriter fw = null;
try {
File file = new File("hello1.txt");
fw = new FileWriter(file, false);
fw.write("I have a dream\n");
fw.write("U have a dream");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileReaderFileWriter(){
FileReader fr = null;
FileWriter fw = null;
try {
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1){
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
P591 IO流-字符流不能处理图片文件的测试
P592 IO流-使用FileInputStream不能读取文本文件的测试
@Test
public void testFileInoutStream() throws IOException {
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
String str = new String(buffer, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
fis.close();
}
}
}
P593 IO流-使用FileInputStream和FileOutputStream读写非文本文件
@Test
public void testFileInputOutputStream(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File("XX.jpg");
File destFile = new File("XX2.jpg");
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer, 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();
}
}
}
P594 IO流-使用FileInputStream和FileOutputStream复制文件的方法测试
public void copyFile(String srcPath, String destPath){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] buffer = new byte[5];
int len;
while((len = fis.read(buffer)) != -1){
fos.write(buffer, 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();
}
}
}
@Test
public void testCopyFile(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\hu\\Desktop\\计划.docx";
String destPath = "C:\\Users\\hu\\Desktop\\plan.docx";
copyFile(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("时间为:" + (end - start));
}
P595 IO流-缓冲流实现非文本文件的复制
public class BufferedTest {
@Test
public void BufferedStreamTest(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcFile = new File("XX.jpg");
File destFile = new File("XXX.jpg");
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[5];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
P596 IO流-缓冲流与节点流读写速度对比
public void copyFileWithBuffered(String srcPath, String destPath){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[5];
int len;
while((len = bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bis != null)
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void testCopyFileWithBuffered(){
long start = System.currentTimeMillis();
String srcPath = "C:\\Users\\hu\\Desktop\\计划.docx";
String destPath = "C:\\Users\\hu\\Desktop\\plan.docx";
copyFileWithBuffered(srcPath, destPath);
long end = System.currentTimeMillis();
System.out.println("时间为:" + (end - start));
}
P597 IO流-缓冲流实现文本文件的复制
@Test
public void testBufferedReaderBufferedWriter(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("XX.txt")));
bw = new BufferedWriter(new FileWriter(new File("XXX.txt")));
String data;
while ((data = br.readLine()) != null){
bw.write(data + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}