java应用技术 3(3)

java应用技术
3(3)
一、内存操作流


案例:
package cn.java.java4;
//内存操作流
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;


public class ByteArrayDemo {


public static void main(String[] args) {
String info = "美国";
ByteArrayInputStream bas = new ByteArrayInputStream(info.getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int temp = 0;
while((temp=bas.read())!=-1){
char c = (char) temp;
bos.write(Character.toLowerCase(c));
}
System.out.println(bos.toString());
}


}
二、转换流:
1.将输出的字符流转化为字节流
案例:
package cn.java.java4;
//转换流
//将输出的字符流转化为字节流
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputStreamWriterDemo {
public static void main(String[] args) {
String path = "F:"+File.separator+"aa"+File.separator+"haoren.txt";
File file = new File(path);
String info = "我是中国人!!!";
try {
OutputStreamWriter ows = new OutputStreamWriter(new FileOutputStream(file));
ows.write(info);
ows.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
2.将输入的字节流转化为字符流
案例:
package cn.java.java4;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
//转换流
//将输入的字节流转化为字符流
public class InputStreamReaderDemo {
public static void main(String[] args) {
String path = "F:"+File.separator+"aa"+File.separator+"qq.txt";
File file = new File(path);
char[] c = null;
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));
c = new char[(int) file.length()];
isr.read(c);
isr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(c);
}
}
三、管道流:
案例:
package cn.java.java4;


import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
//管道流
public class PipedStreamDemo {
public static void main(String[] args) {
Popo po = new Popo();
Papa pa = new Papa();
try {
po.getPos().connect(pa.getPis());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread th1 = new Thread(po);
Thread th2 = new Thread(pa);
th1.start();
th2.start();
}
}
class Popo implements Runnable{
private PipedOutputStream pos = null;
private String info = "你这个东西";

public PipedOutputStream getPos() {
return pos;
}


public void setPos(PipedOutputStream pos) {
this.pos = pos;
}


public Popo() {
pos = new PipedOutputStream();
}


@Override
public void run() {
// TODO Auto-generated method stub
try {
pos.write(info.getBytes());
pos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
class Papa implements Runnable{
private PipedInputStream pis = null;


public PipedInputStream getPis() {
return pis;
}


public void setPis(PipedInputStream pis) {
this.pis = pis;
}


public Papa() {
pis = new PipedInputStream();
}


@Override
public void run() {
byte[] b =new byte[1024];
int len = 0;
try {
len=pis.read(b);
pis.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(new String(b,0,len));
}

}
四、打印流(分为字节打印流和字符打印流)


1.字节打印流和字符打印流
案例:
package cn.java.java4;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
//字节打印流和字符打印流
public class PrintStreamDemo {


public static void main(String[] args) {
// TODO Auto-generated method stub
/*try {
PrintStream ps = new PrintStream(path);
ps.print("我爱你");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
/*try {
PrintWriter pw = new PrintWriter(path);
pw.print("hyl shi shi shi");
pw.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/


File f = new File( "F:"+File.separator+"aa"+File.separator+"qq.txt");
//可以用字节打印流和字符打印流输出
/*PrintStream ps = null;
try {
ps = new PrintStream(new FileOutputStream(f));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String name = "张三";
int age = 20;
double score = 89.7;
char sex = 'F';
//字节打印流格式化输出
pw.printf("name:%s,age:%d,score:%f,sex:%c",name,age,score,sex);
pw.close();
}


}
五、System.out“标准”输出流
案例:
package cn.java.java4;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
//打印流
public class SystemDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
/*PrintStream a = System.out;*/
String path="F:"+File.separator+"aa"+File.separator+"qq.txt";
try {
System.setErr(new PrintStream(path));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.setOut(new PrintStream(path));
//System.setErr(new PrintStream(path));这里的new PrintStream("")如果输入地址将把System.out.println存入地址所在的文件
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
//e.printStackTrace();这里是把异常输出
SimpleDateFormat sdf= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
System.err.print(sdf.format(new Date())+":"+"FileNotFoundException");
}
System.out.println("asdfasdfasdfasdfasdfasdfasdfasf");
/* System.setOut(a);
System.out.println("asdfasdfasdfasdfasdfasdfasdfasf");*/
OutputStream o = System.out;
try {
o.write("haorenasdfasdfasdfasdfasdfasdfasfafd".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值