java之IO节点流示例,其他情况举一反三都是类似的

简介: java之IO节点流示例,其他情况举一反三都是类似的
-I/O流的一般操作流程:-创建或者选择数据源-选择I/O流-创建操作对象-创建读取or存储的缓存区-具体操作,写入,读取,转换等等-**出现乱码原因**:字节转字符,**字节数不够**,导致乱码;**编码不一致**导致乱码-注意事项-`FileInput/OutputStream`最适合用于操作非文本文件(如图片、视频等)-`FileReader/Writer`最适合用于操作文本文件(如txt文件)-`BufferStream`可以加速节点流操作文件的速度```*抽象基类节点流(文件流)缓冲流字节流*InputStreamFileInputStreamBufferedInputStream*OutputStreamFileOutputStreamBufferedOutputStream字符流*ReaderFileReaderBufferedReader*WriterFileWriterBufferedWriter```-示例:文件<----->字节流:`BufferedInputStream(newFileInputStream(src))`----->`ByteArrayOutputStream`;  `ByteArrayInputStream`---->`newBufferedOutputStream(newFileOutputStream(dest)`-示例:```java/**文件转字节流*/publicstaticbyte[] fileToByteArray(StringfilePath){
//create sourceFilefile=newFile(filePath);
//select streamInputStreamis=null;
ByteArrayOutputStreamos=null;
try {
//create objectis=newFileInputStream(file);
os=newByteArrayOutputStream();
//create catch placebyte[] flush=newbyte[1024*10];
//do somethingintlen=-1;
while ((len=is.read(flush))!=-1){
Stringstr=newString(flush, 0, len);
os.write(str.getBytes());
                }
os.flush();
returnos.toByteArray();
            } catch (FileNotFoundExceptione) {
e.printStackTrace();
            } catch (IOExceptione) {
e.printStackTrace();
            } finally {
//先开后关try {
if (null!=os){
is.close();
                    }
                } catch (IOExceptione) {
e.printStackTrace();
                }
try {
if (null!=is){
is.close();
                    }
                } catch (IOExceptione) {
e.printStackTrace();
                }
            }
returnnull;
        }
``````java/**字节流转文件*/publicstaticbooleanbyteArrayToFile(byte[] bytesArray, StringfilePath){
//create sourceFilefile=newFile(filePath);
//select streamByteArrayInputStreamis=null;
FileOutputStreamos=null;
try {
//create objectis=newByteArrayInputStream(bytesArray);
os=newFileOutputStream(file);
//create catch placebyte[] flush=newbyte[1024*10];
//do somethingintlen=-1;
while ((len=is.read(flush))!=-1){
os.write(flush, 0, len);
                }
os.flush();
returntrue;
            } catch (FileNotFoundExceptione) {
e.printStackTrace();
            } catch (IOExceptione) {
e.printStackTrace();
            } finally {
//先开后关try {
if (null!=os){
is.close();
                    }
                } catch (IOExceptione) {
e.printStackTrace();
                }
try {
if (null!=is){
is.close();
                    }
                } catch (IOExceptione) {
e.printStackTrace();
                }
            }
returnfalse;
        }
``````javapublicstaticbooleanbyteArrayToFile(byte[] bytesArray, StringfilePath){
//create sourceFilefile=newFile(filePath);
//select stream(高版本JDK,无需close,try会帮你执行close, 操作字节流使用BufferedI/O,大大提高性能)try (ByteArrayInputStreamis=newByteArrayInputStream(bytesArray); OutputStreamos=newBufferedOutputStream(newFileOutputStream(file))){
//create catch placebyte[] flush=newbyte[1024*10];
intlen=-1;
while ((len=is.read(flush))!=-1){
os.write(flush, 0, len);
            }
os.flush();
returntrue;
        } catch (FileNotFoundExceptione) {
e.printStackTrace();
        } catch (IOExceptione) {
e.printStackTrace();
        }
returnfalse;
    }
```-字节流,字符流转换;页面源代码读取```javaimportjava.io.*;
importjava.net.URL;
/*** @author yyds* 字符流转字节流**/publicclassConvertTest {
publicstaticvoidmain(String[] args) {
//操作system,,in和system.outtry (
// InputStreamReader isr = new InputStreamReader(System.in);//OutputStreamWriter osw = new OutputStreamWriter(System.out);BufferedReaderisr=newBufferedReader(newInputStreamReader(System.in));
BufferedWriterosw=newBufferedWriter(newOutputStreamWriter(System.out));
BufferedReaderis=newBufferedReader(newInputStreamReader(newURL("http://www.baidu.com").openStream(), "UTF-8"));
BufferedWriterbw=newBufferedWriter(newOutputStreamWriter(newFileOutputStream("baidu.html"), "UTF-8"));
             ) {
inttemp;
Stringtemp2;
Stringtemp1;
while ((temp=is.read())!=-1){
System.out.print((char) temp);
bw.write(temp);
                }
while ((temp1=is.readLine())!=null){
bw.write(temp1);
bw.newLine();
                }
bw.flush();
while (!"exit".equals(temp2=isr.readLine())){
osw.write(temp2);
osw.newLine();
osw.flush();
            } } catch (IOExceptione) {
System.out.println(e.toString());
        }
    }
}
```
相关文章
|
Java 开发工具
【Azure Storage Account】Java Code访问Storage Account File Share的上传和下载代码示例
本文介绍如何使用Java通过azure-storage-file-share SDK实现Azure文件共享的上传下载。包含依赖引入、客户端创建及完整示例代码,助你快速集成Azure File Share功能。
892 6
|
Java Unix Go
【Java】(8)Stream流、文件File相关操作,IO的含义与运用
Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。!但本节讲述最基本的和流与 I/O 相关的功能。我们将通过一个个例子来学习这些功能。
525 1
|
IDE Java 关系型数据库
Java 初学者学习路线(含代码示例)
本教程为Java初学者设计,涵盖基础语法、面向对象、集合、异常处理、文件操作、多线程、JDBC、Servlet及MyBatis等内容,每阶段配核心代码示例,强调动手实践,助你循序渐进掌握Java编程。
1574 3
|
Java
java入门代码示例
本文介绍Java入门基础,包含Hello World、变量类型、条件判断、循环及方法定义等核心语法示例,帮助初学者快速掌握Java编程基本结构与逻辑。
906 0
|
9天前
|
Java API 开发者
Java 21 特性及示例
本文介绍了 Java 21 的新特性,包括字符串模板、顺序集合、记录模式和 switch 模式匹配,通过示例展示其用法和优势,帮助开发者提升代码简洁性与可读性,是升级至 JDK 21 的实用指南。
525 0
|
Java 测试技术 API
Java IO流(二):文件操作与NIO入门
本文详解Java NIO与传统IO的区别与优势,涵盖Path、Files类、Channel、Buffer、Selector等核心概念,深入讲解文件操作、目录遍历、NIO实战及性能优化技巧,适合处理大文件与高并发场景,助力高效IO编程与面试准备。
|
SQL Java 数据库连接
Java IO流(一):字节流与字符流基础
本文全面解析Java IO流,涵盖字节流、字符流及其使用场景,帮助开发者理解IO流分类与用途,掌握文件读写、编码转换、异常处理等核心技术,通过实战案例提升IO编程能力。
|
存储 Java Linux
操作系统层面视角下 Java IO 的演进路径及核心技术变革解析
本文从操作系统层面深入解析Java IO的演进历程,涵盖BIO、NIO、多路复用器及Netty等核心技术。分析各阶段IO模型的原理、优缺点及系统调用机制,探讨Java如何通过底层优化提升并发性能与数据处理效率,全面呈现IO技术的变革路径与发展趋势。
365 3
|
监控 Java API
现代 Java IO 高性能实践从原理到落地的高效实现路径与实战指南
本文深入解析现代Java高性能IO实践,涵盖异步非阻塞IO、操作系统优化、大文件处理、响应式网络编程与数据库访问,结合Netty、Reactor等技术落地高并发应用,助力构建高效可扩展的IO系统。
569 0