java日常笔记2016-12-27

1:File.separator

File file1 = new File (“C:\tmp\test.txt”);

File myFile = new File(“C:” + File.separator + “tmp” + File.separator, “test.txt”);


2:过滤器   FileFilter

File[] ff = f.listFiles(new FileFilter() {}

3:两种流(字节流、包装流)

is = new FileInputStream(f);
				
				int value = -1;
				
				while((value=is.read())!=-1){
					System.out.println((char)value);
				}

is = new FileInputStream(f);
				
				int value = -1;
				
				byte[] tong = new byte[512];
				
				while((value=is.read(tong)) !=-1){
					String string = new String(tong, 0, value,"GBK");
					System.out.println(string);
				}

4:类路径加载资源

第一种:获取类加载的根路径   D:\git\daotie\daotie\target\classes
File f = new File(this.getClass().getResource("/").getPath());

第三种:  file:/D:/git/daotie/daotie/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");

5:加载属性文件(properties文件、默认十六机制写中文会自动转码、写注释#)


6:

//资源路径有两种方式, 
//1:一种以 / 开头,则这样的路径是指定绝对路径,
//2:如果不以 / 开头, 则路径是相对与这个class所在的包的
public class Relation_Absolute {
	public static void main(String[] args) throws Exception {
		//绝对路径
		/*File file = new File("./src/test20161227/text.txt");
		System.out.println(file.length()); 
		System.out.println(file.getAbsolutePath());*/
		
		//相对路径
		/*InputStream is = Relation_Absolute.class.getClassLoader().getResourceAsStream("test20161227/text.txt");
		
		System.out.println(is.available());*/
		
		/*InputStream is = Relation_Absolute.class.getResourceAsStream("/test20161227/text.txt");
		
		System.out.println(is.available());*/
	}


7:

/*
 * 以  	字节流	 形式读取properties文件
 */
public class ReadPropertiesDemo_Stream {
	public static void main(String[] args) {
		String path = "test20161227/jdbc.txt";
		String content = getTxtContentFromPath(path);
		String[] rows = content.split("[\\u000a\\u000d]");
		Map<String,String> map = new HashMap<>();
		
		for (String row : rows) {
			if((row!=null && row.trim().length()>0) && !row.trim().startsWith("#")){
				String[] strs = row.split("=");
				String key = row.split("=")[0];
				if(strs.length<2){
					map.put(key, "");
				}else{
					String val = row.split("=")[1];
					map.put(key, val);
				}
			}
		}
		
		for (String key : map.keySet()) {
			System.out.println(key+":"+map.get(key)); 
		}
	}

	private static String getTxtContentFromPath(String path) {
		InputStream is = ReadPropertiesDemo_Stream.class.getClassLoader().getResourceAsStream(path);
		BufferedInputStream bis = new BufferedInputStream(is);
		byte[] bs = new byte[512];
		StringBuilder strb = new StringBuilder();
		
		while(true){
			
			try {
				int count =bis.read(bs);
				if(count==-1){
					break;
				}
				strb.append(new String(bs,0,count,"UTF-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return strb.toString();
	}

/*
 * 以	字符流	的形式读属性文件
 * 
 */
public class ReadPropertiesDemo_Reader {
	public static void main(String[] args) {
		try {
			String path = "test20161227/jdbc.txt";
			
			Map<String, String> map = new HashMap<>();
			
			InputStream is = ReadPropertiesDemo_Reader.class.getClassLoader().getResourceAsStream(path);
			
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			
			while(true){
				String row = br.readLine();
				
				if(row==null){
					break;
				}
				if((row!=null&&row.trim().length()>0)&&!row.trim().startsWith("#")){
					String[] strs = row.split("=");
					
					String key = row.split("=")[0];
					
					if(strs.length<2){
						map.put(key, "");
					}else{
						String val = row.split("=")[1];
						
						map.put(key, val);
					}
				}
			}
			for (String key : map.keySet()) {
				System.out.println(key+":"+map.get(key));
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

8:
/*
 * 字节流
 */
public class InputStreamDemo {
	public static void main(String[] args)  {
		//找到文件再说!!
		File file = new File("e://aa.txt");
		if(file.exists()){
			InputStream is = null;
			try {
				is = new FileInputStream(file);
				byte[] bs = new byte[512];
				int countOfRead = -1;
				while((countOfRead=is.read(bs))!=-1){
					String str = new String(bs,0,countOfRead,"GBK");
					System.out.print(str);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					if(is!=null){
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

/*
 * 输出流
 */
public class OutPutStreamDemo {
	public static void main(String[] args)  {
		//找到文件再说!!
		System.out.println("用铲更好用!!");
		File fileIn = new File("e://aa.txt");
		File dir = new File("e://images");
		if(!dir.exists()){
			dir.mkdir();
		}
		File fileOut = new File(dir,123456+".txt");
		long currentTimeMillis = System.currentTimeMillis();
		if(fileIn.exists()){
			InputStream is = null;
			OutputStream os = null;
			try {
				is = new FileInputStream(fileIn);
				os = new FileOutputStream(fileOut,false);
				byte[] bs = new byte[1024];
				int countOfRead = -1;
				while((countOfRead=is.read(bs))!=-1){
					os.write(bs, 0, countOfRead);
					os.flush();
				}
				System.out.println(System.currentTimeMillis() - currentTimeMillis);
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					if(is!=null){
						is.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				try {
					if(os!=null){
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

9:
/*
 * 基本的字符流读复制文本
 */
public class copy_print {
	public static void main(String[] args) throws Exception {
		File fileIn = new File("e://aa.txt");
		File fileOut = new File("e://a_gbk.txt");
		if (fileOut.exists()) {
			fileOut.delete();
		}
		fileOut.createNewFile();
		//先得到字节流,转换成字符流,这样可以设置编码
		Reader reader = new InputStreamReader(new FileInputStream(fileIn),"GBK");//new FileReader(fileIn,"GBK");//这个代码是错的,直接读取文本,不能指定编码
		BufferedReader br = new BufferedReader(reader);
		BufferedWriter out = new BufferedWriter(new FileWriter(fileOut));
		while(true){
			String txt = br.readLine();
			if(txt==null){
				break;
			}
			out.write(txt+"\r\n");
			out.flush();
			System.out.println(txt);
		}
		br.close();
		out.close();
	}

public static void main(String[] args) throws Exception {
		File fileIn = new File("e://aa.txt");
		File fileOut = new File("e://aaaa.txt");
		if (fileOut.exists()) {
			fileOut.delete();
		}
		fileOut.createNewFile();
		
		BufferedReader br = new BufferedReader(new FileReader(fileIn));
//		BufferedWriter out = new BufferedWriter(new FileWriter(fileOut));
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileOut)));
		while(true){
			String txt = br.readLine();
			if(txt==null){
				break;
			}
			System.out.println(txt);
			out.println(txt); 
			out.flush();//不能省!
		}
		br.close();
		out.close();
	}

10:FileReader:不能指定文件编码        inputstreamRedader可以指定编码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值