package test5;
import org.junit.Test;
import java.io.*;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author abu
* @create 2019/7/30
* @since 1.0.0
*/
public class TestExer {
public String nextString(){
String str = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
return str;
}
}
public int nextInt(){
return Integer.parseInt(nextString());
}
public boolean nextBoolean(){
return Boolean.parseBoolean(nextString());
}
@Test
public void test0(){
TestExer te = new TestExer();
System.out.println("请输入字符串");
System.out.println(te.nextString());
System.out.println("请输入布尔值");
System.out.println(te.nextBoolean());
System.out.println("请输入整数");
System.out.println(te.nextInt() + 1);
}
@Test
//使用字节流实现输出
public void test1(){
BufferedOutputStream bos = null;
try {
bos = new BufferedOutputStream(new FileOutputStream(new File("user4.txt")));
String str = null;
str = "山外青山楼外楼\n西湖歌舞几时休\n暖风熏得游人醉\n却把杭州作汴州";
bos.write(str.getBytes());
bos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
//使用字符流实现输出
public void test2(){
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File("user5.txt")));
String str = null;
str = "少年握槊\n气凭陵\n酒圣诗豪余事";
bw.write(str);
bw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
//字符流实现输入
public void test3(){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File("user4.txt")));
String str = null;
while ((str = br.readLine()) != null){
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
//字节流实现输入
public void test4(){
BufferedInputStream bis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("user4.txt"));
bis = new BufferedInputStream(fis);
int len;
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1){
String str = new String(b,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
//文件的复制
public void test5(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("user4.txt")));
bw = new BufferedWriter(new FileWriter(new File("user6.txt")));
char[] c = new char[20];
int len;
while((len = br.read(c)) != -1){
bw.write(c,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2、对象输入输出
package test5;
import org.junit.Test;
import test2.Person;
import java.io.*;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author abu
* @create 2019/7/31
* @since 1.0.0
*/
public class TestObjectInputOutStream {
@Test
public void testObjectOutputStream(){
Person p1 = new Person("summer",18);
Person p2 = new Person("forever",10);
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("person.txt")));
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testObjectInputStream(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(new File("person.txt")));
Person p1 = (Person)ois.readObject();
System.out.println(p1);
Person p2 = (Person)ois.readObject();
System.out.println(p2);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}