可以通过以下步骤实现:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
ArrayList<String> quotedList = new ArrayList<>();
try {
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
quotedList.add(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Quoted List: " + quotedList);
}
}
请将代码中的"input.txt"替换为你实际的txt文件路径。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
ArrayList<String> quotedList = new ArrayList<>();
try {
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
quotedList.add(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
List<String> unquotedList = quotedList.stream()
.map(s -> s.replaceAll("\"", ""))
.collect(Collectors.toList());
System.out.println("Unquoted List: " + unquotedList);
}
}
这段代码使用了Stream的map操作来对每个元素进行处理,使用replaceAll方法将双引号替换为空字符串。最后,使用collect操作将处理后的元素收集到一个新的List中。
以上就是使用Java8将从外部txt文件读取的带引号的ArrayList<String>转换为不带引号的ArrayList<String>的完整代码。
领取专属 10元无门槛券
手把手带您无忧上云