Java解析字符串再以指定的格式写出txt

起床搬砖 2018-12-05 03:18:55
如下图所示,我List集合里装着这样一条条键值对的数据,里面的每个数据的是以 ", "(逗号+空格) 分隔开,需要注意的是有的数据中的值会有空格比如a=1 3。有些数据为空时会像a=,这样,或者像b=null这样,我要怎么处理才能达到以我想要的格式输出txt呢?如下面所示的每个数据以\t分隔开,并且空的和null的都直接显示为空,大神帮忙看看,最好能有代码示范,谢谢了
...全文
583 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
起床搬砖 2018-12-07
  • 打赏
  • 举报
回复
多谢各位的帮助
dyq6228 2018-12-06
  • 打赏
  • 举报
回复
有java好的书推荐么
起床搬砖 2018-12-06
  • 打赏
  • 举报
回复
引用 7 楼 咸哼酒家 的回复:
头像,你也在java区
是啊,还是要学习
起床搬砖 2018-12-06
  • 打赏
  • 举报
回复
引用 3 楼 wp500 的回复:
引用 4 楼 楓VS痕 的回复:
引用 5 楼 qq_39936465 的回复:
引用 8 楼 qq_39936465 的回复:
谢谢各位,我先一一试试
qq_39936465 2018-12-06
  • 打赏
  • 举报
回复
这个都是差不多了,只是把输出改为文件而已

import java.util.ArrayList;
import java.util.List;
import java.io.FileWriter;
import java.io.BufferedWriter;

public class Test1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
try {
FileWriter fw = new FileWriter("d:\\1.txt");
BufferedWriter bfw = new BufferedWriter(fw);
String[] str;
list.add("a=1 3, b=2, c=3");
list.add("a=, b=null, c=6");
bfw.write("a\tb\tc\r\n");
for (String i : list) {
i = i.replace(" ", "");
i = i.replace("null", "");
str = i.split(",");
for (int j = 0; j < str.length; j++) {
if (str[j].length() > 3) {
str[j] = str[j].substring(0, 3);
System.out.println(str[j] + "end");
}
}
bfw.write(str[0].substring(2) + "\t" + str[1].substring(2) + "\t" + str[2].substring(2)+"\r\n");
}
bfw.close();
fw.close();
} catch (Exception e) {
}

}
}
叶遮沉阳 2018-12-06
  • 打赏
  • 举报
回复
晕,自己弄个输出流。把控制台的输出改成向文本写入,不就行了吗。
起床搬砖 2018-12-06
  • 打赏
  • 举报
回复
引用 4 楼 楓VS痕 的回复:
引用 5 楼 qq_39936465 的回复:
大神们...我指的输出是指以上面的格式写出到txt,不是控制台输出,可以再帮我看看吗
qq_39936465 2018-12-05
  • 打赏
  • 举报
回复
如果需要限定取值位数再分隔语句后加入截取字符串语句

import java.util.ArrayList;
import java.util.List;

public class test17 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<String>();
		String[] str;
		list.add("a=1 3, b=2, c=3");
		list.add("a=, b=null, c=6");
		System.out.println("a\tb\tc");
		for (String i : list) {
			i = i.replace(" ", "");
			i = i.replace("null", "");
			str = i.split(",");
			for(int j=0;j<str.length;j++) {
				if(str[j].length()>3) {
					str[j]=str[j].substring(0,3);
					System.out.println(str[j]+"end");
				}
			}
			System.out.println(str[0].substring(2) + "\t" + str[1].substring(2) + "\t" + str[2].substring(2));
		}

	}

}
substring(start,end) 注意:end要比真实的位置+1,省略为到字符串的末尾。
咸哼酒家 2018-12-05
  • 打赏
  • 举报
回复
头像,你也在java区
qq_39936465 2018-12-05
  • 打赏
  • 举报
回复
qq_39936465 2018-12-05
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.List;

public class test17 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<String>();
		String[] str;
		list.add("a=1 3, b=2, c=3");
		list.add("a=, b=null, c=6");
		System.out.println("a\tb\tc");
		for (String i : list) {
			i = i.replace(" ", "");
			i = i.replace("null", "");
			str = i.split(",");
			System.out.println(str[0].substring(2) + "\t" + str[1].substring(2) + "\t" + str[2].substring(2));
		}

	}

}


a	b	c
13	2	3
		6

楓VS痕 2018-12-05
  • 打赏
  • 举报
回复


package com.test.t2;

import java.util.ArrayList;
import java.util.List;

public class test {

public static void main(String[] args) {

List<String> list = new ArrayList<String>();
list.add("a=1 3, b=2, c=3");
list.add("a=, b=null, c=6");

String str[] = list.get(0).split(", ");
for (int k = 0; k < str.length; k++) {
String str2[] = str[k].split("=");
System.out.print(str2[0] + "---");
}
System.out.println();

for (int i = 0; i < list.size(); i++) {

String str3[] = list.get(i).split(", ");
for (int j = 0; j < str3.length; j++) {

String str4[] = str3[j].split("=");
if (str4.length > 1 && str4[1].equals("null"))
System.out.print(" " + "---");
else if (str4.length > 1)
System.out.print(str4[1].split(" ")[0] + "---");
else
System.out.print(" " + "---");
}
System.out.println();
}
}

}

wp500 2018-12-05
  • 打赏
  • 举报
回复
sorry,没注意你问题中下面的图 问题两个关键点一个是解析,第二个是结果存储和输出 解析使用字符串split先用,号分割之后再使用split方法用=号分割 结果保存在如下数据结构 key应该是a、b 、c value对应数组,遍历的时候先遍历key之后每行输出value数据中对应的数据,如果超过数据长度或者null就显示""

Map<String,List<String>> rst=new HashMap<String,List<String>>
起床搬砖 2018-12-05
  • 打赏
  • 举报
回复
引用 1 楼 wp500 的回复:
"null" 替换成 "" "," 替换成 ",\t"
多谢指导,这个是其中一部分。 但是我觉得难点主要是在写出txt的时候第一行输出键,后面每行输出对应的值
wp500 2018-12-05
  • 打赏
  • 举报
回复
"null" 替换成 "" "," 替换成 ",\t"

62,614

社区成员

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

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