java笔记(一)Io

File
文件的创建
String path = "D:"+File.separator+"text"+File.separator+"test.text";
File f = new File(path);
try {
f.createNewFile( );
} catch (Exception e) {
e.printStackTrace();

}

//删除文件
if(f.exists()){//判断文件是否存在
f.delete();
}

//创建文件夹
File fdir = new File("D:"+File.separator+"文件夹");
fdir.mkdir();//创建文件夹
fdir.delete();//删除文件夹

//列出文件夹的内容
File flist = new File("D:"+File.separator);
String[] list1 = flist.list();
File[] list2 = flist.listFiles();
for(int i=0;i<list1.length;i++){
System.out.println(list1[i]);//只列出文件名
}
for(int i=0;i<list2.length;i++){
System.out.println(list2[i]);//列出完整的路径
}

//判断是不是文件夹
if(flist.isDirectory()){
System.out.print("文件夹");
}else{
System.out.print("不是文件夹");
}

字节流

输出字节流

File f = new File("d:"+File.separator+"text"+File.separator+"test1.text");
		//OutputStream out = new FileOutputStream(f);//文件存在就自动创建
		OutputStream out = new FileOutputStream(f,true);//加上ture后表示追加
		String n = "\r\nwo shi haoren好人";//\r\n表示换行
		byte[] b = n.getBytes();//
		out.write(b);
		out.close();
输出字节流
File f = new File("d:"+File.separator+"text"+File.separator+"test1.text");
		//OutputStream out = new FileOutputStream(f);//文件存在就自动创建
		OutputStream out = new FileOutputStream(f,true);//加上ture后表示追加
		byte[] b = n.getBytes();//(int) f.length()用来确定接收数组的长度
		//第一种方式
		//out.write(b);
		//第二种方式
		for(int i=0;i<b.length;i++){
			out.write(b[i]);
		}
		in.close();
		System.out.print(new String(b));
输入字节流
File f = new File("d:"+File.separator+"text"+File.separator+"test1.text");
	InputStream in = new FileInputStream(f);
	byte[] b = new byte[(int) f.length()];//(int) f.length()用来确定接收数组的长度
	//第一种读入方式
	//in.read(b);
	// int l = in.read(b);
		
	in.close();
	System.out.print(new String(b));
System.out.println(new String(c,0,l));//这里必须要限定长度,不然要出乱码
另外两种读入方式
File f = new File("d:"+File.separator+"text"+File.separator+"test1.text");
		InputStream in = new FileInputStream(f);
		byte[] b = new byte[(int) f.length()];//(int) f.length()用来确定接收数组的长度
		
		//第二种读入方式
		/*for(int i=0;i<b.length;i++){
			b[i] = (byte)in.read();
		}*/
		//第三种方式
		int i = 0;
		int temp =0;
		while((temp = in.read())!=-1 ){//当值为-1时,就表示读到文件的末尾
			b[i] = (byte)temp;
			i++;
		}
		in.close();
		System.out.print(new String(b));
	}

字符流
输出字符流
File f = new File("d:"+File.separator+"text"+File.separator+"test2.text");
		Writer out = new FileWriter(f,true);
		String t = "\r\n我爱我的祖国";
		out.write(t);
		out.close();
输入字符流
FileReader in = new FileReader(f);
		char[] c = new char[(int)f.length()];
		//第一种方式
		//int l=in.read(c);
		//第二种方式
		int temp;
		int l = 0;
		while((temp = in.read()) != -1){
			c[l] = (char)temp;
			l++;
		}
字节流和字符流的区别
字符流使用了缓冲区(即内存中的一块区域),可以用不关闭流,看是否会写到文件中为例来证明。
out.flush():强制清空缓冲区,这样在不关闭字符流的情况下也可以将数据写入到文件中。
在开发中字节流使用较多

内存操作流
String t = "woshihaoren";
		//通过ByteArrayInputStream的构造方法向内存写入数据
		ByteArrayInputStream bis = new ByteArrayInputStream(t.getBytes());
		//创建读内存的流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		int temp=0;
		
		while((temp = bis.read()) != -1){//注意这里是bis.read(),而不是bos.read()
			char c = (char) temp;
			bos.write(Character.toUpperCase(c));
		}
		System.out.print(bos.toString());
		bis.close();
		bos.close();

转换流
//将输出的字符流转化为字节流
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(f));
		String t = "woshihaoren";
		osw.write(t);
		osw.close();
		
		//将输入的字节流转化为字符流
		InputStreamReader isw = new InputStreamReader(new FileInputStream(f));
		char[] c = new char[(int)f.length()];
		int n = isw.read(c);
		isw.close();
		System.out.println(new String(c,0,n));

管道流

	public static void main(String[] args) {

		Person p = new Person();
		Spook s = new Spook();
		try {
			p.getPos().connect(s.getPis());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new Thread(p).start();
		new Thread(s).start();

	}
//发送端
class Person implements Runnable{
	private PipedOutputStream pos= null;
	public  Person(){
		pos = new PipedOutputStream();
	}
	public void run() {
		String l = "wo shi haoren";
		pos.write(l.getBytes());
		pos.close();						
	}
	public PipedOutputStream getPos(){
		return pos;	
	}
}

//接收端
class Spook implements Runnable{
	PipedInputStream pis = null;
	public Spook() {
		this.pis = new PipedInputStream();
	}
	public void run() {
		byte[] b = new byte[1024];
		int length = 0;
		length = pis.read(b);
		pis.close();
		System.out.print(new String(b,0,length));
	}
	
	public PipedInputStream getPis(){
		return pis;
	}	
}

打印流(分为字节打印流和字符打印流)
File f = new File("d:"+File.separator+"text"+File.separator+"test6.text");
//字节打印流
PrintStream ps = new PrintStream(new FileOutputStream(f));
		ps.print("我是好人");
		ps.print("我是好人aasdsad");


//字符打印流
PrintWriter pw  = new PrintWriter(new FileWriter(f));
		pw.print("haoren");
		pw.close();

打印流的格式化输出
File f = new File("d:"+File.separator+"text"+File.separator+"test6.text");
PrintStream ps = new PrintStream(new FileOutputStream(f));
PrintWriter pw  = new PrintWriter(new FileWriter(f));
String name = "张三";
		int age = 20;
		double score = 89.7;
		char sex = 'F';
		//字节打印流格式化输出
//		ps.printf("name:%s,age:%d,score:%f,sex:%c",name,age,score,sex);
//		ps.close();
		//字符打印流格式化输出
		pw.printf("name:%s,age:%d,score:%f,sex:%c",name,age,score,sex);
		pw.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值