IO流的基本API使用
public class Test {
public static void main(String[] args) {
test13();
}
public static void test14() {
try (FileInputStream fis = new FileInputStream("src/l.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
) {
Person p = (Person) ois.readObject();
System.out.println(p);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test13() {
try (FileOutputStream fos = new FileOutputStream("src/l.dat");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("src/l.dat");
ObjectInputStream ois = new ObjectInputStream(fis);
) {
Person p = new Person("张三","男",18);
oos.writeObject(p);
Person p1 = (Person)ois.readObject();
System.out.println(p1);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test12() {
try (FileInputStream fis = new FileInputStream("src/a.txt"); DataInputStream dis = new DataInputStream(fis);) {
byte[] b = new byte[1024];
int length = 0;
int num = dis.readInt();
System.out.println(num);
while ((length = dis.read(b)) != -1) {
String str = new String(b, 0, length);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test11() {
try (FileInputStream fis = new FileInputStream("src/a.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream("src/k.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);) {
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test10() {
try (FileReader fr = new FileReader("src/a.txt");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("src/j.txt", true);
BufferedWriter bw = new BufferedWriter(fw);) {
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test9() {
try (FileWriter fw = new FileWriter("src/i.txt"); BufferedWriter bw = new BufferedWriter(fw);) {
String str = "对于你我无话可说,可是你不知道什么是满足!";
bw.write(str);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test8() {
try (FileReader fr = new FileReader("src/a.txt"); BufferedReader br = new BufferedReader(fr);) {
char[] ch = new char[1024];
int length = 0;
while ((length = br.read(ch)) != -1) {
String str = new String(ch, 0, length);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test7() {
try (FileWriter fw = new FileWriter("src/h.txt");) {
String str = "无论什么时候都要坚强,因为你是家里的男子汉,未来的生活不要把自己陷入被动,无论什么时候你都要招架得住生活任何打击,没有原因,因为你是一个男人,你是家里的希望,你是家里的未来,你不能垮掉,你只能更加的坚强!";
fw.write(str);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test6() {
try (FileReader fr = new FileReader("src/g.txt");) {
char[] ch = new char[1024];
int length = 0;
while ((length = fr.read(ch)) != -1) {
String str = new String(ch, 0, length);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test5() {
try (FileInputStream fis = new FileInputStream("C:\\Users\\BW\\Desktop\\初始化.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("C:\\Users\\BW\\Desktop\\b.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);) {
byte[] b = new byte[1024];
int length = 0;
while ((length = bis.read(b)) != -1) {
bos.write(b, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test4() {
try (FileOutputStream fos = new FileOutputStream("src/g.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);) {
String str = "老爸我想你了,我们已经好长时间没见面了.";
bos.write(str.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test3() {
try (FileInputStream fis = new FileInputStream("src/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);) {
byte[] b = new byte[1024];
int length = 0;
while ((length = bis.read(b)) != -1) {
String str = new String(b, 0, length);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test2() {
try (FileOutputStream fos = new FileOutputStream("src/f.txt");) {
String str = "这个世界没有谁对不起谁,只是自己不够强大而已,对自己好点," + " 对别人再好最终还是是被他妈的当成理所应当.";
fos.write(str.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void test1() {
try (FileInputStream fis = new FileInputStream("src/a.txt");) {
byte[] b = new byte[1024];
int length = 0;
while ((length = fis.read(b)) != -1) {
String str = new String(b, 0, length);
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}