如何读取和加载 java 中包含这些字符的文件?
how to read and load a file with these characters in java?
我有一个文件,在 empleados.txt 中有这个信息
“x002;彼得;Tor;1200.0;1992/4/4”
没有引号
只有当文件在“;”内时,我才能读取文件,如果我有“/”,我就无法读取
感谢
public void ReadFile(String nameFile) {
FileReader file;
BufferedReader reader = null;
String line;
try {
fichero = new FileReader(nombreFichero);
lector = new BufferedReader(fichero);
while ((linea = lector.readLine()) != null ) {
Employee temporalEmployee = new Employee();
GregorianCalendar calTemporal = new GregorianCalendar();
String[] temp = linea.split(";");
ns.setDni(temp[0]);
ns.setName(temp[1]);
ns.setlastName(temp[2]);
ns.setsalary((Float.parseFloat(temp[3])));
calTemporal.add(Calendar.YEAR, Integer.parseInt(temp[4]));
calTemporal.add(Calendar.MONTH, Integer.parseInt(temp[5]));
calTemporal.add(Calendar.DAY_OF_WEEK, Integer.parseInt(temp[6]));
ns.setDateOfHire(calTemporal);
System.out.println(linea);
ArraylistOfEmployee.add(ns);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (lector != null) {
lector.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
你应该替换这个:
calTemporal.add(Calendar.YEAR, Integer.parseInt(temp[4]));
calTemporal.add(Calendar.MONTH, Integer.parseInt(temp[5]));
calTemporal.add(Calendar.DAY_OF_WEEK, Integer.parseInt(temp[6]));
--- 到这个 ---
String[] temp2 = temp[4].split("/");
calTemporal.add(Calendar.YEAR, Integer.parseInt(temp2[0]));
calTemporal.add(Calendar.MONTH, Integer.parseInt(temp2[1]));
calTemporal.add(Calendar.DAY_OF_WEEK, Integer.parseInt(temp2[2]));
您可以对日期位使用 SimpleDateFormat 而不是将其分解。您当前的拆分会将所有日期保留为一个字段。
像这样:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd")
Date dateResult = format.parse(temp[4]));
我有一个文件,在 empleados.txt 中有这个信息 “x002;彼得;Tor;1200.0;1992/4/4” 没有引号
只有当文件在“;”内时,我才能读取文件,如果我有“/”,我就无法读取
感谢
public void ReadFile(String nameFile) {
FileReader file;
BufferedReader reader = null;
String line;
try {
fichero = new FileReader(nombreFichero);
lector = new BufferedReader(fichero);
while ((linea = lector.readLine()) != null ) {
Employee temporalEmployee = new Employee();
GregorianCalendar calTemporal = new GregorianCalendar();
String[] temp = linea.split(";");
ns.setDni(temp[0]);
ns.setName(temp[1]);
ns.setlastName(temp[2]);
ns.setsalary((Float.parseFloat(temp[3])));
calTemporal.add(Calendar.YEAR, Integer.parseInt(temp[4]));
calTemporal.add(Calendar.MONTH, Integer.parseInt(temp[5]));
calTemporal.add(Calendar.DAY_OF_WEEK, Integer.parseInt(temp[6]));
ns.setDateOfHire(calTemporal);
System.out.println(linea);
ArraylistOfEmployee.add(ns);
}
} catch (Exception e) {
e.printStackTrace();
}
try {
if (lector != null) {
lector.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
你应该替换这个:
calTemporal.add(Calendar.YEAR, Integer.parseInt(temp[4]));
calTemporal.add(Calendar.MONTH, Integer.parseInt(temp[5]));
calTemporal.add(Calendar.DAY_OF_WEEK, Integer.parseInt(temp[6]));
--- 到这个 ---
String[] temp2 = temp[4].split("/");
calTemporal.add(Calendar.YEAR, Integer.parseInt(temp2[0]));
calTemporal.add(Calendar.MONTH, Integer.parseInt(temp2[1]));
calTemporal.add(Calendar.DAY_OF_WEEK, Integer.parseInt(temp2[2]));
您可以对日期位使用 SimpleDateFormat 而不是将其分解。您当前的拆分会将所有日期保留为一个字段。
像这样:
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd")
Date dateResult = format.parse(temp[4]));