public class Buffer_FlushDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("bos.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(fos);
String str = ",在懂得爱情的年龄下遇到了不能在一起的爱情";
byte[] bytes = str.getBytes("UTF-8");
bos.write(bytes);
System.out.println("写入完毕!");
bos.close();
}
}
package day04;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Buffered_CopyMp3 {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("C:/Users/臻冉/Desktop/韩甜甜 - 你的答案 (现场)(片段).mp3");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("C:/Users/臻冉/Desktop/韩甜甜 - 你的答案 (现场)(复制).mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len=-1;
long time1 = System.currentTimeMillis();
while((len=bis.read())!=-1){
bos.write(len);
bos.flush();
}
long time2 = System.currentTimeMillis();
System.out.println("复制完毕!");
System.out.println("耗时"+(time2-time1));
bis.close();
bos.close();
}
}
package day04;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Buffered_ReadDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("bos.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] data = new byte[200];
int len = fis.read(data);
String str = new String(data,0,len,"UTF-8");
System.out.println(str);
bis.close();
}
}
package day04;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
public class BufferReaderDemo {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream("pw2.txt"),"UTF-8")
);
String line = null;
while((line = br.readLine())!=null){
System.out.println(line);
}
}
}
package day04;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class FileInputStreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("fos.txt");
byte[] bytes = new byte[200];
int len = fis.read(bytes);
String str = new String(bytes,0,len,"UTF-8");
System.out.println(str);
fis.close();
}
}
package day04;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class FileOutputStream02 {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("fos.txt",true);
String str = ",坏的人给你经历,最差的人给你教训,最好的人给你回忆";
byte[] data = str.getBytes("utf-8");
fos.write(data);
fos.close();
System.out.println("输入完毕!");
}
}
package day04;
import java.io.File;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
public class FileOutputStreamDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("fos.txt");
String str = "永远不要去责怪你生命中的任何人,好的人给你快乐";
byte[] data = str.getBytes("UTF-8");
fos.write(data);
System.out.println("写入完毕!");
fos.close();
}
}
package day04;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("osw.txt");
InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
int len = -1;
while((len=isr.read())!=-1){
System.out.print((char)len);
}
isr.close();
}
}
package day04;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
public class ObjectInputStreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream("person.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
Person person = (Person)ois.readObject();
System.out.println(person);
ois.close();
}
}
package day04;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws Exception {
Person p = new Person();
p.setName("隔壁老王");
p.setAge(88);
p.setSex('男');
FileOutputStream fos = new FileOutputStream("person.obj");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
System.out.println("输入完毕!");
oos.close();
}
}
package day04;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamTransientDemo {
public static void main(String[] args) throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("teacher.obj"));
Teacher t = (Teacher)ois.readObject();
System.out.println(t);
ois.close();
}
}
package day04;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo {
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("osw.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
String str = "终有弱水替沧海";
osw.write(str);
osw.write(",再把相思寄巫山");
System.out.println("写入完毕!");
osw.close();
}
}
package day04;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -7580762681801403029L;
private String name;
private int age;
private char sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
}
}
package day04;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) throws Exception {
PrintWriter pe = new PrintWriter("pe.txt","UTF-8");
pe.println("如果你不努力,你的父母就要更加努力");
pe.print("如果你不勤奋的,你的孩子就更要努力");
pe.println("如果你不辛苦 ,你的另一半就要更加辛苦");
System.out.println("写完完毕!");
pe.flush();
}
}
package day04;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class PrintWriterDemo02 {
public static void main(String[] args) throws Exception {
PrintWriter pw = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("pw2.txt"),"UTF-8")
),true);
pw.println("不要把山那边想的很美好");
pw.println(",等你翻山越岭回来,你会觉得这边更好");
System.out.println("写入完毕!");
}
}
package day04;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class PwCopyDemo {
public static void main(String[] args) throws Exception {
BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/臻冉/Desktop/小丁/益康特/笔记/3第三阶段-JavaSE/day01/day01_02.png"),"UTF-8"));
PrintWriter pw =new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("C:/Users/臻冉/Desktop/小丁/益康特/笔记/3第三阶段-JavaSE/day01/day01_02(复制).png"),"UTF-8")));
String line = null;
while((line=br.readLine())!=null){
pw.println(line);
}
System.out.println("复制完毕!");
br.close();
pw.close();
}
}
```java
package day04;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class PwCopyDemo {
public static void main(String[] args) throws Exception {
BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/day01/day01_02.png"),"UTF-8"));
PrintWriter pw =new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("C:/Users/day01/day01_02(复制).png"),"UTF-8")));
String line = null;
while((line=br.readLine())!=null){
pw.println(line);
}
System.out.println("复制完毕!");
br.close();
pw.close();
}
}
package day04;
import java.io.Serializable;
import java.util.Arrays;
public class Teacher implements Serializable {
private String name;
private int age;
private transient String[] other;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getOther() {
return other;
}
public void setOther(String[] other) {
this.other = other;
}
@Override
public String toString() {
return "Teacher [name=" + name + ", age=" + age + ", other=" + Arrays.toString(other) + "]";
}
}