说说 Java I/O 系统之标准 I/O

标准 I/O 在 Unix 中指的是程序所使用的单一信息流。程序的所有输入都来自于标准输入、所有输出都发送到标准输出,以及所有的错误都会发送到标准错误中。这样我们可以很容易把程序串起来,一个程序的标准输出是另一个程序的标准输入,是不是很强大呀O(∩_∩)O~

1 从标准输入中读取数据

Java 的标准 I/O 模型提供了 System.in、System.out 和 System.err。System.out 和 System.err 都已被包装成了 PrintStream 对象,但 System.in 却没有,所以我们可以直接使用 System.out 和 System.err,而在读取 System.in 之前必须先对其进行包装。

public class Echo {

    public static void main(String[] args) throws IOException {
        BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = stdin.readLine()) != null && s.length() != 0)
            System.out.println(s);
        // An empty line or Ctrl-Z teminates the program
    }
}

注意,readLine() 会抛出 IOException。还有一点,读取 System.in 时要加入缓冲能力。

2 把 System.out 转换为 PrintWriter

public class ChangeSystemOut {
    public static void main(String[] args) {
        PrintWriter out = new PrintWriter(System.out, true);
        out.println("Hello,world");
    }
}

这里使用了带两个参数的 PrintWriter 构造器。第二个参数为 true,表示开启了自动清空功能,这样才能在控制台看到实际的输出!

3 标准 I/O 的重定向

System 类提供了一些静态方法,我们可以利用这些方法对标准输入、标准输出以及错误 I/O 流进行重定向:

  • setIn(InputStream)
  • setOut(PrintStream)
  • setErr(PrintStream)

有时候会一次性输出大量的信息,而这些输出滚动的太快以至于无法阅读时,重定向输出就很有用:

public class Redirecting {
    public static void main(String[] args) throws IOException {
        PrintStream console = System.out;
        BufferedInputStream in = new BufferedInputStream(new FileInputStream
                ("D:\\temp\\1\\1.txt"));
        PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream
                ("D:\\temp\\1\\5.txt")));
        System.setIn(in);
        System.setOut(out);
        System.setErr(out);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null)
            System.out.println(s);
        out.close();// Remember this!
        System.setOut(console);
    }
}

这里,我们将标准输入接入文件,然后将标准输出和标准错误重定向到另一个文件。System.out 最开始先保存在参数中,最后再恢复。这样只有中间这一段的 System.out 被做了重定向处理。

还有一点,I/O 重定向操作的是字节流,所以我们这里使用了 FileInputStream 和 FileOutputStream。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Java I/O操作读写本地图片,可以使用以下步骤: 1. 使用FileInputStream或FileOutputStream类来读写文件。 2. 创建一个File对象,指定要读写的文件路径。 3. 创建一个InputStream或OutputStream对象,将File对象传递给它们。 4. 使用read()或write()方法来读写文件内容。 5. 关闭输入输出流和文件对象。 以下是一个简单的示例代码,可以读取本地图片并将其写入到另一个文件中: ```java import java.io.*; public class ImageIOExample { public static void main(String[] args) { try { // 创建一个File对象,指定要读取的图片路径 File inputFile = new File("input.jpg"); // 创建一个File对象,指定要写入的图片路径 File outputFile = new File("output.jpg"); // 创建一个输入流对象,读取inputFile中的内容 FileInputStream inputStream = new FileInputStream(inputFile); // 创建一个输出流对象,将内容写入outputFile中 FileOutputStream outputStream = new FileOutputStream(outputFile); // 定义一个字节数组,用于存储文件内容 byte[] buffer = new byte[1024]; // 定义一个变量,记录读取到的字节数 int bytesRead = 0; // 读取文件内容到字节数组中,直到读取完毕 while ((bytesRead = inputStream.read(buffer)) != -1) { // 将字节数组中的内容写入到输出流中 outputStream.write(buffer, 0, bytesRead); } // 关闭输入输出流 inputStream.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 以上代码将会读取名为`input.jpg`的图片,并将其写入到名为`output.jpg`的文件中。需要注意的是,如果要读写其他类型的文件,只需要将文件名和文件类型(例如jpg)替换即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值