IO流 输入和输出文档内容
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流 输入和输出文档内容的更多相关文章
- XML解析之sax解析案例(一)读取contact.xml文件,完整输出文档内容
一.新建Demo2类: import java.io.File; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXPar ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- Citrix 服务器虚拟化之二十八 XenApp6.5发布文档内容
Citrix 服务器虚拟化之二十八 XenApp 6.5发布文档内容 XenApp可发布以下类型的资源向用户提供信息访问,这些资源可在服务器或桌面上虚拟化: 1) 服务器桌面:发布场中服务器的整个 ...
- Python读取本地文档内容并发送邮件
当需要将本地某个路径下的文档内容读取后并作为邮件正文发送的时候可以参考该文,使用到的模块包括smtplib,email. #! /usr/bin/env python3 # -*- coding:ut ...
- 运用 Range 对象处理 Word 文档内容
运用 Range 对象处理 Word 文档内容 在所有 Office 应用程序中,Microsoft Word 可能是应用最广泛的应用程序,它还经常在自定义 Office 解决方案中扮演重要的角色 ...
- ASP 读取Word文档内容简单示例
以下通过Word.Application对象来读取Doc文档内容并显示示例. 下面进行注册Word组件:1.将以下代码存档命名为:AxWord.wsc XML code复制代码 <?xml ve ...
- Linux系统下Java 转换Word到PDF时,结果文档内容乱码的解决方法
本文分享在Linux系统下,通过Java 程序代码将Word转为PDF文档时,结果文档内容出现乱码该如何解决.具体可参考如下内容: 1.问题出现的背景 在Windows系统中,使用Spire.Doc ...
- 织梦DedeCMS首页调用单页文档内容的方法
很多使用织梦dedecms单页文档功能的朋友都想知道如何在织梦首页调用单页文档的内容,下面就教大家具体的实现方法: 具体步骤如下: 首先在首页模板需要显示单页文档内容的地方插入如下代码: {dede: ...
- Mongodb(2)创建数据库,删除数据库,创建集合,删除集合,显示文档内容
显示所有数据库列表:show dbs > show dbs local .078GB runoob .078GB > 显示当前数据库:db > db runoob > 显示所有 ...
随机推荐
- django2 连接mysql实现第一个rest framework
1.安装pymysql,mysqlclient,创建项目django-admin startproject django2 2.settings中把DataBase配置换掉 DATABASES = { ...
- jQuery WeUI
jQuery WeUI jQuery WeUI 是专为微信公众账号开发而设计的一个简洁而强大的UI库,包含全部WeUI官方的CSS组件,并且额外提供了大量的拓展组件,丰富的组件库可以极大减少前端开发时 ...
- MDK(KEIL)使用Astyle格式化代码
关于Astyle Astyle 的全称是Artistic Style的简称,是一个开源的源代码格式化工具,可以对C,C++,C#以及Java等编程语言的源代码进行缩进.格式化.美化. Home Pag ...
- shell 一些题目
在a.log中精确查找含有msyql单词的行a.log文件内容如下: mysqlmysql mysqlmysql aa mysql_mysqla mysql b_mysql aa _mysqla _m ...
- 最近开始学习python,学习到了关于web的内容。
然而在win10中IIS发布CGI脚本的时候遇到了各种各样的问题. 如ISAPI和CGI限制,权限限制等等,一一的百度解决了,最后又出现了 HTTP 错误 502.2 - Bad Gateway Th ...
- spring-data-JPA repository自定义方法规则
一.自定义方法的规则 Spring Data JPA框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如find,findBy,read,readBy,get,getBy,然后对剩下的部分进行解 ...
- JavaScript变量名与函数名的命名规范
JavaScrip变量名与函数名的命名规范严格遵循以下5条: (1)首字符必须是字母.下划线.$,后跟任意的字母.数字.下划线.$ (2)严格区分大小写 (3)不能使用系统的关键字和保留字 (4)命名 ...
- linux 系统优化初始化配置
一.系统优化配置 1.修改yum源 配置国内yum源 阿里云yum源地址 #CentOS 5.x wget -O /etc/yum.repos.d/CentOS-Base.repo http://m ...
- JUC 一 Callable
java.util.concurrent.Callable是一个泛型接口,只有一个call()方法 Callable和Runnable的区别 Callable使用call()方法,Runnable使用 ...
- 线性dp——cf1032
升维来保存第i位按j是否可行,然后枚举i-1个的状态,用5*5n就可以完成递推 /* dp[i][j]==0表示第i步按j不可行 */ #include<bits/stdc++.h> us ...