将逗号输入的文件分割成数组Java,重新写入文件
Split file input by comma into an array Java, and re-writing to the file
假设我有一个文件 text.txt 在一行中包含以下内容:
abc, def, ghi, jkl
我正在尝试获取文件中的数据列表,因此 myList 将生成 ["abc"、"def"、"ghi"、"jkl"],但它似乎不起作用:
String[] tokens;
try {
Scanner scanner = new Scanner("text.txt");
while (scanner.hasNext()) { tokens = scanner.nextLine().split(","); }
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tokens.length); // Prints 1 instead of 4
尝试打印索引 0-3 会出现越界错误(显然是因为长度仅为 1)
然后我想更改其中一个索引(在数组中)中的数据,然后 re-write/over-write 将其返回到文件中。
我认为这个答案应该适用于此,但前提是我可以正确设置我的数组。
编辑:目前,我知道我的文件只有 4 个东西,用 3 个逗号分隔,如示例文本文件所示。
你正在为每一行覆盖 tokens
(我认为你有一个空行)
尝试
String[] tokens;
try {
scanner = new Scanner(new File ("text.txt"));
while (scanner.hasNextLine()) // change this
{
tokens = scanner.nextLine().split(",");
System.out.println(tokens.length);
}
} catch (IOException e) {
e.printStackTrace();
}
当你声明一个数组时,你需要指定它的大小。然后您可以将标记分配到数组的特定位置。
String[] myStringArray = new String[3];
myStringArray[0] = "sample string";
你的问题是你真的不知道你的数组将包含多少元素。所以最好的解决方案是使用 ArrayList
ArrayList<String> myStringArrayList = new ArrayList<String>();
myStringArrayList.add("sample string");
尝试将其合并到您的代码中。
您的代码表明您的循环将 运行 直到文件结束,并且 return 文件的最后一行。你能检查一下这是否是你需要的吗?也许你的最后一行不包含“,”。
编辑:您没有阅读 text.txt
文件。使用 new Scanner("text.txt")
你正在使用构造函数 Scanner(String source)`, while what you want is Scanner(File source)
我想这就是您要使用 ArrayList 寻找的东西:
ArrayList<String> tokens = new ArrayList<String>();
try {
File file = new File("text.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String[] str= scanner.nextLine().split(",");
for(int i=0;i<str.length;i++){
tokens.add(str[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tokens.size());
感谢宝贵的意见,但我相信我已经解决了我的问题:
String[] tokens;
try {
Scanner scanner = new Scanner("text.txt");
tokens = scanner.nextLine().split(",");
} catch (IOException e) {
e.printStackTrace();
}
我会 select 如果我得到一些赞成票来证明我的解决方案确实是(其中之一)正确的答案!
假设我有一个文件 text.txt 在一行中包含以下内容:
abc, def, ghi, jkl
我正在尝试获取文件中的数据列表,因此 myList 将生成 ["abc"、"def"、"ghi"、"jkl"],但它似乎不起作用:
String[] tokens;
try {
Scanner scanner = new Scanner("text.txt");
while (scanner.hasNext()) { tokens = scanner.nextLine().split(","); }
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tokens.length); // Prints 1 instead of 4
尝试打印索引 0-3 会出现越界错误(显然是因为长度仅为 1)
然后我想更改其中一个索引(在数组中)中的数据,然后 re-write/over-write 将其返回到文件中。 我认为这个答案应该适用于此,但前提是我可以正确设置我的数组。
编辑:目前,我知道我的文件只有 4 个东西,用 3 个逗号分隔,如示例文本文件所示。
你正在为每一行覆盖 tokens
(我认为你有一个空行)
尝试
String[] tokens;
try {
scanner = new Scanner(new File ("text.txt"));
while (scanner.hasNextLine()) // change this
{
tokens = scanner.nextLine().split(",");
System.out.println(tokens.length);
}
} catch (IOException e) {
e.printStackTrace();
}
当你声明一个数组时,你需要指定它的大小。然后您可以将标记分配到数组的特定位置。
String[] myStringArray = new String[3];
myStringArray[0] = "sample string";
你的问题是你真的不知道你的数组将包含多少元素。所以最好的解决方案是使用 ArrayList
ArrayList<String> myStringArrayList = new ArrayList<String>();
myStringArrayList.add("sample string");
尝试将其合并到您的代码中。
您的代码表明您的循环将 运行 直到文件结束,并且 return 文件的最后一行。你能检查一下这是否是你需要的吗?也许你的最后一行不包含“,”。
编辑:您没有阅读 text.txt
文件。使用 new Scanner("text.txt")
你正在使用构造函数 Scanner(String source)`, while what you want is Scanner(File source)
我想这就是您要使用 ArrayList 寻找的东西:
ArrayList<String> tokens = new ArrayList<String>();
try {
File file = new File("text.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String[] str= scanner.nextLine().split(",");
for(int i=0;i<str.length;i++){
tokens.add(str[i]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(tokens.size());
感谢宝贵的意见,但我相信我已经解决了我的问题:
String[] tokens;
try {
Scanner scanner = new Scanner("text.txt");
tokens = scanner.nextLine().split(",");
} catch (IOException e) {
e.printStackTrace();
}
我会 select 如果我得到一些赞成票来证明我的解决方案确实是(其中之一)正确的答案!