core java--day19(FileInputStream,FileOutputStream)

复习:
    File: 电脑上文件的抽象表现形式
    isFile();
    isDirectory();
    length();
    createNewFile();
    mkdir();
    mkdirs();
    list();
    listFiles(); //返回File
    delete();
    renameTo(); //重命名,移动
    getName();
    getPath();
    getCanonicalPath();
    new File();
    getParent();


    3.3.2: FileInputStream:读取文件中的数据放到这个流中。
           3.3.2.1构造器1:new FileInputStream("文件路径");//读取字符串路径所表示文件的数据
           3.3.2.2构造器2:new FileInputStream(new File("文件路径"));//读取文件的数据
           
        3.3.3: FileOutputStream: 把输出流的数据输出到文件中。
           3.3.3.1构造器1:new FileOutputStream(new File"文件路径");//把流中的数据输出到文件
           3.3.3.2构造器2:new FileOutputStream(new File("文件路径"),是否追加);//读取文件的数据,true-->本次输出的数据在文件末尾追加 ;false-->不追加,覆盖。

package com.briup.ch19;

import java.io.FileInputStream;

/**
 * 读取文件数据的时候统计读取的字节
 * 37
 * */
public class Test2 {
	public static void main(String[] args) throws Exception {
		FileInputStream fis = new FileInputStream("abd.txt");
		//统计读取了多少字节
		int count = 0;
		//接收读取的数据
		int da = 0;
		while( (da = fis.read())!=-1 ) {
			//10 回车 \r 
			if(da==10) {
				break;
			}
			System.out.write(da);
			System.out.flush();
			count++;
		}
		System.out.println("读取了:"+count);
		
		fis.close();
	}
}











package com.briup.ch19;

import java.io.FileInputStream;

/**
 * 多个线程读取同一个文件<br>
 * 46
 */
public class Test3 {
	public static void main(String[] args) throws Exception {
		//开启线程
		My3 t = new My3();
		t.start();
		
		
		FileInputStream fis = new FileInputStream("abd.txt");
		int da = 0;
		while ((da = fis.read()) != -1) {
			System.out.write(da);
			System.out.write(13);// 回车
			System.out.write(10);// 换行
			System.out.flush();
		}
		fis.close();
	}
}

class My3 extends Thread {
	@Override
	public void run() {
		try {
			FileInputStream fis = new FileInputStream("abd.txt");
			int da = 0;
			while ((da = fis.read()) != -1) {
				System.out.write(9);// tab \t
				System.out.write(da);
				System.out.write(13);// 回车
				System.out.write(10);// 换行
				System.out.flush();
			}
			fis.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
package com.briup.ch19;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

//把一个文件的内容读取到另一个文件中
//1end
public class Test7 {
	public static void main(String[] args) throws Exception {
		//要读取的数据
		File file = new File("2.png");
		//要写出的位置
		File fileOut = new File("20.png");
		
		FileInputStream fis 
			= new FileInputStream(file);
		
		FileOutputStream fos =
				new FileOutputStream(fileOut);
		
		//循环读取
		int da = 0;
		while( (da = fis.read())!=-1 ) {
			if(da<100) {
				fos.write(da);
			}
			fos.flush();
		}
		fos.close();
		fis.close();
	}
}
package com.briup.ch19;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import javax.management.relation.Relation;

public class Test9 {
	
	public static void mainold(String[] args) throws Exception {
		FileInputStream fis 
			= new FileInputStream("a.txt");
		
		String readLine = readLine(fis);
		System.out.println(readLine);
		System.out.println(readLine.length());//22
		
	}
	
	public static void main(String[] args) throws Exception {
		File file = 
			new File("src/com/briup/ch19/temp");
		
		FileInputStream fis = 
				new FileInputStream(file);
		//用来接收第一行数据
		String one = readLine(fis);
		//计算 前四行读取到了多少字节
		int count  = one.length();
		//读取剩下3行
		for(int i = 0;i<3;i++) {
			String readLine = readLine(fis);
			count+=readLine.length();
		}
		//154
		System.out.println("前四行数据长度为 :"+count);//27
		//文件长度-前四行字节-最后一行字节=zip所有字节长度
		
		int len= (int)(file.length()-count-one.length()-2);
		//压缩包中所有字节都读取到这个数组中
		byte[] but = new byte[len];
		//把数据读取到字节数组中
		fis.read(but);
		fis.close();
		
		//构建输出流
		FileOutputStream fos 
			= new FileOutputStream("D://e.zip");
		fos.write(but);
		fos.flush();
		fos.close();
		
		
	}
	
	
	
	/**
	 * 一次读取一行数据<Br>
	 * @param in 输入流
	 * @return 返回当前读取到的一行数据
	 * */
	public static String readLine(InputStream in) {
		try {
			//当前这行的字节
			byte[] but  = new byte[1024*1024];
			//计算当前这行的字节数
			int index = 0;
			//标记是否已经读取到了 13 回车
			boolean one = false;
			//标记是否已经读取到了 10  换行
			//boolean two = false;
			
			int da = 0;
			while( (da = in.read())!=-1 ) {
				but[index++] = (byte) da;
				if(13==da) {//读取到回车
					one = true;
				}else if(10==da && one) {
					break;
				}
			}
			return new String(but,0,index);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
age=10
#注释
name=tom123111
package com.briup.ch19;

import java.io.FileReader;
import java.util.Properties;
import java.util.Set;

//测试properties配置文件	50
public class Test10 {
	public static void main(String[] args) throws Exception {
		//创建一个空的properties对象
		Properties p = new Properties();
		//加载.properties文件,获取里面的key-value数据
		p.load(new FileReader("src/com/briup/ch19/a.properties"));
		
		//查看properties对象中所有的key-value数据
		p.list(System.out);
		
		//获取某一个key对应的value值
		String name = p.getProperty("name");
		System.out.println(name);
		//感受代码灵活
		
		//遍历properties
		//获取properties对象中所有的key
		Set<String> key = p.stringPropertyNames();
		for(String k : key) {
			System.out.println(k+"--"+p.getProperty(k));
		}
	}
}
package com.briup.ch19;

import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Properties;

import com.briup.ch12.SayHello;

/**
 * 模仿浏览器发请求访问代码<br>
 *.user 从 b.properteis文件中获取.user对应的类权限名
 *反射加载全限定名,反射调用固定的show方法<br>
 *前提show方法需要添加注解(@SayHello)<br>
 * */
public class Test11 {
	public static void main(String[] args) throws Exception {
		//模仿当前发生的请求
		String url = "/user";
		//从配置文件中 获取 处理该请求的类
		//创建配置文件
		Properties p = new Properties();
		p.load(new FileReader("src/com/briup/ch19/b.properties"));
		//判断配置文件中是否包含url,
		boolean containsKey = p.containsKey(url);
		if(containsKey) {
			//获得url对应的处理类 全限定名
			String className = p.getProperty(url);
			Class<?> cl = Class.forName(className);
			//获得cl下的show方法
			Method show = cl.getMethod("show");
			//获得show方法上的SayHello注解
			//如果没有 返回null
			SayHello sayHello = show.getAnnotation(SayHello.class);
			if(sayHello==null) {
				throw new Exception("请在"+show.getName()+"上添加@SayHello");
			}
			//执行show方法
			show.invoke(cl.newInstance());
		}
	}
}





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值