如何将txt文本里的数据转化为String数组 如图,将txt文本文本里的张三,李四,王二和麻子转换为如String[] i = { "张三", "李四", "王二", "麻子"};这样的数组 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我记得有个逐行读取readline(),然后去空格,存入list,再转换数组。 public class Test3 { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new FileReader( "C:\\Users\\Administrator\\Desktop\\1.txt")); String line = null; List<String> list = new ArrayList<String>(); while ((line = in.readLine()) != null) { String temp = line.trim(); if (temp != null && !"".equals(temp)) list.add(temp); } String[] arr = (String[]) list.toArray(new String[list.size()]); for (String s : arr) System.out.println("===" + s); }} public static void main(String[] args) throws IOException { String[] names = (String[])Files.lines(Paths.get("path to file")) .collect(Collectors.toList()).toArray(); } 用BufferedReader的readLine方法 Stream本身就有toArray方法:String[] array = Files.lines(Paths.get("D:/1.txt")).toArray(String[]::new); 设置JPanel为透明 中文乱码. Integer类的方法~!新手求救~!! 难道写JAVA+SQL SERVER存储过程时不能用一个变量吗, 各位JAVA高手帮忙,学校的assignment,一点头绪也没有 四字节的数组转化为float的问题 新建的Frame能关闭?super(str)起什么作用? 怎样修改JBuilder的系统路径? 关于j2sdk的配置问题 求解!!! java该怎么自学啊? java中为什么反射不能直接获取到被继承父类的方法
BufferedReader in = new BufferedReader(new FileReader(
"C:\\Users\\Administrator\\Desktop\\1.txt"));
String line = null;
List<String> list = new ArrayList<String>();
while ((line = in.readLine()) != null) {
String temp = line.trim();
if (temp != null && !"".equals(temp))
list.add(temp); } String[] arr = (String[]) list.toArray(new String[list.size()]);
for (String s : arr)
System.out.println("===" + s); }
}
public static void main(String[] args) throws IOException {
String[] names = (String[])Files.lines(Paths.get("path to file"))
.collect(Collectors.toList()).toArray();
}
String[] array = Files.lines(Paths.get("D:/1.txt")).toArray(String[]::new);