//读取配置文件(a.properties)
import java.util.Set;   
import java.util.Iterator;   
import java.util.Properties;   
  
import java.io.IOException;   
import java.io.InputStream;   
  
class ReadProperties {   
 static Properties props = new Properties();  
    static{   
  
        ReadProperties test = new ReadProperties();   
        InputStream in = test.getClass().getResourceAsStream("/a.properties");;   
  
        
        try{   
            props.load(in);   
        } catch(IOException e){   
            e.printStackTrace();   
        }   
 
}   
//配置文件(a.properties)
A1(13)=E:/program/file/A100(28).txt
A1(24)=E:/program/file/A200(29).txt
//根据配置(a.properties)读取A100(28).txt和A200(29).txt文件。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class ReadFile { /**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args){
// TODO Auto-generated method stub
File file = null;
//String c = null;
String[] b= new String[]{"A100(28)","A200(29)"};
String d= null;
for(String a:b){
System.out.println(ReadProperties.props.getProperty(a));
d = ReadProperties.props.getProperty(a);
}
file = new File(ReadProperties.props.getProperty(d));
if(!file.exists()){
System.out.println("对不起文件不存在!");
}

BufferedReader br = null;
//StringBuffer sb = new StringBuffer("");
FileReader fr = null;
try{
br = new BufferedReader(new FileReader(file));
String s = null;
 while ((s = br.readLine())!=null) {
              System.out.println(s.toString());
            }

}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(br != null && fr != null)
            {
                try {
                    br.close();
                    br = null;
                    fr.close();
                    fr = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } }

}}

解决方案 »

  1.   


    //大家帮忙看看为什么只能输出A200(29).txt中的数据呢?谢谢
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    public class ReadFile { /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args){
    // TODO Auto-generated method stub
    File file = null;
    //String c = null;
    String[] b= new String[]{"A100(28)","A200(29)"};
    String d=null;
    for(String a:b){
    //System.out.println(ReadProperties.props.getProperty(a));
    d = ReadProperties.props.getProperty(a);
    System.out.println(d);
    }
    file = new File(d);
    if(!file.exists()){
    System.out.println("对不起文件不存在!");
    }

    BufferedReader br = null;
    //StringBuffer sb = new StringBuffer("");
    FileReader fr = null;
    try{
    br = new BufferedReader(new FileReader(file));
    String s = null;
     while ((s = br.readLine())!=null) {
                  System.out.println(s.toString());
                }

    }catch(FileNotFoundException e){
    e.printStackTrace();
    }catch(IOException e){
    e.printStackTrace();
    }finally{
    if(br != null && fr != null)
                {
                    try {
                        br.close();
                        br = null;
                        fr.close();
                        fr = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } }

    }}
      

  2.   

     for(String a:b){
                //System.out.println(ReadProperties.props.getProperty(a));
                d = ReadProperties.props.getProperty(a);
                System.out.println(d);
            }
            file = new File(d);
            if(!file.exists()){
                System.out.println("对不起文件不存在!");
            }===》
     for(String a:b){
                //System.out.println(ReadProperties.props.getProperty(a));
                d = ReadProperties.props.getProperty(a);
                System.out.println(d);
                file = new File(d);
            if(!file.exists()){
                System.out.println("对不起文件不存在!");
            }
            }
            
      

  3.   

    把 file = new File(d);后面的内容放到循环里面去。
      

  4.   


    放到循环里也不对啊!在控制台也输出不了A100(28).txt文件中的内容哦
      

  5.   


    public static void main(String[] args){
            // TODO Auto-generated method stub
            File file = null;
            //String c = null;
            String[] b= new String[]{"A100(28)","A200(29)"};
            String d=null;
            for(String a:b){
                //System.out.println(ReadProperties.props.getProperty(a));
                d = ReadProperties.props.getProperty(a);
                System.out.println(d);
               file = new File(d);
            if(!file.exists()){
                System.out.println("对不起文件不存在!");
            }
            
            BufferedReader br = null;
            //StringBuffer sb = new StringBuffer("");
            FileReader fr = null;
            try{
                br = new BufferedReader(new FileReader(file));
                String s = null;
                 while ((s = br.readLine())!=null) {
                      System.out.println(s.toString());
                    }
                
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }finally{
                if(br != null && fr != null)
                {
                    try {
                        br.close();
                        br = null;
                        fr.close();
                        fr = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }        }
            }
                    
        }
      

  6.   


    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.ResourceBundle;public class ConfigTest {

    public static ArrayList getSystemConfig() {
    ArrayList al = new ArrayList();
    ResourceBundle rs = null;
    try {
    rs = ResourceBundle.getBundle("a");
    Enumeration enu = rs.getKeys();
    while(enu.hasMoreElements()) {
    al.add(rs.getString(enu.nextElement().toString()));
    }

    } catch (Exception e) {
                 e.printStackTrace();
    }
    return al;
    }
    }//根据配置(a.properties)读取A100(28).txt和A200(29).txt文件。
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;public class Test { /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    File file = null;
    ArrayList al = ConfigTest.getSystemConfig(); for(int i = 0;i<al.size();i++) {
    file = new File(al.get(i).toString());
    if (!file.exists()) {
    System.out.println("对不起文件不存在!");
    } else {
    BufferedReader br = null;
    // StringBuffer sb = new StringBuffer("");
    FileReader fr = null;
    try {
    br = new BufferedReader(new FileReader(file));
    String s = null;
    while ((s = br.readLine()) != null) {
    System.out.println(s.toString());
    } } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (br != null && fr != null) {
    try {
    br.close();
    br = null;
    fr.close();
    fr = null;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    }
    }
    }
    }