1.复制文本文件:有5种方式
package file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* 复制文本文件,字符流的五种方式
* @author naughtymonkey
*
*/
public class FileCopyDemo {
public static void main(String[] args) throws IOException {
String sorc="a.txt";
String cop="b.txt";
method5(sorc,cop);
}
private static void method5(String sorc, String cop) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(sorc));
BufferedWriter bw=new BufferedWriter(new FileWriter(cop));
String line=null;
while((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
bw.close();
br.close();
}
private static void method4(String sorc, String cop) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(sorc));
BufferedWriter bw=new BufferedWriter(new FileWriter(cop));
char[] ch=new char[1024];
int len=0;
while((len=br.read())!=-1){
bw.write(len);
}
bw.close();
br.close();
}
private static void method3(String sorc, String cop) throws IOException {
BufferedReader br=new BufferedReader(new FileReader(sorc));
BufferedWriter bw=new BufferedWriter(new FileWriter(cop));
int len=0;
while((len=br.read())!=-1){
bw.write(len);
}
bw.close();
br.close();
}
private static void method2(String sorc, String cop) throws IOException {
FileReader fr=new FileReader(sorc);
FileWriter fw=new FileWriter(cop);
char[] ch=new char[1024];
int len=0;
while((len=fr.read(ch))!=-1){
fw.write(ch, 0, len);
}
fw.close();
fr.close();
}
private static void method(String sorc,String cop) throws IOException {
FileReader fr=new FileReader(sorc);
FileWriter fw=new FileWriter(cop);
int ch=0;;
while((ch=fr.read())!=-1){
fw.write(ch);
}
fw.close();
fr.close();
}
}
2.复制图片:4种方式
package file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileStreamDemo {
public static void main(String[] args) throws IOException {
File srcFile = new File("c:\\mm.jpg");
File copFile = new File("d:\\mm.jpg");
method4(srcFile, copFile);
}
private static void method4(File srcFile, File copFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copFile));
byte[] by = new byte[1024];
int len = 0;
while ((len = bis.read(by)) != -1) {
bos.write(by, 0, len);
}
bos.close();
bis.close();
}
private static void method3(File srcFile, File copFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copFile));
int len = 0;
while ((len = bis.read()) != -1) {
bos.write(len);
}
bos.close();
bis.close();
}
private static void method2(File srcFile, File copFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(copFile);
byte[] by = new byte[1024];
int len = 0;
while ((len = fis.read(by)) != -1) {
fos.write(by, 0, len);
}
fos.close();
fis.close();
}
private static void method(File srcFile, File copFile) throws IOException {
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(copFile);
int len = 0;
while ((len = fis.read()) != -1) {
fos.write(len);
}
fos.close();
fis.close();
}
}
3.已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
请编写程序读取数据内容,把数据排序后写入ss.txt中。
package file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
/**
* 需求:已知s.txt文件中有一个这样的字符串:hcexfgijkamdnoqrzstuvwybpl
* @author naughtymonkey
*
*/
public class FileCopyTest {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new FileReader("ss.txt"));
String s =br.readLine();
char[] cha = s.toCharArray();
Arrays.sort(cha);
String str = new String(cha);
BufferedWriter bw=new BufferedWriter(new FileWriter("ss.txt"));
bw.write(str);
bw.newLine();
bw.flush();
bw.close();
br.close();
}
}
4.键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
package file;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;
public class FileCopy {
public static void main(String[] args) throws IOException {
BufferedWriter bw=new BufferedWriter(new FileWriter("student.txt"));
TreeSet<Student> ts=new TreeSet<Student>();
for(int x=1;x<=3;x++){
Scanner scan =new Scanner(System.in);
System.out.println("输入第"+x+"为学生的姓名");
String name=scan.nextLine();
System.out.println("输入语文成绩");
int chinese=scan.nextInt();
System.out.println("输入数学成绩");
int math=scan.nextInt();
System.out.println("输入英语成绩");
int english =scan.nextInt();
Student s=new Student(name,chinese,english,math);
ts.add(s);
}
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();
}
}
class Student implements Comparable<Student>{
private String name;
private int chinese;
private int english;
private int math;
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 getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public Student() {
super();
}
public Student(String name, int chinese, int english, int math) {
super();
this.name = name;
this.chinese = chinese;
this.english = english;
this.math = math;
}
private int sum(){
return this.chinese+this.english+this.math;
}
@Override
public int compareTo(Student s) {
int num=this.sum()-s.sum();
int num1=num==0?this.getName().compareToIgnoreCase(s.name):num;
return num1;
}
}