图中划线部分在调试时报错,求原因及解决方法,另外想知道划线部分有什么用,能否用其他代码代替,或直接删掉

解决方案 »

  1.   

    好像是读properties文件的,太模糊了
      

  2.   

    package util;
    import java.io.FileReader;
    import java.io.PrintStream;
    import java.util.Properties;
    import javax.swing.JOptionPane;
    import vo.Customer;
    public class FileOpe{
    private static String fileName= "cus.inc";
    private static Properties pps;
    static{
    pps= new Properties();
    FileReader reader=null;
    try{
    reader= new FileReader(fileName);
    pps.load(reader);        //就是这行
    }catch(Exception ex){
    JOptionPane.showMessageDialog(null, "文件操作异常");
    System.exit(0);
    } finally{
    try{
    reader.close();
    } catch(Exception ex) { }
    }
    }
    private static void listInfo(){
    PrintStream ps= null;
    try{
    ps= new PrintStream(fileName);
    pps.list(ps);
    }catch(Exception ex){
    JOptionPane.showMessageDialog(null, "文件操作异常");
    System.exit(0);
    }finally{
    try{
    ps.close();
    } catch(Exception ex){ }
    }
    }
    public static void getInfoByAccount(String account) {
    Customer cus=null;
    String cusInfo= pps.getProperty(account);
    if(cusInfo!=null) {
    String[] infos= cusInfo.split("#");
    cus=new Customer();
    cus.setAccount(account);
    cus.setPassword(infos[0]);
    cus.setName(infos[1]);
    }
    }
    public static void updateCustomer(String account,String password,
    String name) {
    pps.setProperty(account, password+ "#" +name);
    listInfo();
    }
    }
      

  3.   

    那行代码作用是加载文件内容到Properties对象。
    报错原因可能是文件找不到,把fileName改成绝对路径试试看,或者改成可以找到的相对路径。
      

  4.   

    private static String fileName= "cus.inc"; 这一行跟文件的绝对路径,或者把这个文件放在项目下的下面
      

  5.   

    已重新发完代码,麻烦大神看看
    private static String fileName= "cus.inc";应该是cus.inc这个文件你的项目中没有,所以报错了。你只要创建一个文件把文件名传给filename。
                    String fileName="cus.inc";
    Properties properties = new Properties();
    FileReader reader=null;

    try{
    reader= new FileReader(fileName);
    properties.load(reader);       
    }catch(Exception ex){
    System.exit(0);
    } finally{
    try {
    reader.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    System.out.println(properties.getProperty("username"));
    System.out.println(properties.getProperty("age"));
    System.out.println(properties.getProperty("home"));
                    properties.setProperty("age", "20");
    System.out.println(properties.getProperty("age"));我的cus.inc里面内容为
    username=tom
    age=18
    home=hangzhou