补 I/O基础_Test

1.system.in,system.out示范

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;

import org.junit.Test;
//文件:1.流式文件(有方向)2.记录式文件(无方向)
//文件的存取方法:1.顺序存取2.随机存取
//文件的使用:1.操作接口2.应用程序接口
//流的定义:有方向的进行流动,文件里面的数据进行操作,传输数据
//流一般采取缓冲区技术
//流的作用:传输数据
//流类: 字符流,字节流...
//文件类:File文件类,RandomAccessFile随机存取文件类
public class Systeminout {
@Test
public void demo1() {
InputStream in=System.in;
try {
for(int i=0;i<10;i++){
int b=in.read();//阻塞方法,等着输数据
System.out.println((char)b);
}
} catch (IOException e) {
e.printStackTrace();
}
//int a=10/0;
}
@Test//展示System.out的用法
public void demo2(){
PrintStream out=System.out;
for(int i=0;i<10;i++){
out.println("Hello,World!");
}
}


}


2.fileInputStream,fileOutPutStream用法

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class FileInOutputStreamDemo {

@Test
public void inDemo(){
try {
FileInputStream fin = new FileInputStream("d:/a.txt");
while(fin.available()>0){
int b = fin.read();
System.out.print((char)b);
}
fin.close();
//int c = fin.read();//流关闭之后不能再读,否则出异常!!!!!
//fin.close();//这里没问题。有的流在关闭之后不能再关闭
} catch (Exception e) {
e.printStackTrace();
}
}
@Test//循环读--法1
public void inDemo2() throws IOException{
byte buf[] = new byte[10];
FileInputStream fin = new FileInputStream("d:/a.txt");
while(fin.available()>0){
int len = fin.read(buf); //总共读了len个字节数据
for(int i=0;i<len;i++){
System.out.print((char)buf[i]);
}
}
}
@Test//循环读--法2
public void inDemo3() throws IOException{
byte buf[] = new byte[10];
FileInputStream fin = new FileInputStream("d:/a.txt");
int len=0;
while((len=fin.read(buf))!=-1){
for(int i=0;i<len;i++){
System.out.print((char)buf[i]);
}
}
}

@Test//※※※※以后开发时IO异常处理的模板※※※※
public void inDemo4(){
byte buf[] = new byte[512];
FileInputStream fin=null;
try {
fin = new FileInputStream("d:/a/a.log");
int len=0;
while((len=fin.read(buf))!=-1){
//下面方式可以处理中文
String str = new String(buf,0,len,"gbk");
System.out.print(str);
}
}catch (FileNotFoundException e) {
System.out.println("文件找不到!");
//e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读取失败!");
//e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(fin!=null){
fin.close();
}
} catch (IOException e) {
throw new RuntimeException("关流失败!", e);
}
}
}

///
@Test
public void outDemo(){
try {
FileOutputStream fout = new FileOutputStream("d:\\a\\dd.txt");//写新文件
//FileOutputStream fout = new FileOutputStream("d:\\a\\dd.txt",true);//追加方式写
byte buf[]={48,98,99,32,49,50,51,13,10,65,66};
fout.write(buf); //输出整个缓冲区的数据
//fout.write(buf, 0, 11);//输出缓冲区指定范围的数据

fout.close();
//fout.write(68);//流关闭之后不能再输出,否则出异常
} catch (Exception e) {
e.printStackTrace();
}

}

}

3.DateInPutStream,DateOutPutStream用法

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

