Java文件流之字节流

InputStream 与OutputStream是字节流中的输入流和输出流。

public class FileStream {

    public static void main(String[] args) {
        FileStream fs = new FileStream();
        // String content =
        // fs.readFileWithInputStream("/home/rding/file/File1.txt");
        // System.out.println(content);

        fs.writeFileWithOutputStream("/home/rding/file/File3.txt", "Hello File3");

        // fs.copyFile("/home/rding/file/File3.txt",
        // "/home/rding/file/File4.txt");

    }

    public void copyFile(String srcPath, String descPath) {
        InputStream inStream = null;
        OutputStream outStream = null;
        try {
            inStream = new FileInputStream(srcPath);
            outStream = new FileOutputStream(descPath);
            int size = inStream.available();
            byte[] byteArr = new byte[size];
            inStream.read(byteArr);
            outStream.write(byteArr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void writeFileWithOutputStream(String filePath, String content) {
        OutputStream outStream = null;
        byte[] byteArr = content.getBytes();
        try {        //注意,outStream第一次建立后,就不会更新了,必须关闭再重新建立
            outStream = new FileOutputStream(filePath);
            // outStream = new FileOutputStream(new File(filePath));
       //  如果传入参数true,则接着原来文件后面继续写,而不是覆盖源文件。
            // outStream = new FileOutputStream(filePath, true);
            // outStream = new FileOutputStream(new File(filePath), true);

            outStream.write(byteArr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public String readFileWithInputStream(String filePath) {
        InputStream inStream = null;
        String result = "";
        try {
            inStream = new FileInputStream(filePath);
            // inStream = new FileInputStream(new File(filePath));
            int size = inStream.available();
            byte[] byteArr = new byte[size];
            inStream.read(byteArr);
            result = new String(byteArr);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

}
时间: 2024-08-11 01:35:58

Java文件流之字节流的相关文章

java io流(字节流)复制文件

java io流(字节流) 复制文件 //复制文件 //使用字节流 //复制文本文件用字符流,复制其它格式文件用字节流 import java.io.*; public class Index{ public static void main(String[] args) throws Exception{ //字符流方式 //FileReader fz = new FileReader("E:/1.txt"); //FileWriter zt = new FileWriter(&qu

文件流和字节流

所有的IO操作都由以下步骤构成:1.建立流.根据数据源和具体的操作选择流,然后建立流.通过流的建立,创建内存到数据源之间的数据通道,以传输数据.2.操作流.将数据读取到内存,或将内存中的数据写入数据源.3.关闭流.流操作结束后,释放所有与该流相关的系统资源.   文件类.java.io包中的File类提供了管理磁盘文件和目录的基本功能.因为File对象类似于一个子妇产,值代表一个文件或者目录的路径名,所以即使指定的文件或目录不存在,这些构造器也能成功执行.如: File f = new File

java的文件流:字节流(FileInputStream、FileOutputStream)和字符流(FileReader、FileWriter)。

java的输入输出建立在4个抽象类的基础上:InputStream.OutputStream.Reader.Writer.InputSream和OutputStream被设计成字节流类,而Reader和Writer被设计成字符流类.一般来说,处理字符或者字符串时应该使用字符流类,处理字节或者二进制对象时应该使用字节流类. 一般在操作文件流时,不管是字节流还是字符流,都可以按照以下的方式进行. 1.使用File类找到一个文件 2.通过File类实例化字节流或字符流 3.进行字节(字符)的读写操作

Java字符流和字节流对文件操作

记得当初自己刚开始学习Java的时候,对Java的IO流这一块特别不明白,所以写了这篇随笔希望能对刚开始学习Java的人有所帮助,也方便以后自己查询.Java的IO流分为字符流(Reader,Writer)和字节流(InputStream,OutputStream),字节流顾名思义字节流就是将文件的内容读取到字节数组,然后再输出到另一个文件中.而字符流操作的最小单位则是字符.可以先看一下IO流的概述: 下面首先是通过字符流对文件进行读取和写入: 1 package lib; 2 3 import

JAVA 字符流与字节流的区别

Java 流在处理上分为字符流和字节流.字符流处理的单元为 2 个字节的 Unicode 字符,分别操作字符.字符数组或字符串,而字节流处理单元为 1 个字节,操作字节和字节数组. Java 内用 Unicode 编码存储字符,字符流处理类负责将外部的其他编码的字符流和 java 内 Unicode 字符流之间的转换.而类 InputStreamReader 和 OutputStreamWriter 处理字符流和字节流的转换.字符流(一次可以处理一个缓冲区)一次操作比字节流(一次一个字节)效率高

【Java IO流】字节流和字符流详解

字节流和字符流 对于文件必然有读和写的操作,读和写就对应了输入和输出流,流又分成字节和字符流. 1.从对文件的操作来讲,有读和写的操作——也就是输入和输出. 2.从流的流向来讲,有输入和输出之分. 3.从流的内容来讲,有字节和字符之分. 这篇文章先后讲解IO流中的字节流和字符流的输入和输出操作. 一.字节流 1)输入和输出流 首先,字节流要进行读和写,也就是输入和输出,所以它有两个抽象的父类InputStream.OutputStream. InputStream抽象了应用程序读取数据的方式,即

第二十四节(Java文件流,缓冲流)

Java流 文件通常是由一连串的字节或字符构成,组成文件的字节序列称为字节流,组成文件的字符序列称为字符流. Java 中根据流的方向可 以分为输入流和输出流. 输入流是将文件或其它输入设备的数据加载到内存的过程: 输出流恰恰相反, 是将内存中的数据保存到文件或其他输出设备. 见图: 文件是由字符或字节构成, 那么将文件加载到内存或再将文件输出到文件, 需要有输入和输出流的支持,那么在 Java 语言中又把输入和输出流分为了两个,字节输入和输出流,字符输入和输出流 InputStream 是字节

java文件流之copy文件(用一次读取一个字节数组方式)

package fileoutputstream; import java.io.FileInputStream; import java.io.FileOutputStream; public class CopyFileDemo { public static void main(String[] args) throws Exception { //封装数据源 FileInputStream fis = new FileInputStream("fos.txt");//fos.t

java文件流

File file = new File("E:/a.txt"); if(file.isFile()&&file.exists()){ FileInputStream fis = new FileInputStream(file);//变成文件输出流 InputStreamReader reader = new InputStreamReader(fis);//一种管道 BufferedReader br = new BufferedReader(reader);//转