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 > 显示所有 ...
随机推荐
- 转:Linux环境下段错误的产生原因及调试方法小结
源地址:http://www.cnblogs.com/panfeng412/archive/2011/11/06/2237857.html 补充:http://baike.baidu.com/link ...
- 1day:了解python
一.’计算机语言有哪些? 1.开发语言: 高级语言:Python,Java,PHP,C#,C++,(面向字节码) 低级语言:C.汇编(面向机器码) 备注:机器码是直接和计算机硬件沟通,字节码是通过解释 ...
- IDA 远程调试设置
第一步,先去 IDA dbgsrv 这个目录下,找到要调试的那个远程计算机对应的可用客户端, 比如,android_server, 把它拷贝到目标计算机中, 比如 adb push .... 然 ...
- struts2文件上传,文件类型 allowedTypes对应
'.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript', 3 '.aif' : 'audio/x-aiff', 4 ...
- [模拟退火][UVA10228] A Star not a Tree?
好的,在h^ovny的安利下做了此题 模拟退火中的大水题,想当年联赛的时候都差点打了退火,正解貌似是三分套三分,我记得上一道三分套三分的题我就是退火水过去的... 貌似B班在讲退火这个大玄学... 这 ...
- var、fucntion关键字优先级问题
情况1:使用var关键字定义的变量优先被声明 console.log(a); // undefined var a = 20; /* var a =20; (1)声明变量a -- 优先被执行, ...
- Making the Grade
Making the Grade 给定长度为n的序列\(\{a_i\}\),求构造长度为n的递增序列\(\{b_i\}\),求\(\sum_{i=1}^n|a_i-b_i|\)最小值,\(1 ≤ N ...
- Mysql 数据库crash恢复
之前搭建的ghost博客比较坑,修改comment之后重启数据丢了,对node不熟悉,所以就切换回到wordpress了. 回滚快照之后发现数据库crash了,提示如下信息 2016-06-15 23 ...
- thinkphp 模板主题
一个模块如果需要支持多套模板文件的话,就可以使用模板主题功能. 默认情况下,没有开启模板主题功能,如果需要开启,设置 DEFAULT_THEME 参数即可: 大理石平台精度等级 // 设置默认的模板主 ...
- Sharepoint常见概念
有待补充: 1.环境部署(AD+DNS+SQL+SharePoint前端): SharePoint基本都是这样的结构,可以在多台服务器中,也就是场,当然也可以在一台服务器上.说说这几部分的功能 (1) ...