JavaIO的简单代码实例和展示

简介: JavaIO的简单代码实例和展示

IO流实现文件的拷贝 几种不同的方法:

复制代码
package com.wxisme.TestIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**

  • 字节流拷贝文本文件
  • @author wxisme
    /
    public class StreamOne {
    public static void main(String[] args) {

     String path = "E:" + File.separator + "test.txt";
     File file = new File(path);
     try {
         file.createNewFile();
     } catch (IOException e) {
         System.out.println("创建文件失败");
         e.printStackTrace();
     }
     InputStream is = null;
         is = new FileInputStream(file);
     } catch (FileNotFoundException e1) {
         e1.printStackTrace();
     String fileName = "E:" + File.separator + "Bullet.java";
     OutputStream os = null;
         os = new FileOutputStream(fileName,true);
     byte[] b = new byte[10];
     int len = 0;
         while((len = is.read(b)) != -1) {
             os.write(b, 0, len);
         }
         os.flush();//强制刷出缓冲区
    
     }finally {
         try {
             os.close();
         } catch (IOException e) {
             e.printStackTrace();
             is.close();
    
     System.exit(0);
    

    }
    }
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.Reader;
    import java.io.Writer;

  • 字节流拷贝文件
    public class StreamTwo {
     String name = "E:" + File.separator + "Bullet.java";
     Reader r = null;
         r = new FileReader(name);
     } catch (FileNotFoundException e) {
         System.out.println("创建字符输入流失败");
     Writer w = null;
         w = new FileWriter(path);
         System.out.println("创建字符输出流失败");
     char[] cbuf = new char[10];
         while((len = r.read(cbuf)) != -1) {
             //w.write(cbuf);
             w.write(cbuf, 0, len);
         w.flush();//强制刷出
             w.close();
             r.close();
    
    import java.io.PrintStream;
    import java.util.Scanner;
  • 重定向输入输出 实现文件的拷贝
    public class Reset {
    public static void main(String[] args) throws FileNotFoundException {
     String src = "E:" + File.separator + "Bullet.java";
     FileInputStream fis = new FileInputStream(src);
     System.setIn(fis);
     PrintStream ps = new PrintStream(new FileOutputStream(path));
     System.setOut(ps);
     Scanner scan = new Scanner(System.in);
     scan.useDelimiter("\n");//以换行符为分隔符
     while(scan.hasNext()) {
         System.out.println(scan.next());
    
    //代码效果参考:http://www.zidongmutanji.com/zsjx/443913.html

处理流PrintStrream PrintWriter
import java.io.PrintWriter;

  • 使用处理流printStream printWriter
    public class StreamFour {
    public static void main(String[] args) throws IOException {
     PrintStream ps = new PrintStream(new FileOutputStream(path,true));
     ps.print("PrintStream");
     PrintWriter pw = new PrintWriter(new FileWriter(path));
     pw.print("PrintWriter");
     pw.close();
     ps.close();
    
    读取单个字符
    import java.io.StringReader;
  • 读取单个字符
    public class ReaderOne {
     String str = scan.next();
     Reader r = new StringReader(str);
     while(r.read(cbuf,0,1) != -1) {
         char c = cbuf[0];
         System.out.println(c);
     r.close();
    
    转换流
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
  • 使用转换流
    public class StreamThree {
     /*
      * 输入流底层使用字节流,然后使用转换流把字节流转换成字符流,并且指定解码字符集。
      * 然后把字符流包装成缓冲流,按行读取文件。
      * 乱码问题的两个主要原因:
      *    1. 解码字符集与编码字符集不统一
      *    2. 读取字节缺少,长度丢失。
      */
     String path = "E:" + File.separator + "Bullet.java";
     BufferedReader br = new BufferedReader(
             new InputStreamReader(
                     new FileInputStream(
                             new File(path)),"utf-8"));
     String str;
     while((str = br.readLine()) != null) {
         System.out.println(str);
     br.close();
    
    //代码效果参考:http://www.zidongmutanji.com/bxxx/125984.html

关闭文件的工具方法的两种写法:
import java.io.Closeable;

  • 关闭文件资源的工具类 有两种实现方法 可以实现一次关闭多个文件,先打开的文件后关闭
    public class CloseAll {

    /**

    • 使用多态 可变参数 可以有多个形参以数组的形式 可变形参必须在所有参数的最后
    • @param io
      */
      public static void closeAll1(Closeable ... io) {
      for(Closeable temp : io) {
       if(temp != null) {
           try {
               temp.close();
           } catch (IOException e) {
               System.out.println("文件关闭失败");
               e.printStackTrace();
           }
      
    • 泛型方法 使用泛型方法和可变参数
      public static void closeAll2(Closeable ... io) {
相关文章
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17712 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
3天前
|
人工智能 安全 网络安全
|
7天前
|
云安全 数据采集 人工智能
|
8天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1399 5
|
14天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2446 8
|
19天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3970 22
|
21天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。

热门文章

最新文章