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 > 显示所有 ...
随机推荐
- 6_3.springboot2.x数据整合Mybatis(注解和非注解)
1.配置文件 pom.xml 导入mybatis提供的启动器 <dependency> <groupId>org.mybatis.spring.boot</groupId ...
- <scrapy爬虫>爬取quotes.toscrape.com
1.创建scrapy项目 dos窗口输入: scrapy startproject quote cd quote 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) import ...
- 获取调用U9接口时报错的方法
- loj6244 七选五
题意:从n个数中选k个数,问有多少种排列与标准k项串恰好有x个位置相同. 标程: #include<cstdio> using namespace std; typedef long lo ...
- page方法也是模型的连贯操作方法之一
page方法也是模型的连贯操作方法之一,是完全为分页查询而诞生的一个人性化操作方法. 我们在前面已经了解了关于limit方法用于分页查询的情况,而page方法则是更人性化的进行分页查询的方法,例如还是 ...
- 洛谷P4027 [NOI2007]货币兑换
P4027 [NOI2007]货币兑换 算法:dp+斜率优化 题面十分冗长,题意大概是有一种金券每天价值会有变化,你可以在某些时间点买入或卖出所有的金券,问最大收益 根据题意,很容易列出朴素的状态转移 ...
- maven相互依赖导致无法编译成功
起初是新加了个模块,启动前编译时error,提示找不到依赖模块的类,但java文件上是没有报错的. 后经过排查,发现是循环依赖导致的此问题. 如图,弹出框中有循环依赖的模块会显示红色,右键Open M ...
- 33 N皇后问题
原题网址:https://www.lintcode.com/zh-cn/old/problem/n-queens/# n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整 ...
- PAT甲级题目1-10(C++)
1001 A+B Format(20分) Calculate a+b and output the sum in standard format -- that is, the digits must ...
- PAT甲级——A1101 Quick Sort
There is a classical process named partition in the famous quick sort algorithm. In this process we ...