为了账号安全,请及时绑定邮箱和手机立即绑定

Java Java中的空行

Java Java中的空行

SMILET 2021-05-06 10:10:20
 protected synchronized static void getRandomProxy(String srcFile) throws FileNotFoundException {        List<String> words = new ArrayList<>();         BufferedReader reader = null;        try {             reader = new BufferedReader(new FileReader(srcFile));            String line;            while ((line = reader.readLine()) != null) {                words.add(line);                System.out.println(line);            }            int k = 0;            for (int i = 0; i < words.size(); i++) {                k++;                String[] splitted = words.get(i).split(":");                String ip = splitted[0];                String port = splitted[splitted.length - 1];//                System.out.println(k + " " + ip + " * " + port);            }        } catch (IOException iOException) {        } finally {            try {                reader.close();            } catch (IOException ex) {               ex.printStackTrace();            }        }    }我想输出没有空行的输出。这些结果越来越像:结果1。结果2。结果3。我想要像这样的输出:结果1.结果2.结果3。没有空白行。
查看完整描述

3 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

如果字符串为空,则不要将其添加到列表中:


if(!line.trim().isEmpty()) {

    words.add(line);

    System.out.println(line);

}

如果您仍想将空白行添加到列表中但不显示它们,则只需移动条件:


words.add(line);

if(!line.trim().isEmpty())

    System.out.println(line);


查看完整回答
反对 回复 2021-05-26
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

ArrayList<String> words = new ArrayList<>();

BufferedReader reader = null;

try {

    reader = new BufferedReader(new FileReader(srcFile));

    String line;

    while ((line = reader.readLine()) != null) {

        line = line.trim(); // remove leading and trailing whitespace

        if (!line.isEmpty() && !line.equals("")) {


            words.add(line);

            System.out.println(line);

        }

    }


查看完整回答
反对 回复 2021-05-26
?
慕神8447489

TA贡献1780条经验 获得超1个赞

使用System.out.print。请注意,该文件在每一行的末尾包含一个换行符。

如果使用记事本创建了srcFile,请尝试首先删除回车符char System.out.print(line.replaceAll("\\r",""))


查看完整回答
反对 回复 2021-05-26
  • 3 回答
  • 0 关注
  • 224 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信