4.5.3.3PrintStream类

本文介绍了Java中PrintStream和PrintWriter类的基本概念及其使用方法。重点讲解了PrintStream类提供的多种构造方法和如何格式化输出数据,同时对比了PrintWriter类在文本换行处理上的不同之处。

h4 { margin-top: 0.49cm; margin-bottom: 0.51cm; line-height: 156%; page-break-inside: avoid; }h4.western { font-family: "Arial",sans-serif; font-size: 14pt; }h4.cjk { font-family: "黑体","SimHei"; font-size: 14pt; }h4.ctl { font-family: "DejaVu Sans"; font-size: 14pt; }p { margin-bottom: 0.21cm; }a:link { color: rgb(0, 0, 255); }


PrintStream 类提供了一系列的printprintln 方法,可以将基本数据类型的数据格式化成字符串输出 System.out 其实就是PrintStream 的一个指定类

格式化输出是什么意思? 将一个数据按照字符串的格式输出 如将97 输入到文件中 就是把97asci 码输入到文件之中

 

PrintStream 的构造方法

PrintStream ( File  file)
          
创建具有指定文件且不带自动行刷新的新打印流。

PrintStream ( File  file, String  csn)
          
创建具有指定文件名称和字符集且不带自动行刷新的新打印流。

PrintStream ( OutputStream  out)
          
创建新的打印流。

PrintStream ( OutputStream  out, boolean autoFlush)
          
创建新的打印流。

PrintStream ( OutputStream  out, boolean autoFlush, String  encoding)
          
创建新的打印流。

PrintStream ( String  fileName)
          
创建具有指定文件名称且不带自动行刷新的新打印流。

PrintStream ( String  fileName, String  csn)
          
创建具有指定文件名称和字符集且不带自动行刷新的新打印流。

 

 

PrintStream 对应的PrintWriter 类 即使遇到了文本换行表示符’/n’printWriter 类也不会自动清空缓冲区

PrintWriterprintln 方法能根据操作系统的不同生产相对应的文本换行符表示,在windows/r/n linux 下是/n

 

### Java中解决MissingFormatArgumentException异常的方案 在Java中,`java.util.MissingFormatArgumentException` 异常通常发生在使用 `String.format()` 或 `PrintStream.printf()` 方法时,格式化字符串中的占位符数量与传递的实际参数数量不匹配。例如,如果格式化字符串中包含 `%s` 占位符,但未提供对应的参数,则会抛出该异常[^2]。 为了解决这个问题,可以采取以下方法: #### 1. 检查并确保参数数量匹配 在调用 `String.format()` 或 `PrintStream.printf()` 之前,必须仔细检查格式化字符串中的占位符数量是否与实际传递的参数数量一致。例如,对于以下代码: ```java System.out.printf("Hello, %s", "World"); ``` 如果格式化字符串中有一个 `%s` 占位符,则需要提供一个对应的参数 `"World"`。如果缺少参数,将抛出 `MissingFormatArgumentException` 异常[^2]。 #### 2. 使用动态参数验证 在复杂场景下,可以通过编程方式验证参数数量是否匹配。例如: ```java public static void safePrintf(String format, Object... args) { int placeholderCount = countPlaceholders(format); if (placeholderCount != args.length) { throw new IllegalArgumentException("Placeholder count does not match argument count"); } System.out.printf(format, args); } private static int countPlaceholders(String format) { int count = 0; int index = 0; while ((index = format.indexOf("%", index)) != -1) { // 跳过转义字符 %% 的情况 if (index + 1 < format.length() && format.charAt(index + 1) == &#39;%&#39;) { index += 2; continue; } count++; index += 2; // 跳过 % 和后续字符 } return count; } ``` 上述代码通过 `countPlaceholders` 方法计算格式化字符串中的占位符数量,并将其与传递的参数数量进行比较。如果不匹配,则提前抛出 `IllegalArgumentException`,从而避免运行时异常[^2]。 #### 3. 使用默认值填充缺失参数 如果某些情况下无法保证参数数量完全匹配,可以考虑为缺失的参数提供默认值。例如: ```java public static String fillMissingArgs(String format, Object... args) { int placeholderCount = countPlaceholders(format); Object[] filledArgs = new Object[Math.max(placeholderCount, args.length)]; System.arraycopy(args, 0, filledArgs, 0, args.length); Arrays.fill(filledArgs, args.length, filledArgs.length, "(missing)"); return String.format(format, filledArgs); } ``` 上述代码会在参数数量不足时,用 `"(missing)"` 填充缺失的参数,从而避免抛出异常。 #### 4. 日志记录与调试 在生产环境中,建议对格式化字符串和参数进行日志记录。例如: ```java try { System.out.printf("User: %s, Age: %d", "Alice"); } catch (MissingFormatArgumentException e) { System.err.println("Error in formatting: " + e.getMessage()); e.printStackTrace(); } ``` 通过捕获异常并记录错误信息,可以帮助快速定位问题所在。 ### 示例代码 以下是一个完整的示例,展示如何避免 `MissingFormatArgumentException`: ```java public class FormatExample { public static void main(String[] args) { try { // 正确的参数数量 System.out.printf("Hello, %s%n", "World"); // 缺少参数的情况 safePrintf("User: %s, Age: %d", "Alice"); // 自动检测参数数量不匹配 } catch (IllegalArgumentException e) { System.err.println("Parameter mismatch detected: " + e.getMessage()); } } public static void safePrintf(String format, Object... args) { int placeholderCount = countPlaceholders(format); if (placeholderCount != args.length) { throw new IllegalArgumentException("Placeholder count does not match argument count"); } System.out.printf(format, args); } private static int countPlaceholders(String format) { int count = 0; int index = 0; while ((index = format.indexOf("%", index)) != -1) { if (index + 1 < format.length() && format.charAt(index + 1) == &#39;%&#39;) { index += 2; continue; } count++; index += 2; } return count; } } ``` ### 注意事项 - 确保格式化字符串中的占位符数量与实际参数数量一致。 - 在复杂场景下,可以通过编程方式验证参数数量。 - 提供默认值或日志记录以增强程序的健壮性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值