从数据库中读取带换行的字符串

数据库中的workExp(工作经历)字段是带换行或回车的字符串。再取出拼json的时候,换行会破坏json格式。于是先将该字符串中的换行或是回车替换成“;”,将替换好的字符串拼入json字符串中,传入前台再将“;”换成换行符即可。主要代码如下:
后台:
//workexp 工作经历中包含换行,进行处理后放入json字符串中。
BufferedReader br = new BufferedReader(new StringReader(user.getWorkExp().trim()));
String sting ="";
try
{
String line = br.readLine();
while(line != null){
sting += line+';';
line = br.readLine();
}
br.close();
} catch (IOException e)
{
e.printStackTrace();
}
前台:
var workexp = node.attributes.workExp.replace(/;/g,"\n");
parent.document.getElementById("WorkExp").value=workexp;
以下Java代码实现读取文件的内容,并根据不同的标记进行不同的读取方式和处理: ```java import java.io.BufferedReader; import java.io.FileReader; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class ReadFile { public static void main(String[] args) { String filePath = "file.txt"; // 文件路径 String line; String introContent = ""; // [INTRO]标记后的内容 String policyContent = ""; // [优待政策]标记后的内容 String facilityContent = ""; // [服务设施]标记后的内容 String gradeContent = ""; // [GRADE]标记后的内容 boolean isIntro = false; // 是否已经读取到[INTRO] boolean isPolicy = false; // 是否已经读取到[优待政策] boolean isFacility = false; // 是否已经读取到[服务设施] boolean isGrade = false; // 是否已经读取到[GRADE] boolean isContent = false; // 是否正在读取内容 try (BufferedReader br = new BufferedReader(new FileReader(filePath))) { while ((line = br.readLine()) != null) { // 清除空格和换行 line = line.replaceAll("\\s", ""); if (line.equals("[INTRO]")) { isIntro = true; isContent = false; } else if (line.equals("优待政策")) { isPolicy = true; isContent = false; } else if (line.equals("服务设施")) { isFacility = true; isContent = false; } else if (line.equals("[GRADE]")) { isGrade = true; isContent = false; } else if (!line.isEmpty()) { // 如果不是空行 if (isIntro) { // [INTRO]后的内容 if (!isContent) { introContent += line; // 读取第一行 isContent = true; } else { introContent += line; // 读取第二行 isIntro = false; } } else if (isPolicy || isFacility || isGrade) { // 多行读取 if (!line.equals("服务设施") && !line.equals("[GRADE]")) { if (!isContent) { isContent = true; } else { policyContent += "\n"; facilityContent += "\n"; gradeContent += "\n"; } policyContent += line; facilityContent += line; gradeContent += line; } else { isPolicy = false; isFacility = false; isGrade = false; isContent = false; } } } } } catch (Exception e) { e.printStackTrace(); } // 将数据存入数据库 try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password"); PreparedStatement ps = conn.prepareStatement("INSERT INTO test_data (intro, policy, facility, grade) VALUES (?, ?, ?, ?)");) { ps.setString(1, introContent); ps.setString(2, policyContent); ps.setString(3, facilityContent); ps.setString(4, gradeContent); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 需要注意的是,这段代码数据库连接和表名等需要根据实际情况进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值