如何在Java中从文件读取数据到数组?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java中从文件读取数据到数组?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2404字,纯文字阅读大概需要4分钟。
内容图文
![如何在Java中从文件读取数据到数组?](/upload/InfoBanner/zyjiaocheng/708/08acf7fd9bf249d9869047ea9e07d923.jpg)
我需要一些帮助从文本文件中读取数据到我的ArrayList.创建并将ArrayList放入文本文件的第一部分非常有效.我只是在“标记”区域的最后需要一些帮助.
请注意,某些标识符使用我的母语.
public class ContAngajat {
String username;
String password;
}
public class CreazaCont {
// creating the arraylist and putting it into a file
public static void ang(String args[]) {
ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50);
Scanner diskScanner = new Scanner(in);
Scanner forn = new Scanner(in);
int n;
out.print("Introduceti numarul de conturi noi care doriti sa le introduceti: ");
n=forn.nextInt();
out.println();
try{
FileWriter fw = new FileWriter("ConturiAngajati.txt", true);
for(int i=0; i<n; i++){
ContAngajat cont = new ContAngajat();
out.print("Username: ");
cont.username=diskScanner.nextLine();
out.print("Password: ");
cont.password=diskScanner.nextLine();
angajati.add(cont);
fw.write(cont.username + " ");
fw.write(cont.password +"|");
}
fw.close();
}
catch(IOException ex){
System.out.println("Could not write to file");
System.exit(0);
}
for (int i=0; i<n; i++) {
out.println("username: " + angajati.get(i).username + " password: " +angajati.get(i).password );
}
}
// HERE I'M TRING TO GET THE ARRAYLIST OUT OF THE FILE
public static void RdAng(String args[]) {
ArrayList<ContAngajat> angajati=new ArrayList<ContAngajat>(50);
ContAngajat cont = new ContAngajat();
int count,i2,i;
try{
FileReader fr = new FileReader("ConturiAngajati.txt");
BufferedReader br = new BufferedReader(fr);
String line = "";
while((line=br.readLine())!=null) {
String[] theline=line.split("|");
count=theline.length;
for(i=0;i<theline.length;i++) {
String[] theword = theline[i].split(" ");
}
}
for(i2=0;i2<count;i2++) {
ContAngajat contrd = new ContAngajat();
// "ERROR" OVER HERE
for (int ird=0; ird <theword.length; ird++) {
cont.username=theword[0];
cont.password=theword[1];
// they keep telling me "theword cannot be resolved" whenever i try to run this
}
angajati.add(contrd);
}
}
catch(IOException ex){
System.out.println("Could not read to file");
System.exit(0);
}
}
}
编译错误是无法解决的.
解决方法:
they keep telling me “theword cannot be resolved” whenever I try to run this
这意味着该字未在范围中声明.您无法访问它以调用任何方法.他们做对了.您需要在更广泛的范围内声明该词,或者将依赖于该词的代码移动到声明它的范围内.也许你已经在if或while块中声明了它,并且你试图在它被声明的块之外使用它.那不行.
每当您清理代码时,都可以给出更详细的答案,以便更好地阅读.
内容总结
以上是互联网集市为您收集整理的如何在Java中从文件读取数据到数组?全部内容,希望文章能够帮你解决如何在Java中从文件读取数据到数组?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。