好的,这是代码,我需要以某种方式从文本文件中取出一行并转换为数组对象。像 p[0] = "asdasdasd"
public class Patient2 {
public static void main(String args[])
{
int field = 0;
String repeat = "n";
String repeat1 = "y";
Scanner keyIn = new Scanner(System.in);
// FILE I/O
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("Patient.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
ArrayList<Patient1> patients=new ArrayList<Patient1>();
Patient1 p =new Patient1();
//set value to the patient object
patients.add(p);
System.out.println(p);
}
}
您可以将其添加到,而不是将其打印到控制台List<String>
List<String> lines = new ArrayList<String>();
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
lines.add(strLine)
}
Note:你的代码可以更干净,你可以在finally中处理关闭资源
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)