扩展OutputStream类;写(int)方法

发布于 2025-01-08 04:07:00 字数 863 浏览 0 评论 0原文

所以我的目标是实现OutputStream类中的write方法来创建一个新的NumStream类,它基本上将整数转换为字符串。这是我的示例代码:

import java.io.*; 
public class NumStream extends OutputStream {
    public void write(int c) throws IOException {
        // What goes here?
    }

    public static void main(String[] args) {
        NumStream ns = new NumStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(ns));
        pw.println("123456789 and ! and # ");
        pw.flush(); // needed for anything to happen, try taking it out
    }
}

我尝试使用几种不同的方法,我的结果总是导致程序编译,但是当我运行它时,什么也没有发生。到目前为止,我已经尝试使用 switch 语句来产生这个结果:

public void write(int c) throws IOException {
StringBuffer sb = new StringBuffer();
    switch (c) {
        case 1: sb.append("1");
        break;
    //etc. through 9

我不确定下一步要做什么或尝试产生结果。 :/有什么建议可以引导我走向正确的方向吗?

So my goal is to implement the write method in the class OutputStream to create a new class NumStream, which basically converts ints to Strings. Here is my sample code:

import java.io.*; 
public class NumStream extends OutputStream {
    public void write(int c) throws IOException {
        // What goes here?
    }

    public static void main(String[] args) {
        NumStream ns = new NumStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(ns));
        pw.println("123456789 and ! and # ");
        pw.flush(); // needed for anything to happen, try taking it out
    }
}

I've tried using several different approaches, and my result always results in the program compiling, but when I run it, nothing happens. So far I've tried using switch statements to produce this result:

public void write(int c) throws IOException {
StringBuffer sb = new StringBuffer();
    switch (c) {
        case 1: sb.append("1");
        break;
    //etc. through 9

I'm unsure of what to do or try next to produce a result. :/ Any tips to steer me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

○愚か者の日 2025-01-15 04:07:00

我也遇到了同样的问题,这是解决方案:

public class MyOutputStream extends OutputStream {

 StringBuilder anotatedText;

 public MyOutputStream() {
  // Custom constructor
 }

 @Override
 public void write(int b) {
     int[] bytes = {b};
     write(bytes, 0, bytes.length);
 }

 public void write(int[] bytes, int offset, int length) {
     String s = new String(bytes, offset, length);
     anotatedText.append(s);
 }

 public void myPrint() {
     System.out.println(anotatedText);
 }
}

我们需要做的就是正确实现“write”方法,这在上面的示例中已明确说明。

I had the same problem too, Here is the solution:

public class MyOutputStream extends OutputStream {

 StringBuilder anotatedText;

 public MyOutputStream() {
  // Custom constructor
 }

 @Override
 public void write(int b) {
     int[] bytes = {b};
     write(bytes, 0, bytes.length);
 }

 public void write(int[] bytes, int offset, int length) {
     String s = new String(bytes, offset, length);
     anotatedText.append(s);
 }

 public void myPrint() {
     System.out.println(anotatedText);
 }
}

All we need to do is to implement the "write" method correctly which is clearly instructed in the above example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文