我从文本文件中获取这个字符串并将其存储到一个变量中File file = new File("output.txt");FileInputStream fis = new FileInputStream(file);byte[] data = new byte[(int) file.length()];fis.read(data);fis.close();String str = new String(data, "UTF-8");str 像这样返回帖子:| SITENAME | TOTAL XML GENERATED | TOTAL URL ADDED IN XML ||:-----------------------------|:-----------:|:-----------------------------||https://www.google.com|71|2800884||https://www.google.com|71|2800884|但是当我将str它发送到 api 时,它返回bad gateway.但是当我使用以下格式的字符串发送请求时,它被接受并将其发布到信使。String str = ("| SITENAME \t\t\t | TOTAL XML GENERATED | \tTOTAL URL ADDED IN XML |\n" + "|:-----------------------------|:-----------:|:-----------------------------|\n" + "|https://www.google.com|71|2800884|\n" + "|https://www.google.com|71|2800884|\n" +任何人都可以建议我如何转换字符串?
3 回答
MMMHUHU
TA贡献1834条经验 获得超8个赞
起初,我以为你会在 Windows 上工作,但 Ubuntu ( \n
vs \r\n
)。后来,我认为文本上下文可能包含一个奇怪的 Unicode 字符,这可能会误导代码,但很干净。最终,后者是由键盘编写的字符串"\n"
,每行都有字符结尾。据推测,传递方法也预期文本的字符结尾。
String str = new String(data, "UTF-8") + "\n";
收到一只叮咚
TA贡献1821条经验 获得超4个赞
在我的字符串中,我应该保持换行符和 api 不支持\r\n
以下方式我刚刚解决了我的问题
str.replaceAll("\r\n",System.getProperty("line.separator"));
富国沪深
TA贡献1790条经验 获得超9个赞
试试这个:
BufferedReader br = new BufferedReader(new FileReader(filename));
StringBuffer sb = new StringBuffer();
while((line = bufferedReader.readLine()) != null)
{
sb.append(line); //append the lines to the string
sb.append('\n'); //append new line
}
//Required string
String newString = sb.toString();
return newString;
添加回答
举报
0/150
提交
取消