编码: 就是把字符串转换成字节数组
- 把一个字符串转换成一个字节数组
- public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- 解码: 把字节数组转换成字符串
- public String(byte[] bytes): 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
- public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
- 使用什么字符集进行编码,那么就是使用什么字符集进行解码
- 老地方 ----- 十进制 ---- 二进制 ---- 发出去
- 接收 ---- 二进制 ---- 十进制 --- 老地方
tring(byte[] bytes, String charsetName):通过指定的字符集解码字节数组
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
编码:把看得懂的变成看不懂的: String -- byte[]
解码:把看不懂的变成看得懂的: byte[] -- String
public class MyTest2 {
public static void main(String[] args) throws IOException {
//采用字符流,来复制,文本文件
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
//读一个字符,写一个字符来复制
int len=0;//用来记录你读取到的那个字符
while ((len=in.read())!=-1){
out.write(len);
out.flush();//字符流记得刷新一下
}
in.close();
out.close(); //关闭并刷新
}
}
public class MyTest3 {
public static void main(String[] args) throws IOException {
//一次读取一个字符数组,写出一个字符数组 推荐 使用
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
char[] chars = new char[1000];
int len=0;//读取到有效字符个数
while ((len=in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();//字符流记得刷新一下
}
in.close();
out.close();
}
}
public class MyTest {
public static void main(String[] args) throws IOException {
//字符转换流
//InputStreamReader 字节流到字符流桥梁
//OutputStreamWriter 字符流到字节流的桥梁
转换流 父类 子类 便捷流 唯一缺点,就是不能指定编码,他也没有特有方法,都是从父类继承下来的
//InputStreamReader----- FileReader
//OutputStreamWriter------ FileWriter
FileReader in = new FileReader("src/MyTest.java");
FileWriter out = new FileWriter("MyTest3.java");
char[] chars=new char[1000];
int len=0;
while ((len=in.read(chars))!=-1){
//System.out.println("读取的次数"+len);
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
}
}
public class MyTest {
public static void main(String[] args) throws IOException {
//高效的字符流
//BufferedReader
// BufferedWriter
// BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
// 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
BufferedReader bfr = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aa.txt"));
char[] chars = new char[1024];
int len = 0;
while ((len=bfr.read(chars)) != -1) {
bfw.write(chars, 0, len);
bfw.flush();
}
bfr.close();
bfw.close();
}
}
public class MyTest1 {
public static void main(String[] args) throws IOException {
// BufferedReader从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
// 可以指定缓冲区的大小,或者可使用默认的大小。大多数情况下,默认值就足够大了。
BufferedReader bfr = new BufferedReader(new FileReader("回顾.java"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("aaa.java"));
//BufferedReader 他有一个特有的方法,readLine() 一次读取一行内容
//bfr.readLine()
// bfw.write("\r\n");
//bfw.newLine(); //写一个换行符
//读取一行,写出一行来复制
String line=null;
while ((line=bfr.readLine())!=null){
bfw.write(line);
bfw.newLine(); //写出一个换行符,具有平台兼容性
bfw.flush();
}
bfr.close();
bfw.close();
}
}
public class MyTest {
public static void main(String[] args) throws IOException {
ArrayList list = new ArrayList<>();
list.add("张飞");
list.add("赵云");
list.add("马超");
list.add("黄忠");
list.add("关羽");
FileWriter writer = new FileWriter("username.txt");
for (String s : list) {
writer.write(s);
writer.write("\r\n");
writer.flush();
}
writer.close();
}
}
public class MyTest2 {
public static void main(String[] args) throws IOException {
ArrayList list = new ArrayList<>();
BufferedReader bfr = new BufferedReader(new FileReader("username.txt"));
String line = null;
while ((line = bfr.readLine()) != null) {
list.add(line);
}
for (String s : list) {
System.out.println(s);
}
}
}
public class MyTest3 {
public static void main(String[] args) throws IOException {
ArrayList list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("username.txt")));
//读取一行数据,往集合里面添加一个
String line=null;
while ((line=reader.readLine())!=null){
list.add(line);
}
//随机从集合里面抽取一个人
Random random = new Random();
int index = random.nextInt(list.size());
String s = list.get(index);
System.out.println(s);
}
}
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("D:\\测试图片");
File file1 = new File(file.getAbsolutePath().replace("D", "E"));
if(!file1.exists()){
file1.mkdirs();
}
copyFolder(file,file1);
}
private static void copyFolder(File file, File file1) throws IOException {
File[] files = file.listFiles();
for (File f : files) {
if(f.isFile()){
copyFile(f,file1);
}else {
file=new File(f.getAbsolutePath());
file1=new File(file.getAbsolutePath().replace("D","E"));
if(!file1.exists()){
file1.mkdirs();
}
copyFolder(file,file1);
}
}
}
private static void copyFile(File f, File file1) throws IOException {
f = new File(f.getAbsolutePath());
file1 = new File(f.getAbsolutePath().replace("D","E"));
FileInputStream in = new FileInputStream(f);
FileOutputStream out = new FileOutputStream(file1);
byte[] bytes = new byte[1024 * 8];
int len=0;
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
}
in.close();
out.close();
}
}
public class Student {
private String name;
private int chineseScore;
private int mathScore;
private int englishScore;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChineseScore() {
return chineseScore;
}
public void setChineseScore(int chineseScore) {
this.chineseScore = chineseScore;
}
public int getMathScore() {
return mathScore;
}
public void setMathScore(int mathScore) {
this.mathScore = mathScore;
}
public int getEnglishScore() {
return englishScore;
}
public void setEnglishScore(int englishScore) {
this.englishScore = englishScore;
}
//获取总分的方法
public int totalScore(){
return this.chineseScore+this.mathScore+this.englishScore;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", chineseScore=" + chineseScore +
", mathScore=" + mathScore +
", englishScore=" + englishScore +
'}';
}
}
--------------------------------------------------------------------------------------------
public class MyTest {
public static void main(String[] args) throws IOException {
TreeSet treeSet = new TreeSet<>(new Comparator() {
@Override
public int compare(Student s1, Student s2) {
//按照总分排大小
int num= s1.totalScore()-s2.totalScore();
//再比较姓名
int num2=num==0?s1.getName().compareTo(s2.getName()):num;
return num2;
}
});
for (int i = 1; i <= 3; i++) {
Student student = new Student();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入"+i+"个学生的姓名");
String name = scanner.nextLine();
student.setName(name);
System.out.println("请输入" + i + "个学生的语文成绩");
int yw = scanner.nextInt();
student.setChineseScore(yw);
System.out.println("请输入" + i + "个学生的数学成绩");
int xs= scanner.nextInt();
student.setMathScore(xs);
System.out.println("请输入" + i + "个学生的英语成绩");
int yy = scanner.nextInt();
student.setEnglishScore(yy);
//把学生添加到集合里面去
treeSet.add(student);
}
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Student.txt",true)));
//收到写一样表头
writer.write("编号"+"\t"+"姓名"+"\t"+"语文"+"\t"+"数学"+"\t"+"外语"+"\t"+"总分");
writer.newLine();
writer.flush();
//遍历集合把学生的数据存到文本文件中
int index=1;
for (Student student : treeSet) {
writer.write(index+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.totalScore());
writer.newLine();
writer.flush();
index++;
}
writer.close();
}
}