Java中的IO流之输出流|乐字节

一、输出流
1、抽象类:OutputStream 和 Writer


2、文件节点类: FileOutputStream 和 FileWriter
public class WriteFile {
public static void main(String[] args) {
//1、建立联系 File对象 源头 目的地
File dest=new File("c:/IO/print.txt");
//2、选择流 文件输出流 OutputStream FileOutputStream
OutputStream out=null;
//以追加形式写出文件 必须为true 否则会覆盖
try {
out=new FileOutputStream(dest,true);
//3、操作
String str="shsxt is very good \r\n good good good";
//字符串转成字节数组
byte[] data=str.getBytes();
out.write(data,0,data.length);
out.flush();//强制刷新出去
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件未找到");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("文件写出失败");
}finally{
try {
if(out!=null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("关闭输出流失败");
}
}
}
}
//1、创建源
File dest=new File("f:/IO/char.txt");
//2、选择流
Writer wr=new FileWriter(dest,true);
//3、写出
String str="锄禾日当午\r\n码农真辛苦\r\n一本小破书\r\n一读一上午";
wr.write(str);
//追加内容
wr.append("我就是追加进去的");
wr.flush();//强制刷出
//4、关闭资源
wr.close();
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException,IOException{
// 1、建立联系 源(存在且为文件) 目的地(文件可以不存在)
File src = new File(srcPath);
File dest = new File(destPath);
if(!src.isFile()){//不是文件或者为null时抛出异常
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
// 2、选择流
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
// 3、操作
byte[] flush = new byte[1024];
int len = 0;
// 读取
while (-1 != (len = in.read(flush))) {
// 写出
out.write(flush, 0, len);
}
out.flush();// 强制刷出
// 关闭流 先打开的后关闭
out.close();
in.close();
}
3、缓冲处理流:BufferedOutputStream 和 BufferedWriter
public static void copyFile(String srcPath, String destPath) throws FileNotFoundException,IOException{
// 1、建立联系 源(存在且为文件) 目的地(文件可以不存在)
File src = new File(srcPath);
File dest = new File(destPath);
if(!src.isFile()){//不是文件或者为null时抛出异常
System.out.println("只能拷贝文件");
throw new IOException("只能拷贝文件");
}
// 2、选择流
InputStream in = new BufferedInputStream(new FileInputStream(src));
OutputStream out =new BufferedOutputStream(new FileOutputStream(dest));
// 3、操作
byte[] flush = new byte[1024];
int len = 0;
// 读取
while (-1 != (len = in.read(flush))) {
// 写出
out.write(flush, 0, len);
}
out.flush();// 强制刷出
// 关闭流 先打开的后关闭
out.close();
in.close();
}
}
//1、创建源 仅限于 字符的纯文本
File src=new File("f:/char.txt");
File dest=new File("f:/testIO/char.txt");
//2、选择流
BufferedReader reader=new BufferedReader(new FileReader(src));
BufferedWriter wr=new BufferedWriter(new FileWriter(dest,true));pend(msg2);
//3、新增方法操作
String line=null;
while(null!=(line=reader.readLine())){
wr.write(line);
//wr.append("\r\n");
//换行符号
wr.newLine();
}
wr.flush();//强制刷出
// 4、关闭流 先打开的后关闭
out.close();
in.close();
4、转换处理流:OutputStreamWriter
//写出文件 编码
BufferedWriter bw=new BufferedWriter(
new OutputStreamWriter(
new BufferedOutputStream(
new FileOutputStream(
new File("f:/testIO/char.txt")
)
),"utf-8"
)
);
String info=null;
while(null!=(info=br.readLine())){
bw.write(info);
bw.newLine();
}
bw.flush();
bw.close();
5、字节数组节点类: ByteArrayOutputStream
/**
* 字节数组输出流:操作与文件输出流有些不同,有新增方法,所以不可以使用多态
* @throws IOException
*/
public static byte[] write() throws IOException{
//目的地 字节数组
byte[]dest;
//选择流 不同点:不需要将目的地放入new ByteArrayOutputStream()
ByteArrayOutputStream bos=new ByteArrayOutputStream();
//操作 写出, 可以当作将本地的内容通过字节数组写入服务器
String msg="字节数组输入流:操作与文件输入流操作一致";
byte[]info=msg.getBytes();
//将内容写入bos
bos.write(info,0,info.length);
//不同点:获取数据 toByteArray():是将字节数组输出流转为字节数组
dest=bos.toByteArray();
//释放资源
bos.close();//由于bos在jvm中,所以关闭与否不影响
return dest;
}
Java中的IO流之输出流|乐字节的更多相关文章
- Java中的IO流之输入流|乐字节
亲爱的乐字节的小伙伴们,小乐又来分享Java技术文章了.上一篇写到了IO流,这篇文章着重 谈谈输入流,再下次再说输出流. 点击回顾上一篇:乐字节Java之file.IO流基础知识和操作步骤 一. 输入 ...
- java中的IO流
Java中的IO流 在之前的时候我已经接触过C#中的IO流,也就是说集中数据固化的方式之一,那么我们今天来说一下java中的IO流. 首先,我们学习IO流就是要对文件或目录进行一系列的操作,那么怎样操 ...
- Java中的IO流大体介绍
由于Java中的IO流是在是知识点繁多,所以我大约花了1周的时间将其整理起来.但是整理起来后并不是将完事了,我还是要分字节流和字符流来讲述.然后字节流和字符流中还有是否带有缓冲流. 讲述完IO流后我将 ...
- Java中的IO流,Input和Output的用法,字节流和字符流的区别
Java中的IO流:就是内存与设备之间的输入和输出操作就成为IO操作,也就是IO流.内存中的数据持久化到设备上-------->输出(Output).把 硬盘上的数据读取到内存中,这种操作 成为 ...
- Java中的IO流(五)
上一篇<Java中的IO流(四)>记录了一下Properties类,此类不属于IO流,它属于集合框架.接下来说一下IO流中的其它流 一,打印流PrintStream PrintStream ...
- Java中的IO流(六)
上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...
- JAVA 中的IO流
Java中的IO流是用来处理设备与设备之前的数据传输,在java中以流的形式传输.流分为两类:字节流和字符流. 字节流:InputStream,OutPutSteam.(计算机内的数据都是以字节存储的 ...
- Java中的IO流(四)
上一篇<Java中的IO流(三)>把IO流中的文件及目录操作的对象File类记录了一下,本篇把本不属性IO流但又和IO流有关系的一个对象作一下记录,此对象本属于集合框架里的一个子集,即Pr ...
- Java中的IO流(二)
上一篇<Java中的IO流(一)>把学习IO流的字符流作了一下记录,本篇把字节流记录一下. 一,Java中的字节流 Java中的字节流的操作方式与字符流的操作方式大致相同,连方法名都是类似 ...
随机推荐
- 42、JDBC数据源案例
一.JDBC数据源案例 1.概述 Spark SQL支持使用JDBC从关系型数据库(比如MySQL)中读取数据.读取的数据,依然由DataFrame表示,可以很方便地使用Spark Core提供的各种 ...
- shell history 命令
1.history命令可以显示历史执行过的命令: 2.使用!+序号执行该序号对应的命令: 例子 $ history sed 's/haha/hello/g' test cat test cat tes ...
- sudo 命令
su命令和su -命令最大的本质区别就是: 前者只是切换了root身份,但Shell环境仍然是普通用户的Shell: 而后者连用户和Shell环境一起切换成root身份了.只有切换了Shell环境才不 ...
- SignalR简单实用_转自:https://www.cnblogs.com/humble/p/3851205.html
一.指定通信方式 建立一个通讯方式需要一定的时间和客户机/服务器资源.如果客户机的功能是已知的,那么通信方式在客户端连接开始的时候就可以指定.下面的代码片段演示了使用AJAX长轮询方式来启动一个连接, ...
- python操作toml文件
# -*- coding: utf-8 -*- # @Time : 2019-11-18 09:31 # @Author : cxa # @File : toml_demo.py # @Softwar ...
- centos7磁盘分区、格式化、挂载
1.分区:a. 查看磁盘分区表: # fdisk -l b. 查看指定磁盘分区表: # fdisk -l /dev/sdb c. 分区命令: fdisk /dev/sdb 常用命令: n:创建新分区 ...
- 第10组 Alpha冲刺(3/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 对推送模块进行详细划分 基于用户的协同过滤,寻找更感兴趣的话题 学习API文档 ...
- How to Hack Unity Games using Mono Injection Tutorial
https://guidedhacking.com/threads/how-to-hack-unity-games-using-mono-injection-tutorial.11674/ Unity ...
- gogs 实现webhook钩子(php接口形式)
1.概要流程 2.准备工作 gogs服务器 linux网站服务器(宝塔) 本地客户端 3.编写钩子访问的接口 在public下新建githook.php文件,代码如下: <?php $cmd = ...
- vs2017在前端页面使用F12无法转到js脚本函数定义
这样设置后就可以正常使用了