public class DataInOutputStreamDemo {
//EOFException : End Of File Exception
@Test
public void inDemo() throws IOException{

//文件默认位置为当前项目根目录: 跟src并列位置
InputStream fin = new FileInputStream("a.txt");
DataInputStream din = new DataInputStream(fin); //流的套接

/*
while(din.available()>0){
int n = din.readInt();
System.out.println(n);
}
*/

for(int i=0;i<100;i++){
int n = din.readInt();
System.out.println(n);
}

double d = din.readDouble();
System.out.println("d:"+d);

din.close();
}

@Test
public void outDemo() throws IOException{
DataOutputStream dout = new DataOutputStream( new FileOutputStream("a.txt") );
for(int i=0;i<100;i++){
dout.writeInt(i);
}

dout.writeDouble(12.34);

dout.close();
}

@Test
public void FibnaciiDemoWriteDemo(){
try {
DataOutputStream dout = new DataOutputStream(new FileOutputStream("fib.txt"));
short i=0,j=1;
do{
dout.writeShort(i);
dout.writeShort(j);
i=(short) (i+j);
j=(short) (i+j);
}while(i>0);//如果符号位变成1,就为负数

dout.close();
} catch (IOException e) {
e.printStackTrace();
}
}


@Test //用available()方法来判断输入结束★★★
public void FibnaciiDemoReadDemo(){
try {
DataInputStream din = new DataInputStream( new FileInputStream("fib.txt") );
while(din.available()>0){
short n1 = din.readShort();
short n2 = din.readShort();
System.out.print(n1+" "+n2+" ");
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test //捕获异常控制输入结束★★★
public void FibnaciiDemoReadDemo2(){
try {
DataInputStream din = new DataInputStream( new FileInputStream("fib.txt") );
while(true){
short n1 = din.readShort();
System.out.print(n1+" ");
short n2 = din.readShort();
System.out.print(n2+" ");
}
} catch (EOFException e) {
System.out.println();
System.out.println("Over!");
}catch (Exception e) {
e.printStackTrace();
}
}


}

4.Objectstream用法

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.junit.Test;

public class ObjectStreamDemo {

@Test
public void writeDemo(){
Person p1 = new Person("Jack",22);
Person p2 = new Person("Tom",12);
Person p3 = new Person("Rose",23);
Person p4 = new Person("张三",31);
Person p5 = new Person("李四",32);

try {

ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("persons.dat"));
out.writeObject(p1);
out.writeObject(p2);
out.writeObject(p3);
out.writeObject(p4);
out.writeObject(p5);
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

//☆☆☆注意:对象流的读取必须采用捕捉异常的方式控制结束,不能采用available()>0
@Test
public void readDemo(){
ObjectInputStream in=null;
try {
in = new ObjectInputStream(new FileInputStream("persons.dat"));
while(true){
Person p = (Person) in.readObject();
System.out.println(p);
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (EOFException e) {
System.err.println("数据加载完毕....");
}catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
throw new RuntimeException("关流失败", e);
}
}
}

}
}

//☆☆☆用对象流写的对象必须要实现Serializable接口----贴标签技术
class Person implements Serializable{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
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;
}
@Override
public String toString() {
return name + ", " + age;
}

}

5.fileCopy

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;

public class fileCopy1 {
@Test //拷贝文件: 从一个文件读取数据,写到另一个文件,循环进行“边读边写”
public void test1() throws IOException{
//文本文件
FileInputStream fin=new FileInputStream("a.txt");
FileOutputStream fout=new FileOutputStream("aa.txt");


/*/二进制文件:图像、声音、可执行程序类
FileInputStream fin = new FileInputStream("d:/a/11.jpg");
FileOutputStream fout = new FileOutputStream("d:/11.jpg");*/
byte buf[]=new byte[128];
int len=0;
while((len=fin.read(buf))!=-1){
fout.write(buf,0,len);
}

fin.close();
fout.close();
}


@Test//拷贝文件变种--加密、解密: 从一个文件读取数据,把数据按某种规则改变一下,再写到另一个文件,循环进行“边读边写”
public void test2() throws IOException{
//文本文件
FileInputStream fin=new FileInputStream("a.txt");
FileOutputStream fout=new FileOutputStream("bb.txt");

byte buf[] = new byte[64];
int len = 0;
while( (len=fin.read(buf))!=-1){
//加密或解密
for(int i=0;i<len;i++){
buf[i]=(byte) (buf[i]-1);
}

fout.write(buf, 0, len);
}
fin.close();
fout.close();
}

@Test//网络其实也是一个IO的终端
public void test3() throws IOException{
URL url=new URL("http://www.hncu.net");
URLConnection con=url.openConnection();
InputStream in=con.getInputStream();
byte buf[] = new byte[512];
int len=0;
while((len=in.read(buf))!=-1){
String str = new String(buf,0,len,"utf-8");
System.out.print(str);
}
in.close();
}
}