【问题标题】:System.out.printf(“%4d”) in BufferedWriter / FileWriter [duplicate]BufferedWriter / FileWriter 中的 System.out.printf(“%4d”) [重复]
【发布时间】:2020-04-15 11:00:39
【问题描述】:

我做了一个乘法表。问题是表格没有按应有的顺序排列。

如果我只想将它打印在屏幕上,那么我使用这个System.out.printf(“%4d”)。如何使用BufferedWriter 解决此问题?

而不是这个:

Irj be egy szamot:
5
1 2 3 4 5 
2 4 6 8 10 
3 6 9 12 15 
4 8 12 16 20 
5 10 15 20 25 `

我想要这个:

Irj be egy szamot: 
5
1  2  3  4  5 
2  4  6  8 10 
3  6  9 12 15 
4  8 12 16 20 
5 10 15 20 25 `

这是我的代码:

public class EgyszerEgy {
    public static void main(String[] args) {

        int a;
        int b;

        try {
            FileWriter writer = new FileWriter("EgyszerEgy.txt");
            BufferedWriter bf = new BufferedWriter(writer);

            Scanner tastatur = new Scanner(System.in);
            System.out.println("Irj be egy szamot: ");
            int szam = tastatur.nextInt();

            for (a = 1; a <= szam; ++a) {
                for (b = 1; b <= szam; ++b) {
                    int eredmeny = a * b;
                    String eredmenyString = String.valueOf(eredmeny);
                    bf.write(eredmenyString);
                    bf.write(" ");
                }
                bf.newLine();
            }
            bf.flush();
            bf.close();
        } catch (Exception e) {

        }

        // Kiolvasas
        //String result;
        try {
            FileReader fr = new FileReader("EgyszerEgy.txt");
            BufferedReader br = new BufferedReader(fr);
            String result;
            while ((result = br.readLine()) != null) {
                System.out.println(result);
            }
            br.close();
        } catch (Exception e) {

        }
    }
}

【问题讨论】:

  • System.out.println(result); 替换为System.out.printf("%4d", result);
  • @MaartenBodewes 所以马文比马文做得更好(同一个人)? ??????
  • 我认为我发现错误的速度比你快,为时已晚再次:P
  • 请注意,尽管 Marvin 的两个建议都有很好的答案,但问题本身非常笨拙且格式错误。 两者都是关于流而不是作家。

标签: java filewriter bufferedwriter


【解决方案1】:

您已经知道如何用BufferedWriter 包装FileWriter。现在用PrintWriter 再次包装它,它具有printf() 方法。

您还应该使用 try-with-resources。它是在 Java 7 中添加的,所以绝对没有理由不使用它,除非你被 Java 6 或更早版本卡住了。

使用 NIO.2 API 代替旧的 File I/O API 也是如此。

Scanner tastatur = new Scanner(System.in);
System.out.println("Irj be egy szamot: ");
int szam = tastatur.nextInt();

try (PrintWriter fout = new PrintWriter(Files.newBufferedWriter(Paths.get("EgyszerEgy.txt")))) {
    for (int a = 1; a <= szam; ++a) {
        for (int b = 1; b <= szam; ++b) {
            int eredmeny = a * b;
            fout.printf("%3d ", eredmenyString);
        }
        fout.println();
    }
}

【讨论】:

    【解决方案2】:

    您可以创建与printf 完全相同的格式,方法是使用String.format 并将结果写入BufferedWriter

    【讨论】:

    • 为什么需要明确使用String.format?为什么不直接使用PrintStream
    • 获得问题要求的间距而不做任何不同的事情。毕竟它想要"%4d"
    • 使用String.format 使用PrintWriter.printf。两者都做是多余的!
    猜你喜欢
    • 2013-07-27
    • 2019-06-08
    • 2016-07-29
    • 1970-01-01
    • 2012-09-03
    • 2018-04-16
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多