java 如何在java中读取字符串(文件)到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4332755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a String (file) to array in java
提问by Sundhas
Suppose there is a file named as SUN.txt
假设有一个名为 SUN.txt 的文件
File contains : a,b,dd,ss,
文件包含:a、b、dd、ss、
I want to make dynamic array depending upon the number of attributes in file. If ther is a char after comma then array will be of 0-4 i.e of length 5. In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.
我想根据文件中的属性数量创建动态数组。如果逗号后有一个字符,则数组将为 0-4,即长度为 5。在上述情况下,没有返回长度为 4 的 0-3 数组的 Char。我也想在逗号后读取 NULL。
How do i do that?
我怎么做?
Sundhas
圣达
回答by Markus Lausberg
You should think about
你应该考虑
- Reading the file into a String
- Splitting the file by separator ','
- Using a list for adding the characters and convert the list to an array, when the list is filled
- 将文件读入字符串
- 通过分隔符“,”分割文件
- 使用列表添加字符并将列表转换为数组,当列表被填充时
回答by Brendon Randall
As Markus said, you want to do something like this..
正如马库斯所说,你想做这样的事情..
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\SUN.txt")));
//The StringBuffer will be used to create a string if your file has multiple lines
StringBuffer sb = new StringBuffer();
String line;
while((line = reader.readLine())!= null)
{
sb.append(line);
}
//We now split the line on the "," to get a string array of the values
String [] store = sb.toString().split(",");
I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.
我不太明白为什么你想要逗号后的 NULL?我假设您的意思是在最后一个逗号之后,您希望数组中的值为空?我不太明白这一点,但这不是问题所在。
If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.
如果是这种情况,您不会读入 NULL,如果逗号后有一个空格,您可以读入。
If you would like a NULL you would have to add it in yourself at the end so you could do something like
如果你想要一个 NULL 你必须在最后自己添加它,这样你就可以做类似的事情
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\SUN.txt")));
//Use an arraylist to store the values including nulls
ArrayList<String> store = new ArrayList<String>();
String line;
while((line = reader.readLine())!= null)
{
String [] splitLine = line.split(",");
for(String x : splitLine)
{
store.add(line);
}
//This tests to see if the last character of the line is , and will add a null into the array list
if(line.endsWith(","))
store.add(null);
}
String [] storeWithNull = store.toArray();
回答by Jason Rogers
Well if you want want to simply open the file and store the content in a array of string then
好吧,如果您只想打开文件并将内容存储在字符串数组中,那么
1) open the file into a string
1) 将文件打开成字符串
2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
2)使用正则表达式“,”分割字符串http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
but I'm curious why you can't use a String file directly ?
但我很好奇为什么不能直接使用 String 文件?
回答by Andreas Dolk
For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:
对于您的数据结构,请使用数组列表。每个列表条目是文本文件的一行,每个条目是一个包含逗号分隔值的数组:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
data.add(line.split(","));
line = readNextLine();
}
(assuming, your file contains 1..n lines of comma separated values)
(假设您的文件包含 1..n 行逗号分隔值)
You may want to have it like this:
你可能想要这样:
"a,b,c,d," -> {"a", "b", "c", "d", null}
Here's a suggestion how to solve that problem:
以下是如何解决该问题的建议:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
String[] values = new String[5];
String[] pieces = line.split(",");
for (int i = 0; i<pieces.length; i++)
values[i] = pieces[i];
data.add(values);
line = readNextLine();
}
回答by chinni776
its seems like a CSV file something like this will work assuming it has 5 lines and 5 values
它看起来像一个 CSV 文件,假设它有 5 行和 5 个值,这样的文件会起作用
String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
while((line = br.readLine()) != null ){
StringTokenizer s = new StringTokenizer(line,",");
while (s.hasMoreTokens()){
value[row][col] = s.nextToken();
col++;
}
col = 0;
row++;
}
i havent tested this code
我还没有测试过这段代码
回答by Camilo Díaz Repka
Read the file, using BufferedReader
, one line at the time.
读取文件,使用BufferedReader
,一次一行。
Use split(",", -1)
to convert to an array of String[]
including also empty strings beyond the last comma as part of your array.
使用split(",", -1)
转换为数组String[]
还包括超越了最后一个逗号空字符串作为阵列的一部分。
Load the String[]
parts into a List
.
将String[]
零件装入List
.