3:复制文本文件:有5种方式
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copytxt {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
method5();
}
private static void method5() throws FileNotFoundException, IOException {
BufferedReader br=new BufferedReader(new FileReader("ArrayListInHashMap.java"));
BufferedWriter bw=new BufferedWriter(new FileWriter("e.txt"));
String line=null;
while((line=br.readLine())!=null) {
bw.write(line);
bw.newLine();
}
br.close();
bw.close();
}
private static void method4() throws FileNotFoundException, IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("ArrayListInHashMap.java"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("d.txt"));
byte[] bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
private static void method3() throws FileNotFoundException, IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("ArrayListInHashMap.java"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.txt"));
int by=0;
while((by=bis.read())!=-1) {
bos.write(by);
}
bos.close();
bis.close();
}
private static void method2() throws FileNotFoundException, IOException {
FileInputStream fis=new FileInputStream("ArrayListInHashMap.java");
FileOutputStream fos=new FileOutputStream("a.txt");
byte[] bys=new byte[1024];
int len=0;
while((len=fis.read(bys))!=-1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
private static void method1() throws FileNotFoundException, IOException {
FileInputStream fis=new FileInputStream("ArrayListInHashMap.java");
FileOutputStream fos=new FileOutputStream("b.txt");
int by=0;
while((by=fis.read())!=-1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
4:复制图片:4种方式
public class CopyJpg {
public static void main(String[] args) throws IOException {
method1();
method2();
method3();
method4();
}
private static void method4() throws FileNotFoundException, IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("timg.jpg"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("d.jpg"));
byte[] bys=new byte[1024];
int len=0;
while((len=bis.read(bys))!=-1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
private static void method3() throws FileNotFoundException, IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("timg.jpg"));
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.jpg"));
int by=0;
while((by=bis.read())!=-1) {
bos.write(by);
}
bos.close();
bis.close();
}
private static void method2() throws FileNotFoundException, IOException {
FileInputStream fis=new FileInputStream("timg.jpg");
FileOutputStream fos=new FileOutputStream("b.jpg");
byte[] bys=new byte[1024];
int len=0;
while((len=fis.read(bys))!=-1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
private static void method1() throws FileNotFoundException, IOException {
FileInputStream fis=new FileInputStream("timg.jpg");
FileOutputStream fos=new FileOutputStream("a.jpg");
int by=0;
while((by=fis.read())!=-1) {
fos.write(by);
}
fos.close();
fis.close();
}
}
5:已知s.txt文件中有这样的一个字符串:“hcexfgijkamdnoqrzstuvwybpl”
请编写程序读取数据内容,把数据排序后写入ss.txt中。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
public class CopyString {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("String.txt");
FileOutputStream fos=new FileOutputStream("StringSort.txt");
ArrayList<Character> al=new ArrayList<Character>();
int by=0;
while((by=fis.read())!=-1) {
al.add((char)by);
}
Collections.sort(al);
for(Character s:al) {
fos.write((int)s);
}
}
}
String.txt内容:hcexfgijkamdnoqrzstuvwybpl
StringSort.txt内容:abcdefghijklmnopqrstuvwxyz
6:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件
package weekends_10;
import java.io.Serializable;
public class Student implements Serializable,Comparable<Student>{
String name;
public Student(String name, String chinese, String math, String english) {
super();
this.name = name;
Chinese = chinese;
this.math = math;
English = english;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String Chinese;
String math;
String English;
public String getChinese() {
return Chinese;
}
public void setChinese(String chinese) {
Chinese = chinese;
}
public String getMath() {
return math;
}
public void setMath(String math) {
this.math = math;
}
public String getEnglish() {
return English;
}
public void setEnglish(String english) {
English = english;
}
public int getSum() {
return Integer.parseInt(Chinese)+Integer.parseInt(English)+Integer.parseInt(math);
}
public Student(String chinese, String math, String english) {
super();
Chinese = chinese;
this.math = math;
English = english;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((Chinese == null) ? 0 : Chinese.hashCode());
result = prime * result + ((English == null) ? 0 : English.hashCode());
result = prime * result + ((math == null) ? 0 : math.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (Chinese == null) {
if (other.Chinese != null)
return false;
} else if (!Chinese.equals(other.Chinese))
return false;
if (English == null) {
if (other.English != null)
return false;
} else if (!English.equals(other.English))
return false;
if (math == null) {
if (other.math != null)
return false;
} else if (!math.equals(other.math))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [name=" + name + ", Chinese=" + Chinese + ", math=" + math + ", English=" + English + "]";
}
@Override
public int compareTo(Student s) {
int num=s.getSum()-this.getSum();
int num1=num==0?s.getName().compareTo(this.getName()):num;
return num1;
}
}
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;
public class StudentDemo {
public static void main(String[] args) throws IOException {
TreeSet<Student> ts=new TreeSet<Student>();
Scanner sc=new Scanner(System.in);
for(int i=0;i<5;i++) {
Student s=new Student();
System.out.println("请输入第"+(i+1)+"个学生的名字");
String name = sc.nextLine();
s.setName(name);
System.out.println("请输入第"+(i+1)+"个学生的数学成绩");
String math = sc.nextLine();
s.setMath(math);
System.out.println("请输入第"+(i+1)+"个学生的英语成绩");
String English = sc.nextLine();
s.setEnglish(English);
System.out.println("请输入第"+(i+1)+"个学生的语文成绩");
String chinese = sc.nextLine();
s.setChinese(chinese);
ts.add(s);
}
FileWriter fos=new FileWriter("Student.txt");
for(Student s:ts) {
fos.write("姓名"+s.getName());
fos.write(" ");
fos.write("总分"+String.valueOf(s.getSum()));
fos.write(" ");
fos.write("语文"+s.getChinese());
fos.write(" ");
fos.write("英语"+s.getEnglish());
fos.write(" ");
fos.write("数学"+s.getMath());
fos.write("\r\n");
fos.flush();
}
fos.close();
}
}
运行结果
姓名alice 总分450 语文190 英语140 数学120
姓名bob 总分430 语文130 英语120 数学180
姓名allen 总分320 语文100 英语130 数学90
姓名Tubo 总分320 语文100 英语120 数学100
姓名逝水流 总分257 语文78 英语89 数学90