java文件流处理 读取 配置文件

 

                try {
                    FileWriter filewriter = new FileWriter(
                            new File("f:/1.txt"), true);
                    String content = text_1.getText();
                    filewriter.write(content);
                    filewriter.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }

 

 

        InputStream in = Config.class.getResourceAsStream("/config/config.properties");
        try {
            p.load(in);
        } catch (Exception e) {
            throw new RuntimeException("error when try read config:"+property);
        }

 

配置文件应该放在/src/config/config.properties   

/config/config.properties    第一个/指的应该就是classpath的root, 即源码的src目录

 

 

 

 

                File f2 = new File("E:/99.inc");
                InputStreamReader read = null;
                try {
                    read = new InputStreamReader(new FileInputStream(f2),
                            "UTF-8");
                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                BufferedReader reader2 = new BufferedReader(read);
                String line2;
                try {
                    while ((line2 = reader2.readLine()) != null) {
                        System.out.println(line2);
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

 

 

 

        File f = new File("/Users/aaa.txt");
        String strContent = "";
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(f));
            String line;
            while ((line = reader.readLine()) != null) {
                strContent += line;
            }
            System.out.println(strContent);
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

 

 

utf-8   

BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); 

 

 

 

package com.lich;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
	public static void main(String[] args) {
        File f = new File("c:\\2222.csv");
        BufferedReader reader = null;
        try {
        	int linenum = 0;
        	InputStream fis = new FileInputStream("c:\\2222.csv");
            InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-16"));
            reader = new BufferedReader(isr);
            String line;
            while ((line = reader.readLine()) != null) {
            	linenum++;
            	int count = matchCount(line, "222/0");
            	if(count!=2){
            		System.out.print(linenum+"   "+line.length()+"   ");
                	
                	System.out.print(line);
                	System.out.println(count);
            	}
            	
            }
            
        } catch (FileNotFoundException e2) {
            e2.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if (reader!=null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
	}
	
	
	public static int matchCount(String str, String findStr){
		Pattern p = Pattern.compile(findStr);
		Matcher m = p.matcher(str);
		int count = 0;
		while (m.find()){
		    count +=1;
		}
		return count;
	}
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值