我在将数组值传递给构造函数时遇到了一些问题。我有一个file.txt
包含一些值的行,例如:
彼得|柏林|德国|0930295235|富。
我想出了如何将线条转换为数组。但我不知道如何将数组中的值传递给构造函数,以将数组转换为具有数组属性的对象。
这是主要课程:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class FileReader {
public static void main(String[] args) {
try {
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("file.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
// Read the file line by line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
String s[] = strLine.split("\\|");
// System.out.println(java.util.Arrays.toString(s));
// System.out.println (strLine);
for (String element : s) {
System.out.println(element);
// HERE I NEED TO PASS THE ARRAY VALUES FOR EACH LINE TO TRANSFORM THE ARRAYS TO OBJECTS
Item item = new Item(element,element,element,element,element);
System.out.println("*************************************");
}
// Close the input stream
in.close();
} catch (Exception e) { //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
这是类项目:
public class Item {
private String name;
private String city;
private String state;
private String number;
private String foo;
public Object(String name, String city, String state, String number,String foo){
this.name = name;
this.city = city;
this.state = state;
this.number = number;
this.foo = foo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
}
我需要对象来更多地处理数据。我很感激任何帮助。