小白第一次来提问 100分不成敬意 图中划线部分在调试时报错,求原因及解决方法,另外想知道划线部分有什么用,能否用其他代码代替,或直接删掉 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像是读properties文件的,太模糊了 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();}} 那行代码作用是加载文件内容到Properties对象。报错原因可能是文件找不到,把fileName改成绝对路径试试看,或者改成可以找到的相对路径。 private static String fileName= "cus.inc"; 这一行跟文件的绝对路径,或者把这个文件放在项目下的下面 已重新发完代码,麻烦大神看看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=tomage=18home=hangzhou 大家好,,,怎么用JAVA解析OSM文件?谢谢!! 求教:服务器端内容变化如何实时通知到图形客户端?高手请进。 ArrayList容器中怎么根据输出Id删除对象元素 移动图片后坐标的获取问题 ror的问题 ★★★关于用java制作msn(或)qq界面的一些思考★★★ 如何将一篇txt文章内的文字都放入到一个string中?请会的人帮忙解答 网页中的applet老是说找不到类 谁有ISA的SN? 编写的脚本放到哪个盘里最好呢? api不会用 Java构造方法的问题
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();
}
}
报错原因可能是文件找不到,把fileName改成绝对路径试试看,或者改成可以找到的相对路径。
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