[英]Read a text file to an array Java
I know there are many questions about reading text files here but I have gone through all of them and I think I'm having some difficulty with syntax or SOMETHING because nothing that I've been trying has been working at all. 我知道这里有很多有关读取文本文件的问题,但是我已经遍历了所有这些问题,我认为我在语法或某些方面遇到了一些困难,因为我一直在尝试的所有内容都没有起作用。
What I'm attempting to do is this: 我正在尝试做的是这样的:
1) read a text file inputed by user
2) copy each individual line into an array, so each line is its own element in the array
I feel like I am very close but for some reason I can't figure out exactly how to get it to work! 我觉得我已经很接近了,但是由于某种原因,我无法弄清楚如何使其正常工作!
Here is the relevant code I have right now: 这是我现在拥有的相关代码:
I keep getting out of bounds exceptions in three locations which I've marked off. 我一直在我标记的三个位置超出范围例外。
Been working on this for quite a while not sure what to do next! 已经为此工作了很长时间,不确定下一步该怎么做! Any ideas? 有任何想法吗?
import java.io.IOException;
import java.util.Scanner;
public class FindWords {
public static void main (String args[]) throws IOException{
FindWords d = new Dictionary();
((Dictionary) d).dictionary(); //********* out of bounds here
}
/**
* Validates and returns the dictionary inputed by the user.
*
* @param
* @return the location of the dictionary
*/
public static String getDict(){
///////////////////ASK FOR DICTIONARY////////////////////
System.out.println("Please input your dictionary file");
//initiate input scanner
Scanner in = new Scanner(System.in);
// input by user
String dictionary = in.nextLine();
System.out.println("Sys.print: " + dictionary);
//make sure there is a dictionary file
if (dictionary.length() == 0){
throw new IllegalArgumentException("You must enter a dictionary");
}
else return dictionary;
}
}
which calls on the class Dictionary: 调用类Dictionary:
import java.io.*;
public class Dictionary extends FindWords{
public void dictionary () throws IOException{
String dict = getDict();
String[] a = readFile(dict); //********** out of bounds here
int i = 0;
while(a[i] != null){
System.out.println(a[i]);
i++;
}
}
public static String[] readFile(String input) throws IOException{
//read file
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
System.out.println ();
int count = 0;
String[] array = new String[count];
try{
while (br.readLine() != null){
array[count] = br.readLine(); //********out of bounds here
count++;
}
br.close();
}
catch (IOException e){
}
return array;
}
}
Thank you for looking! 感谢您的光临!
Edit: Just fyi: I have my .txt file in the parent project folder. 编辑:只是:我在父项目文件夹中有我的.txt文件。
Have you tried this?: 您尝试过吗?:
List<String> lines = Files.readAllLines(Paths.get("/path/to/my/file.txt"));
and then transform your list to an array if you want: 然后根据需要将列表转换为数组:
String[] myLines = lines.toArray(new String[lines.size()]);
You are initializing a zero-length array, hence the exception on the first iteration: 您正在初始化一个零长度的数组,因此第一次迭代是一个例外:
int count = 0;
String[] array = new String[count];
Since you probably don't know the expected size, work with a List
instead: 由于您可能不知道预期的大小,请改用List
:
List<String> list = new ArrayList<>();
String thisLine = null;
try{
while ((thisLine = br.readLine()) != null) {
list.add(thisLine);
}
}
You can get the total size afterwards by: 您可以通过以下方式获得总大小:
list.size();
Or even better, go with morganos solution and use Files.readAllLines()
. 甚至更好的是,使用morganos解决方案并使用Files.readAllLines()
。
You start with an array size of zero... 您从零开始的数组大小开始...
int count = 0;
String[] array = new String[count];
Several issues here : 这里有几个问题:
while
loop, you're making 2 calls to br.readLine()
, so basically you're skipping one line out of 2. 在while
循环中,您要对br.readLine()
进行2次调用,因此基本上是从2中跳过一行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.