博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中使用字符(字节)读取文本内容
阅读量:4677 次
发布时间:2019-06-09

本文共 3622 字,大约阅读时间需要 12 分钟。

package test;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; /**  * 使用FileInputStream读取文件  * 使用FileOutputStream写入文件  * @author 尹涛  *  */public class Test {    public static void main(String[] args) throws IOException {      //文件地址路径          String path="D:\\test\\test.txt";          //要写入的内容      String str="使用FileOutputStream写入文件";      //输出流对象      FileOutputStream fos=null;      //创建文件输入流对象      FileInputStream fis=null;      try {          //实例化输出流对象参数文件路径地址         fos=new FileOutputStream(path);          //将要写入内容的转换为byte数组         byte[] but=str.getBytes();         //写入文件 参数(byte数组 从0开始写,长度为数组长度)         fos.write(but, 0, but.length);         //实例化输出流对象参数读取的文件路径地址         fis=new FileInputStream(path);          //将读取到文件转存储到byte数组 参数为文件的字节长度         byte[] inputStream=new byte[fis.available()];         //循环读取         while (fis.read(inputStream)!=-1) {             //将byte数组中的内容转换为String内容字符串输出             String s = new String(inputStream, 0, inputStream.length);                 System.out.println(s);        }             } catch (FileNotFoundException e) {        e.printStackTrace();    }finally{        if(fos!=null){            fos.close(); //关闭流对象        }        if(fis!=null){            fis.close();  //关闭流对象        }    }    }}

java的输出流主要由OutputStream和Writer作为基类,而输入流主要由InputStream和Reader作为基类.

字节输入流InputStream读取数据常用的方法

int read()  读取一个字节数据

int  read(byte[] b) 将数据读取到字节数组中

int read(byte[]b,int off, int len) 从输入流中读取最多len长度字节,保存到字节数组b中,保存位置从off开始

void close() 关闭输入流

int avaiable() 返回输入流读取的估计字节数

 

字节输入流OutputStream常用的方法

void write(int c) 写入一个字节数据

void write(btye[] buf) 写入数组buf的所有字节

void write(byte[]b ,int off,int len) 将字节数组中从off位置开始,长度为len的字节数据输出到输出流中

void close() 关闭流对象

 

package test;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException; /**  * 使用字符流写入文本  * 使用字符流读取文本  * @author 尹涛  *  */public class Test1 {    public static void main(String[] args) throws IOException {        //文件地址路径            String path="D:\\test\\test.txt";        //创建FileReader对象        FileReader fr=null;        //创建BufferedReader         BufferedReader br=null;        //创建FileWriter         FileWriter fw=null;        //创建BufferedWriter对象        BufferedWriter bw=null;        try {             //实例化FileReader对象             fr=new FileReader(path);             //实例化BufferedReader对象             br=new BufferedReader(fr);             //实例化FileWriter对象 参数要写入的地址             fw=new FileWriter("E:\\test\\test.txt");             //实例化BufferedWriter 对象             bw=new BufferedWriter(fw);             //对取一行数据             String line="";             //循环读取             while ((line=br.readLine())!=null) {                System.out.println(line);                bw.write(line); //将读取到循环写入                bw.newLine();  //换行            }        } catch (FileNotFoundException e) {                        e.printStackTrace();        }finally{              //关闭顺序为先开的后关             bw.close();             fw.close();            br.close();            fr.close();        }    }

Reader类是读取字节流的抽象类常用方法有

int read() 从输入留读取单个字符

int read(byte[] b) 从输入流中读取b.length 长度的字符保存带数组b中 返回实际读取的字符数

read(byte[] b,int off,int len) 从输入流读取最多len的长度字符,保存到字符数组b中,保存位置从off位置开始.返回实际读取的字符长度

void close() 关闭流

 

Writer类是向文件写入数据的字符流,常用方法有

writer(String str) 将str字符串里包含的字符输出到指定的输出流中

writer(String str,int off,int len) 将str字符串里从off位置开始长度为len的字符输出到输出流中

void close() 关闭流对象

void flush() 刷新输出流

 

转载于:https://www.cnblogs.com/AnotherEon001/p/6913678.html

你可能感兴趣的文章
CSS文本方向
查看>>
Dev Grid拖拽移动行
查看>>
Read
查看>>
this关键字的构造方法的使用
查看>>
Spring的数据库操作---- Spring框架对JDBC的整合---- Spring的数据库操作
查看>>
2016/12/14---- C3P0
查看>>
python tkinter组件学习
查看>>
调用wx.request接口时需要注意的几个问题
查看>>
HotSpot 自动内存管理笔记与实战
查看>>
django缓存
查看>>
winform中真正的透明label
查看>>
(Dynamic Proxy)动态代理模式的Java实现
查看>>
sql三大范式
查看>>
关于TP5模板输出时间戳问题--A non well formed numeric value encountered
查看>>
js延迟加载
查看>>
如何在win 2008 server和win 7上add web site
查看>>
[Selenium]如何实现上传本地文件
查看>>
★不评价别人的生活,是一个人最基本的修养
查看>>
MySQL里执行SHOW INDEX结果中Cardinality的含义
查看>>
centos 7 下vnc弹出窗口太小解决方法
查看>>