您的位置:首页 > 编程语言 > Java开发

Java作业杨枝11.25/11.26

2018-04-10 04:19 337 查看
package prac;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*复制文本文件的5种方法(将当前项目下a.txt的内容复制到当前项目下的b.txt中)*/
public class Prac01 {
public static void main(String[] args) throws IOException {

String s1="a.txt";
String s2="b.txt";

method1(s1,s2);
method2(s1,s2);
method3(s1,s2);
method4(s1,s2);
method5(s1,s2);
}
//字符缓冲流一次读写一个字符串
private static void method5(String s1,String s2) throws IOException {
BufferedReader fr=new BufferedReader(new FileReader(s1));
BufferedWriter  fw = new BufferedWriter(new FileWriter(s2));

String len=null;
while((len=fr.readLine())!=null){
fw.write(len);
fw.newLine();
fw.flush();
}
fw.close();
fr.close();
}

//字符缓冲流一次读写一个字符数组
private static void method4(String s1,String s2) throws IOException {
BufferedReader fr=new BufferedReader(new FileReader(s1));
BufferedWriter  fw = new BufferedWriter(new FileWriter(s2));

char[] chs=new char[1024];
int len=0;
while((len=fr.read())!=-1){
fw.write(chs,0,len);
}
fw.close();
fr.close();
}

//字符缓冲流一次读写一个字符
private static void method3(String s1,String s2) throws IOException {
BufferedReader fr=new BufferedReader(new FileReader(s1));
BufferedWriter  fw = new BufferedWriter(new FileWriter(s2));

int by=0;
while((by=fr.read())!=-1){
fw.write(by);
}
fw.close();
fr.close();
}

//基本字符流一次读写一个字符数组
private static void method2(String s1,String s2) throws IOException {
FileReader fr=new FileReader(s1);
FileWriter  fw = new FileWriter(s2);

char[] chs=new char[1024];
int len=0;
while((len=fr.read())!=-1){
fw.write(chs,0,len);
}
fw.close();
fr.close();
}

//基本字符流一次读写一个字符
private static void method1(String s1,String s2) throws IOException {
FileReader fr=new FileReader(s1);
FileWriter  fw = new FileWriter(s2);

int by=0;
while((by=fr.read())!=-1){
fw.write(by);
}
fw.close();
fr.close();
}
}


package prac;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*4:复制图片:4种方式
* 将F:\\照片\\1.JPG复制到F:\\照片\\2.jpg
* */
public class Prac02 {
public static void main(String[] args) throws IOException {
String s1="F:\\picture\\1.JPG";
String s2="F:\\picture\\2.jpg";

method1(s1,s2);
method2(s1, s2);
method3(s1, s2);
method4(s1, s2);
}
//字节缓冲流一次读写一个字节数组
private static void method4(String s1, String s2) throws IOException {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(s1));
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(s1));

byte[] chs=new byte[1024];
int len=0;
while((len=fis.read(chs))!=-1){
fos.write(chs,0,len);
}
fis.close();
fos.close();
}
//字节缓冲流一次读写一个字节
private static void method3(String s1, String s2) throws IOException {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(s1));
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(s1));

int by=0;
while((by=fis.read())!=-1){
fos.write(by);
}

fis.close();
fos.close();
}

//一般字节流一次读写一个字节数组
private static void method2(String s1, String s2) throws IOException {
FileInputStream fis = new FileInputStream(s1);
FileOutputStream  fos=new FileOutputStream(s2);

byte[] chs=new byte[1024];
int len=0;
while((len=fis.read(chs))!=-1){
fos.write(chs,0,len);
}
fis.close();
fos.close();
}

//一般字节流一次读写一个字节
private static void method1(String s1, String s2) throws IOException {
FileInputStream fis = new FileInputStream(s1);
FileOutputStream  fos=new FileOutputStream(s2);

int by=0;
while((by=fis.read())!=-1){
fos.write(by);
}

fis.close();
fos.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: