如何将txt文本里的数据转化为String数组

qq_31919421 2017-07-21 04:03:56
如图,将txt文本文本里的张三,李四,王二和麻子转换为如 String[] i = { "张三", "李四", "王二", "麻子"}; 这样的数组
...全文
1360 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_31919421 2017-09-06
  • 打赏
  • 举报
回复
已解决,谢谢各位。。。。
qq_31919421 2017-09-06
  • 打赏
  • 举报
回复
引用 10 楼 丶2p的回复:
解决了还不结帖、赶紧
怎么结帖啊。。。。。
窝 窝 2017-08-11
  • 打赏
  • 举报
回复
丶2p 2017-08-05
  • 打赏
  • 举报
回复
解决了还不结帖、赶紧
解开者 2017-08-03
  • 打赏
  • 举报
回复
引用 3 楼 soton_dolphin 的回复:

public static void main(String[] args) throws IOException {
		String[] names = (String[])Files.lines(Paths.get("path to file"))
				              .collect(Collectors.toList()).toArray();
	}
Stream本身就有toArray方法:
String[] array = Files.lines(Paths.get("D:/1.txt")).toArray(String[]::new);
z896709283 2017-08-03
  • 打赏
  • 举报
回复
用BufferedReader的readLine方法
nvli_bugufu 2017-08-03
  • 打赏
  • 举报
回复
引用 6 楼 qq_36746928 的回复:
[quote=引用 3 楼 soton_dolphin 的回复:]

public static void main(String[] args) throws IOException {
		String[] names = (String[])Files.lines(Paths.get("path to file"))
				              .collect(Collectors.toList()).toArray();
	}
我脑子里的库过时了,得更新更新了。。还在1.6的渣渣[/quote] 我怎么编译不通过
kb43446 2017-07-23
  • 打赏
  • 举报
回复
引用 3 楼 soton_dolphin 的回复:

public static void main(String[] args) throws IOException {
		String[] names = (String[])Files.lines(Paths.get("path to file"))
				              .collect(Collectors.toList()).toArray();
	}
我脑子里的库过时了,得更新更新了。。还在1.6的渣渣
qq_31919421 2017-07-21
  • 打赏
  • 举报
回复
2l的算法非常管用,谢谢
qq_31919421 2017-07-21
  • 打赏
  • 举报
回复
3l正解,谢谢各位大佬!
soton_dolphin 2017-07-21
  • 打赏
  • 举报
回复

public static void main(String[] args) throws IOException {
		String[] names = (String[])Files.lines(Paths.get("path to file"))
				              .collect(Collectors.toList()).toArray();
	}
110成成 2017-07-21
  • 打赏
  • 举报
回复
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);

	}
}
一个帅逼 2017-07-21
  • 打赏
  • 举报
回复
我记得有个逐行读取readline(),然后去空格,存入list,再转换数组。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