package io;

 import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer; public class test { public static void main(String[] args) { // testFileOutputStream(); System.out.println("**********这是一个分隔符**********"); // testFileInputStream(); System.out.println("**********这是一个分隔符**********"); // testBufferedOutputStream(); System.out.println("**********这是一个分隔符**********"); // testBufferedInputStream(); System.out.println("**********这是一个分隔符**********"); // testFileWriter(); System.out.println("**********这是一个分隔符**********"); // testFileWriterAppend(); System.out.println("**********这是一个分隔符**********"); // testFileReader(); System.out.println("**********这是一个分隔符**********"); // testBufferedWriter(); System.out.println("**********这是一个分隔符**********"); // testBufferedWriterNewLineAppend(); System.out.println("**********这是一个分隔符**********"); // testBufferedWriterAppend(); System.out.println("**********这是一个分隔符**********"); // testBufferedReader(); System.out.println("**********这是一个分隔符**********"); // testBufferedReaderReadLine(); } /**
* 缓冲输入字符流
*/
private static void testBufferedReaderReadLine() { Reader r = null;
BufferedReader br = null; try { File f = new File("D:\\IO\\testBufferedReader.txt"); r = new FileReader(f);
br = new BufferedReader(r); String temp; while ((temp = br.readLine()) != null) { System.out.println(temp);// 每次读取一行,为字符串,字符串为null时,文件内容读取完毕 } } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { br.close();
r.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输入字符流
*/
private static void testBufferedReader() { Reader r = null;
BufferedReader br = null; try { File f = new File("D:\\IO\\testBufferedReader.txt"); r = new FileReader(f);
br = new BufferedReader(r); int temp;
String str = ""; while ((temp = br.read()) != -1) { str += (char) temp;// 每次读一个字符的ASCII码,转换为字符,添加为字符串,读取ASCII码为-1时,文件内容读取完毕 } System.out.println(str); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { br.close();
r.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字符流
*/
private static void testBufferedWriterAppend() { Writer w = null;
BufferedWriter bw = null; try { File f = new File("D:\\IO\\testBufferedWriter.txt"); w = new FileWriter(f, true);// 将新的字符串添加到文件内容末尾
bw = new BufferedWriter(w); bw.write("新增加一行\r\n"); bw.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bw.close();
w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字符流
*/
private static void testBufferedWriterNewLineAppend() { Writer w = null;
BufferedWriter bw = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testBufferedWriter.txt"); w = new FileWriter(f);
bw = new BufferedWriter(w); bw.write(str); bw.newLine();// 适应计算机系统的换行
bw.append("新增加一行\r\n"); bw.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bw.close();
w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字符流
*/
private static void testBufferedWriter() { Writer w = null;
BufferedWriter bw = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testBufferedWriter.txt"); w = new FileWriter(f);
bw = new BufferedWriter(w); bw.write(str); bw.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bw.close();
w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输入字符流
*/
private static void testFileReader() { Reader r = null; try { File f = new File("D:\\IO\\testFileReader.txt"); r = new FileReader(f); int temp;
String str = ""; while ((temp = r.read()) != -1) { str += (char) temp;// 每次读一个字符的ASCII码,转换为字符,添加为字符串,读取ASCII码为-1时,文件内容读取完毕 } System.out.println(str); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { r.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输出字符流
*/
private static void testFileWriterAppend() { Writer w = null; try { File f = new File("D:\\IO\\testFileWriter.txt"); w = new FileWriter(f, true);// 将新的字符串添加到文件内容末尾 w.write("新增加一行\r\n"); w.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输出字符流
*/
private static void testFileWriter() { Writer w = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testFileWriter.txt"); w = new FileWriter(f); w.write(str); w.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { w.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输入字节流
*/
private static void testBufferedInputStream() { InputStream is = null;
BufferedInputStream bis = null; try { File f = new File("D:\\IO\\testBufferedInputStream.txt"); is = new FileInputStream(f);
bis = new BufferedInputStream(is); byte[] b = new byte[(int) f.length()];// 获取文件的字节长度 bis.read(b); System.out.println(new String(b));// 转换成字符串输出 } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bis.close();
is.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 缓冲输出字节流
*/
private static void testBufferedOutputStream() { OutputStream os = null;
BufferedOutputStream bos = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testBufferedOutputStream.txt"); os = new FileOutputStream(f);
bos = new BufferedOutputStream(os); bos.write(str.getBytes());// 获取字节数组 bos.flush();// 将缓冲区的数据全部刷新到文件中,使用缓冲区时使用 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { bos.close();
os.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输入字节流
*/
private static void testFileInputStream() { InputStream is = null; try { File f = new File("D:\\IO\\testFileInputStream.txt"); is = new FileInputStream(f); byte[] b = new byte[(int) f.length()];// 获取文件的字节长度 is.read(b); System.out.println(new String(b));// 转换成字符串输出 } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { is.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } /**
* 文件输出字节流
*/
private static void testFileOutputStream() { OutputStream os = null; try { String str = "我在等你,等下完这场雨\r\n" + "满城涓涤,净此生的别离\r\n" + "心太入戏,梦流转四季只是回忆\r\n" + "手中焰火向谁泣\r\n"
+ "如果可以,别下完这场雨\r\n" + "放慢朝夕,拾与你的点滴\r\n" + "怕来不及,回眸再重温此刻如往昔\r\n" + "伞外朦胧可是你\r\n";// "\r\n"是Windows系统的换行 File f = new File("D:\\IO\\testFileOutputStream.txt"); os = new FileOutputStream(f); os.write(str.getBytes());// 获取字节数组 System.out.println("完成"); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} finally { try { os.close(); } catch (IOException e) {
System.out.println("IO异常,失败");
e.printStackTrace();
} } } }

IO流 输入和输出文档内容的更多相关文章

  1. XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容

    一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...

  2. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  3. Citrix 服务器虚拟化之二十八 XenApp6.5发布文档内容

    Citrix 服务器虚拟化之二十八  XenApp 6.5发布文档内容 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1)  服务器桌面:发布场中服务器的整个 ...

  4. Python读取本地文档内容并发送邮件

    当需要将本地某个路径下的文档内容读取后并作为邮件正文发送的时候可以参考该文,使用到的模块包括smtplib,email. #! /usr/bin/env python3 # -*- coding:ut ...

  5. 运用 Range 对象处理 Word 文档内容

    运用 Range 对象处理 Word 文档内容   在所有 Office 应用程序中,Microsoft Word 可能是应用最广泛的应用程序,它还经常在自定义 Office 解决方案中扮演重要的角色 ...

  6. ASP 读取Word文档内容简单示例

    以下通过Word.Application对象来读取Doc文档内容并显示示例. 下面进行注册Word组件:1.将以下代码存档命名为:AxWord.wsc XML code复制代码 <?xml ve ...

  7. Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法

    本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...

  8. 织梦DedeCMS首页调用单页文档内容的方法

    很多使用织梦dedecms单页文档功能的朋友都想知道如何在织梦首页调用单页文档的内容,下面就教大家具体的实现方法: 具体步骤如下: 首先在首页模板需要显示单页文档内容的地方插入如下代码: {dede: ...

  9. Mongodb(2)创建数据库,删除数据库,创建集合,删除集合,显示文档内容

    显示所有数据库列表:show dbs > show dbs local .078GB runoob .078GB > 显示当前数据库:db > db runoob > 显示所有 ...

随机推荐

  1. VS2010-MFC(常用控件:列表视图控件List Control 下)

    转自:http://www.jizhuomi.com/software/197.html 上一节是关于列表视图控件List Control的上半部分,简单介绍了列表视图控件,其通知消息的处理和有关结构 ...

  2. System.Convert.cs

    ylbtech-System.Convert.cs 1. 程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c56 ...

  3. 7_2.springboot2.x启动配置原理_2.运行run方法

    当创建完SpringApplication对象之后运行run方法 public ConfigurableApplicationContext run(String... args) { StopWat ...

  4. SQL语句中exists和in的区别

    转自https://www.cnblogs.com/liyasong/p/sql_in_exists.html 和 http://blog.csdn.net/lick4050312/article/d ...

  5. 面试系列 31 zk都有哪些使用场景

    大致来说,zk的使用场景如下,我就举几个简单的,大家能说几个就好了: (1)分布式协调:这个其实是zk很经典的一个用法,简单来说,就好比,你A系统发送个请求到mq,然后B消息消费之后处理了.那A系统如 ...

  6. java_序列化

    import java.io.*; class People implements Serializable { /* * 序列化和反序列化的时候,会抛出就NotSerializableExcepti ...

  7. Android开发 获取View的尺寸的2个方法

    前言 总所周知,在activity启动的onCreate或者其他生命周期里去获取View的尺寸是错误的,因为很有可能View并没有初始化测量绘制完成.你这个时候获取的宽或的高不出意外就是0.所以,我们 ...

  8. windows10 vs2019 + opencv 3.4.7环境搭建

    windows vs2019 + opencv 3.4.7环境搭建 安装Opencv 3.4.7 下载 Opencv 第1步 进入 opencv releases 页面,点击 "Window ...

  9. 解决Mybatis的invalid bound statement (not found)异常

    使用Maven构建SSM时, 需要在pom.xml中配置一些信息, 否则mapper.xml就无法被扫描到, 程序就会抛invalid bound statement (not found)异常 解决 ...

  10. DataTime 和 时间转化

    如果知道tostring 的字符串格式那么可以根据字符串格式转化成 DateTime string timeText = DateTime.Now.ToString("yy/MM/dd HH ...